Solved

String to int's

Posted on 2004-09-07
13
1,377 Views
Last Modified: 2008-02-01
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
Comment
Question by:AntoniRyszard
[X]
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
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 11996502
The C++ way would be using stringstreams. Find more ways at:
http://www.codeguru.com/forum/showthread.php?s=&threadid=231054
0
 

Author Comment

by:AntoniRyszard
ID: 11996630
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
 
LVL 13

Accepted Solution

by:
SteH earned 20 total points
ID: 11996753
no for STL string's you need to use

int i = atoi (strInt.c_str ());
float f = atof (strFloat.c_str ());
0
Technology Partners: 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!

 

Author Comment

by:AntoniRyszard
ID: 11996781
Thanks
0
 
LVL 11

Expert Comment

by:bcladd
ID: 11996797
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
 

Author Comment

by:AntoniRyszard
ID: 11996896

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
 
LVL 13

Expert Comment

by:SteH
ID: 11997042
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
 

Author Comment

by:AntoniRyszard
ID: 11997518
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
 
LVL 13

Expert Comment

by:SteH
ID: 11997703
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
 

Author Comment

by:AntoniRyszard
ID: 11997842
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
 
LVL 11

Expert Comment

by:vo1d
ID: 12218082
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
 
LVL 11

Expert Comment

by:vo1d
ID: 12218201
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
 
LVL 13

Expert Comment

by:SteH
ID: 12218543
>> 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

Industry Leaders: 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!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

729 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