Archived

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

vbuser

String -> Integer

Recommended Posts

vbuser    122
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
Waverider    169
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
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
Neophyte    595
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
Programmer One    746
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
Beowulf_    122
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
smart_idiot    1298



      
int cInt(const char *input) // takes an array of chars

{
int result = 0;

for(;*input>='0'&&*input<='9';++input)
result=result*10+*input-'0';

return result;
}


Actually, since it's signed, this might be better. . .


  
int cInt(const char *input)
{
int result = 0;

while(*input==' ')
++input;

if(*input=='-')
{
++input;
for(;*input>='0'&&*input<='9';++input)
result=result*10-*input-'0';
}
else
for(;*input>='0'&&*input<='9';++input)
result=result*10+*input-'0';

return result;
}


[edited by - smart_idiot on October 24, 2002 7:19:52 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
"You don''t want char* const, but const char*. The former doesn''t prevent you from modifying the characters."

wouldn''t const char* just make the pointer constant? i.e. a constant pointer to a char? as opposed to a pointer to a constant char?

Share this post


Link to post
Share on other sites
Beowulf_    122
"You don''t want char* const, but const char*. The former doesn''t prevent you from modifying the characters."

wouldn''t const char* just make the pointer constant? i.e. a constant pointer to a char? as opposed to a pointer to a constant char?

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by smart_idiot
When in doubt, odds are that Fruny is right.

I don''t know if I want to assume the implied responsibility, I''m wrong way too often for my taste. (especially since there should be a ss.clear(); before ss.str( "1.5e6" ), just to be safe )

quote:

wouldn''t const char* just make the pointer constant? i.e. a constant pointer to a char? as opposed to a pointer to a constant char?


Read it right to left :

char* p = p is pointer to char
const char* p = p is pointer to const char
char* const p = p is const pointer to char
const char* const p = p is const pointer to const char

Share this post


Link to post
Share on other sites
Beer Hunter    712
quote:
Original post by Beowulf_
wouldn''t const char* just make the pointer constant? i.e. a constant pointer to a char? as opposed to a pointer to a constant char?
Nope. Try it out:

void f(char* const foo) { *foo = ''T''; }
void g(const char* foo) { *foo = ''T''; }

Share this post


Link to post
Share on other sites
Beowulf_    122
Ok, thanks for the info. (I just learned about pointers yesterday) So is that right to left thing a real rule, or is that just a trick to remember pointers or what?

Hey, as long as you''re helping out a NB, do you know any good intros to making windows apps? I tried dissassembing the default visual studio one, but it had way too much baggage, and was really hard to figure out what each thing did.

Share this post


Link to post
Share on other sites
Outworlder    100
quote:
Original post by Fruny
I don''t know if I want to assume the implied responsibility, I''m wrong way too often for my taste. (especially since there should be a ss.clear(); before ss.str( "1.5e6" ), just to be safe )




Yes, there should be a ss.clear(). You wouldn''t believe the amount of time I lost trying to debug this.

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by Outworlder
Yes, there should be a ss.clear(). You wouldn''t believe the amount of time I lost trying to debug this.


About as much as I lose everytime I forget Nowadays, I just stick a boost::lexical_cast in there ( int i = boost::lexical_cast<int>( "1000" ); )

quote:

Ok, thanks for the info. (I just learned about pointers yesterday) So is that right to left thing a real rule, or is that just a trick to remember pointers or what?


Your type is cut in two parts : before the * and after the *. What is before refers to what you''re pointing to, what is after refers to the pointer itself. So yeah, it''s as close as a ''rule'' as you can get. To the point that some people advise to write char const* p instead of const char* p so that reading it from right to left exactly reads "p is a * (pointer) to const char". Which you may, or may not want to do (the compiler doesn''t care, it is still on the left of the *).

quote:

Hey, as long as you''re helping out a NB, do you know any good intros to making windows apps? I tried dissassembing the default visual studio one, but it had way too much baggage, and was really hard to figure out what each thing did.


Nope, sorry.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites