String to int's


Could anyone advice if there is a function in C++ to convert a string to small ints, 0-255.

And a string to float's

Who is Participating?
SteHConnect With a Mentor Commented:
no for STL string's you need to use

int i = atoi (strInt.c_str ());
float f = atof (strFloat.c_str ());
The C++ way would be using stringstreams. Find more ways at:
AntoniRyszardAuthor Commented:
Something like this?

   string strInt = "125";
   string strFloat = "125.5";

   int i = atoi(strInt);
   float f = atof(strFloat);

And should the string work, as a  char * does?
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

AntoniRyszardAuthor Commented:
No, string is not the same as char *. You can get a const char * at the contents of the string with the .c_str() method:

   string strInt = "125";
   string strFloat = "125.5";

   int i = atoi(strInt.c_str());
   float f = atof(strFloat.c_str());

As SteH says, you can also use strstream to do the work without having to use C-style strings.

AntoniRyszardAuthor Commented:

Are there any advantages of using char * as apposed to strings?

I thought strings's were just a modern addition to the language, but much the same as a char *.
strings have memory management built in. You can add to a string chars, char* and strings without the necessity to look whether the allocated memory is sufficient. So I would see the advantages on the strings side.
AntoniRyszardAuthor Commented:

I just remembered the variables I wanted to convert to an int, were char[]

I used this code to get the values from a file:

sscanf(buffer, "%s %s %s", &field0, &field1, &field2);

but as I understand although field0,1,2 were delcared as 200 char[] in size. If I then store the field0 value as a string:

string temp = field0.

The empty spaces from field0 would not be stored in temp.

So could I still use the atoi, I tried strlwr(field0); and this worked.

Yes, string.c_str () will give you a const char* to be used as any other char* (or char[]).

But in C++ the way to go should be
string buffer;
stringstream ss (buffer); //generate a string stream from the buffer.
int f0, f1, f2;
ss >> f0 >> f1 >> f2; // reading is done with type checking unlike sscanf

or for strings you can do:
string sf0, sf1. sf2;
ss >> sf0 >> sf1 >> sf2;
as if ss was a normal stream (like a file).
AntoniRyszardAuthor Commented:
The complete code is, I think it works: Does everything seem ok to you?

Each line in the file, has three strings

struct has three variables:

string field0;
string field1;
int field2;

void function(char * fileName){
   char buffer[300];
   char field0[100], field1[100], field2[100];  
   ifstream in(fileName);  
      in.getline(buffer, 300);          
      int stringCount = sscanf(buffer, "%s %s %s", &field0, &field1, &field2);
      programStrut newObject;
      if(stringCount == 3){        
         newObject.field0 = strlwr(field0);
         newObject.field1 = atoi(field1);
         newObject.field2 = strlwr(field2);      
try using :
std::string strInt = "125";
int i = atoi(&strInt[0]);

&strint[0] gives you a char pointer instead of a const char pointer.
no, cause field2 is declared as int in your struct but you try to pass a string to it.
i think it has to be:

 newObject.field0 = strlwr(&field0[0]);
 newObject.field1 = strlwr(&field1[0]);
 newObject.field2 = atoi(&field2[0]);
>> sscanf(buffer, "%s %s %s", &field0, &field1, &field2);
>> string temp = field0.
>> The empty spaces from field0 would not be stored in temp.
I think you make an error here. field0 won't contain any spaces since sscanf will use any whitespace in buffer to terminate a string from the next one. Whereas the string temp will contain everything of field0 up to its terminating 0.

And for me the construct to scan first strings from a buffer string to convert them later to numbers seem overcomplication. Why don't you use
sscanf (buffer, "%s%s%d", &newObject.field0, &newObject.field1, &newObject.field2); // format string could be equally "%s %s %d"
No need to convert field 2 from char to int later.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.