Feb 4, 2012 at 7:27pm UTC
How do I get an input of very large number, store them as characters and store them into a vector?
I tried using cin.get but it only takes in one character at a time, whereas the input is of many digits.
Very confused, please help!! Much thanks!
Feb 4, 2012 at 7:52pm UTC
cin.get will take many digits. cin>> will work just as well
example:
1 2 3 4 5 6 7 8 9 10
int i;
cin >> i;
int num_digits=0;
for (int decimal = 1; i/decimal != 0; decimal*=10)
num_digits++;
vector<char > mystring;
while (num_digits--)
mystring.push_back(num/pow((int )10,num_digits));
where we have this available:
1 2 3 4 5 6 7 8 9
int pow(int x, int y)
{
int output=1;
for (int i=0; i<y; i++)
output *= x;
return output;
}
Edit: Is it funny that I press F7 in my browser when I've finished correcting the posted code?
Last edited on Feb 4, 2012 at 7:56pm UTC
Feb 5, 2012 at 1:46am UTC
what if i have to store the input as characters instead of ints?
will this work?
cout << "Enter a non-negative number: \n";
char number = '0';
cin.get >> number;
vector<char> number;
but what if the input has many digits? do i have to initialize every digit and store it in a type char? and then transfer it to the vector?
Feb 5, 2012 at 7:43am UTC
The code above DOES store it as a vector of chars:
vector<char > mystring;
is a vector of chars.
First I'm inputting everything as an int, and then converting it to a char array.