Solved

c++ substring to char

Posted on 2006-11-03
7
2,184 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is an explanation of a simple data model to help parse a JSON feed
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

776 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