If i understand you clearly, you are trying to take a string of characters and derive the integer value from it. So, for example the user might type the number 4235 and you would have the string "4235". What you want is when they enter 4235, to have an integer representing that value.
What i was saying was a function was written in stdio.h already to do that. If you have ever used printf() instead of cout, it will be familiar.
1 2 3 4 5 6 7 8 9 10 11
|
#include <stdio.h> /* same as <cstdio> */
int main(){
int input_decimalInteger;
double input_float;
printf("Enter an integer, then a floating point number.\n");
scanf("%d %Lf",&input_decimalInteger,&input_float);
printf("Your integer was %d, \nand your float was %f.\n",input_decimalInteger,input_float);
return 0;
}
|
These functions are a little bit tricky. You can find their descriptions at
http://www.cplusplus.com/reference/clibrary/cstdio/
but i'll try and explain a little in case you don't feel like going there.
scanf() looks at stdin, short for standard in. This is the stream (a giant array type thing) that command-line input goes. The first argument to scanf() is a format string. This tells it what to look for. I told it to look for a regular decimal integer (%d), and then a double size floating point (%Lf). I then told it where to put the input. Notice I put '&' before the variable names when i gave them to scanf(). That means I'm really passing their memory addresses. Because a function can only return one value, and because scanf() is meant to find more than one number, it goes ahead and writes to those addresses from within the function. printf() is similar, but because it doesn't change any values, you send the variables' values, not addresses.
Another function to suit your purpose, and might be more obvious is as follows, from the iostream library.
1 2 3 4 5 6 7 8 9 10 11
|
#include <iostream>
using namespace std;
int main(){
int input_integer;
cout << "Enter a decimal integer.\n";
cin >> input_integer;
cout << "The number you entered is:\n" << input_integer;
return 0;
}
|
After testing something for myself, it turns out cin can also accept multiple values.
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
using namespace std;
int main(){
int input_integer;
double input_float;
cout << "Enter a decimal integer.\n";
cin >> input_integer >> input_float;
cout << "The numbers you entered were " << input_integer << " " << input_float << endl;
return 0;
}
|
I hope that helps.