Solved

c++ substring to char

Posted on 2006-11-03
7
2,179 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
7 Comments
 
LVL 5

Expert Comment

by:rmahesh
Comment Utility
This should work:

c = strNum.at(I);

Mahesh
0
 
LVL 5

Expert Comment

by:rmahesh
Comment Utility

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
Comment Utility
is there any other way to do it, see I dont think that I am allowed to use either of those methods
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Expert Comment

by:hclgroup
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 9 70
groovy example issue 10 64
Device same like our heart 12 42
How can i compile this github project?? 2 38
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now