• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1380
  • Last Modified:

String to int's

Hello,

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

Thanks
0
AntoniRyszard
Asked:
AntoniRyszard
  • 5
  • 5
  • 2
  • +1
1 Solution
 
SteHCommented:
The C++ way would be using stringstreams. Find more ways at:
http://www.codeguru.com/forum/showthread.php?s=&threadid=231054
0
 
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?
0
 
SteHCommented:
no for STL string's you need to use

int i = atoi (strInt.c_str ());
float f = atof (strFloat.c_str ());
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AntoniRyszardAuthor Commented:
Thanks
0
 
bcladdCommented:
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.

-bcl
0
 
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 *.
0
 
SteHCommented:
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.
0
 
AntoniRyszardAuthor Commented:
Hello,

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.

Thanks
0
 
SteHCommented:
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).
0
 
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);  
   
   while(!in.eof()){  
      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);      
     
         programVector.push_back(newObject);
      }  
}      
0
 
vo1dCommented:
try using :
std::string strInt = "125";
int i = atoi(&strInt[0]);

&strint[0] gives you a char pointer instead of a const char pointer.
0
 
vo1dCommented:
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]);
0
 
SteHCommented:
>> 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.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now