String -> Integer

Started by
18 comments, last by vbuser 21 years, 6 months ago
can anyone tell me to to change String Variable to Integer Variable? that if I open a file , the text there is 123, how can I change it to integer 123 and do some caculation? Please Help ME! I''''m a beginner..
Please Help ME!I''m a beginner.. :P
Advertisement
I believe for Visual Basic, you are loking for the CInt() function.

Dim str as String
dim val as Integer

str = "123"
val = CInt(str)

val now equals 123. Or at least, it should!
It's not what you're taught, it's what you learn.
YEAH!
I know that in Visual Basic......

But I want to know is how to do it in C++?

I need to learn that......
Please Help ME!I''m a beginner.. :P
The atoi() function is one way.
It's not what you're taught, it's what you learn.
And, if you''re using C++, you might want to look into the function fscanf.

You''d use it something like this (as I recall):

fscanf(file,"%d",&variable);

this statement would read a number from the file, and place the decimal number into the variable named variable.

If you have a string, not from a file, that you want to use, use the function scanf instead of fscanf.

-Arek the Absolute
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Use the atoi() function.

Don''t listen to me. I''ve had too much coffee.
Actually Arek, that was wrong on almost every count.
The scanf family of functions are C (not C++).
If you''re scanning from an inputstream other then STDIN, then you would use fscanf (as you noted).
Pure scanf takes it''s input from STDIN.
If you want to scan from a string, use sscanf.

But if you want to easily convert a string to an integer, use atoi.


-Neophyte
If it's in a file, you can just always do...

int myVar;
fstream myFile;

file.open("myPath/MyFile.myExtention", ios::in, ios::nocreate);
if (file)
{
file >> myVar;
printf("I read: %d, myVar);
file.close();
}
else
{
printf("No file...");
}

edit: Or just use atoi()

[edited by - Programmer One on October 23, 2002 9:38:25 PM]
Read it off a std::stringstream

#include <sstream>int main();{   int i;   float f;   std::stringstream ss;   ss.str( "1000" );   ss >> i;   ss.str( "1.5e6" );   ss >> f;} 


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ok, you COULD use fancy functions and whatnot, or you could do it the cool way, which is to program it yourself. Basically, what you''re going to want to to is to make sure your string is in char[] format (an array of chars) terminated with the ''\0'' char.

Next, you''re going to want to loop through each char, convert it to an int, subtract 48(converts the ascii code to an int), and then add it to a variable (keeping in mind the powers of 10 which come with each place) Anyway, here''s the code, figure it out:

int cInt(char* const input) // takes an array of chars
{
int testChar;
int result = 0;

for(int i=0; input != ''\0''; i++) // reads the string, stops when it reaches terminator
{
testChar = int(input); // converts current char into an int;<br> testChar -= 48; // converts askii code into int<br><br> result *= 10;<br> result += testChar;<br> }<br><br> return result;<br>}<br><br>If you want a better explination, check a later topic, something like ''stupid question'' or easy, or something, it was about testing if input was an int… I used the same code there, just modified it and explained it better. </i>

This topic is closed to new replies.

Advertisement