Turning a member of a string to an integer?
This is bothering me, How do I turn a member of a string to an integer, let''s say:
char string1[9];
int number;
scanf("%s", string1);
some people might type in something like "hello", which is a string, but others might and will type in 5, or 6, or a number.
So basically string1[0] is a number, but in string format.
How do I like convert it into an integer format (store the value in number) without change its value?
Like if someone type in a 6,
and I do:
printf("%d\n", number)
It''ll like display 6, ya know?
Thanx
I want to work for Squaresoft ;)
I want to work for Squaresoft ;)
Convert it to an integer..
If you have:
char number[] = "32768"; // Number as text..
then you can say:
int n;
sscanf(number, "%d", &n);
printf("The number is %d\n", n);
Or, use the C-Runtime function atoi() (ascii to integer)
int n;
n = atoi(number);
printf("The number is %d\n", n);
Hope that helps..
// CHRIS
If you have:
char number[] = "32768"; // Number as text..
then you can say:
int n;
sscanf(number, "%d", &n);
printf("The number is %d\n", n);
Or, use the C-Runtime function atoi() (ascii to integer)
int n;
n = atoi(number);
printf("The number is %d\n", n);
Hope that helps..
// CHRIS
// CHRIS [win32mfc]
AWWWW, I need to familiarize myself with the standard C library!!! That will be my goal for the next few weeks, hehe...
Thanks for the help man! I greatly appreciated
I want to work for Squaresoft ;)
Thanks for the help man! I greatly appreciated
I want to work for Squaresoft ;)
I want to work for Squaresoft ;)
Let this be a testament to the dangers of self education:
I never knew about atoi(), I actually wrote my own function to do this..I felt pretty good at the time, now I feel sort of ridiculous ..you can all start laughing now..
"Like all good things, it starts with a monkey.."
I never knew about atoi(), I actually wrote my own function to do this..I felt pretty good at the time, now I feel sort of ridiculous ..you can all start laughing now..
"Like all good things, it starts with a monkey.."
"Like all good things, it starts with a monkey.."
you can also do..
scanf("%s", string1);
printf("The first number is %d", (int) string1[0]-''0'');
..-=gLaDiAtOr=-..
scanf("%s", string1);
printf("The first number is %d", (int) string1[0]-''0'');
..-=gLaDiAtOr=-..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement