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
Solved

c++ substring to char

Posted on 2006-11-03
7
2,186 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This article will show, step by step, how to integrate R code into a R Sweave document
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…

856 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