Solved

c++ substring to char

Posted on 2006-11-03
7
2,189 Views
Last Modified: 2008-01-09
Okay I want to take a single char from a sting and place into a char variable.
Heres my current code:

//The convert function takes a string of numbers and returns its integer value.
int convert(string strNum)
{
      char c;
      const int Size = strNum.size();
      int I;
      int J;
      int X;
      int Y;
      int total;
      int tmpint1;
      int tmpint2;
      const int plusone = Size + 1;
      
      for(I = 1; I < plusone; I++)
      {
            X = Size - I;
            if (X > 0)
            {
                  Y = 10;
                  for(J = 1; J < X; J++)
                  {
                        Y = Y * 10;
                  }
            }
            else Y = 1;
            c = strNum.substr(I,I);
            tmpint1 = c - '0';
            tmpint2 = tmpint1 * Y;
            total = total + tmpint2;
      }
      return total;
}

The only error with it is:

sr1 $ CC -o Program08 Program08.cpp
"Program08.cpp", line 182: Error: Cannot assign std::string  to char.
1 Error(s) detected.

How could I fix this?
0
Comment
Question by:xneo27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Expert Comment

by:rmahesh
ID: 17868608
This should work:

c = strNum.at(I);

Mahesh
0
 
LVL 5

Expert Comment

by:rmahesh
ID: 17868656

But I believe that you want to do this conversion yourself for some reason.

Hope you know "atoi" and related functions doing the same.
0
 

Author Comment

by:xneo27
ID: 17868759
is there any other way to do it, see I dont think that I am allowed to use either of those methods
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:hclgroup
ID: 17868850
Why not just use builtin c++ functions to do your convertion

e.g.

#include <stdlib.h>

atoi(strNum.c_str());
0
 
LVL 5

Expert Comment

by:rmahesh
ID: 17869017
You should just be able to call atoi ... ( atoi(strNum.c_str()); )  whats the error you get when u try to call?
0
 
LVL 4

Expert Comment

by:chhokra_expert
ID: 17871339
if it is a c++ string, you can simply do c = strNum[i] where string::size_type i = 0; i < strNum.length(); ++i
strNum.at(i) is its functional equivalent
0
 
LVL 2

Accepted Solution

by:
Ehsan_Noureddin earned 500 total points
ID: 17885943
chhokra_expert is correct. if you have string why you use these code ?!
but here i will write you the function for changing the String to Integer :

int String_to_Integer(char *s)
{
      int value=0;
      while (*s)
      {
            value*=10;
            value+=*s-48;          // Or you can use : value+=*s-'0';
            s++;
      }
      return value;
}

this function will convert the string to Integer (just Integer not Float) .
you can use this
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 133
Rubik's Cube Code for Effective Presentation 3 74
VB.NET String Settings and Temp Folder Question 3 70
"lblTime is not declared" 3 31
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question