Solved

String to int's

Posted on 2004-09-07
13
1,373 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now