?
Solved

c++ substring to char

Posted on 2006-11-03
7
Medium Priority
?
2,193 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

752 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