I have to enter a three digit number and then get an output of
the first digit
the second digit
the third digit
~Sam Mixter~
I have to enter a three digit number and then get an output of
the first digit
the second digit
the third digit
~Sam Mixter~
Please post your code. Its not easy to guess what's going on.
if you don't know how to do an array, here is a liitle bit of code...
hope this helpsCode:#include <iostream.h> #include <stdlib.h> int main() { int array[2]; // remember that C++ regards 0 as one of the parts of the array cout << "Enter your 3 digit number: "; cin >> array[2]; cout << "\n"; cout << array[0]; cout <<"\n"; cout << array[1]; cout << "\n"; cout << array[2]; system("pause"); // system calls are crap, I know return 0; }
Hi
just few remarks about the code above:
int array[2]; -
declare an array with only 2 elements of type int
they can be accessed via indexes 0 and 1 so the last cout with array[3] is illegal, it will print a garbage.
cin >> array[2]; -
is it supposed to read several int's from the cin? if true - then you are in trouble.
this expression just read one integer value from the keyboard and places it in an unexisting place - array[2];
so to correct the code above - enlarge the array ehough to hold all the three elements and read them one by one.
damyan
in this little code, when it inputs array[2], it gets all three numbers. eg. if you enter 111, it will output:
1
1
1
but if you input 1111 it will output a whole lot of garbage. In other words: it works, end of story.
Hi face_master,
:-) did you ever try this code, so becouse you didn't
just for example
let declare an variable of the same type as the elements of the array - int - just before the declaration of the array[2]
....
int foo = 5;
int array[2];
...
and output that variable - foo - after your
cin >> array[2];
cout << foo;
these are all local variables allocated in the stack
when you modify an unallocated variable - in our case array[2]
you change the memory just above the array elements - the variable declared just before the array or if such variable didn't exist you override the return addres of the function and it will not exit normally.
damyan
Damyan is right on declaring arrays. Declaring
int array[2]; will declare a 2 element array, with values array[0] and array[1].
As for inputting cin >> array[2] to input the entire number into each array element, maybe, but I've never seen that. You usually use a loop to input multiple values. cin >> array[2] would input only into the specific variable array[2], which in this case doesn't exist. It may be compiler dependent, but this will give out of bounds errors in msvc.
If this is a homework question then you may not be able to use an array. Check this post for details on how to split the number up.
i'd write this program like this:
is this right? or is there some reason i shouldn't do this?Code:#include<iostream.h> #include<stdio.h> int main() { int x; char y[2]; cout<<"Enter a three digit number\n"; cin>>x; cout<<"\n"; sprintf(y, "%i", x); cout<<y[0]<<"\n"; cout<<y[1]<<"\n"; cout<<y[2]<<"\n"; return 0; }
Sometimes, the farthest point from the center is the center itself.
Your life is your canvas, it's only as beautiful as you paint it.
Probably (if you add an extra charater to the array), but you can do it without the function call and array -is this right?
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Enter 3 digit number: ";
cin>>a;
for(int i=100;i>0;i/=10)
{
cout <<a/i << " ";
a%=i;
}
return 0;
}