the title says it all Lets say the year is 1981 then i want to be able to put the digits 1,8,9 and 1 in a variable, then I also want to be able to put the whole number in an variable. ... How can I do this?
You can try to read the input from the user into a string, that will allow you to access every single digit easily.
As for converting strings to ints, see: http://www.cplusplus.com/forum/articles/9645/
If you use the type int for processing of numbers then it has its own characteristics as maximum and minimum values, the number of decimal digits and so on. So insetad of magic values 10 and 1000000 it is better to use predefined values for the type int. These values can be gotten if to include header <limits>
So your program could look the following way
#include<iostream>
#include<cstdlib>
#include <limits>
usingnamespace std;
int main()
{
constint base = 10;
int digits[numeric_limits<int>::digits10 + 1];
int num;
do
{
cout << "Enter number from 0 to " << numeric_limits<int>::max() << ": ";
cin >> num;
if ( !cin )
{
cout<<"Invalid Input! Try Again\n";
cin.clear();
cin.ignore();
}
} while ( !cin );
int i = 0;
do
{
digits[i++] = num % base;
} while ( num /= base );
cout << "i = " << i << endl;
system("pause");
return 0;
}
char input[10]; //string of maximum 10 characters
cin >> input; //store the user input in the string, for instance 12345
//now input[0] = 1, input[1] = 2, input[2] = 3, etc.
//to get the string as a number:
int number;
number = atoi(input); //atoi() function converts a c-string to an integer
its working now...and its counting every digit off my input thanks...now all i need to do is to figure out how to display every number that has been extracted...
@maculhet
its working now...and its counting every digit off my input thanks...now all i need to do is to figure out how to display every number that has been extracted...
Did you mean "every digit"?
As you know how many there are filled elements in the array due to the value in 'i', you can output the array in the reverse order