Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

vbuser

String -> Integer

This topic is 5750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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..

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
YEAH!
I know that in Visual Basic......

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

I need to learn that......

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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[i]); // converts current char into an int;
testChar -= 48; // converts askii code into int

result *= 10;
result += testChar;
}

return result;
}

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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!