In the dark

How would I write a program where the user enters a four digit number, but the answer is that each digit is on a separate line. Like this;

8
4
9
3
Any help would be great!
I'll give you the cryptic answer first....

Imagine you had to write a program that took a four letter word and displayed each letter on a separate line. For instance, a program that takes the word "mice" and displays it;

m
i
c
e

Question is; how would you do this? (hint - arrays are your friend).

Once you answer THAT question, remember that the computer doesn't CARE what WE think the characters mean. To the computer, turning "mice" into;

m
i
c
e

and turning "1234" into

1
2
3
4

is one and the same thing.

Hope this helps get the ol' brain started in the right direction -- let me know if not :)

Regards
muzhogg

Right you use arrays. AND for loops. If you don't konw what that is, look it up in the C++ Tutorial Section.
hint to think about ;)
* escape chars in either printf() or cout functions

I would GIVE you the code to this, however, that would not better you as s programmer. Do as SirCapsAlot said and go to the C++ Tutorial section for control structures and arrays. This should be more than enough to solve this problem, without giving you the answer explicitly.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include<iostream.h>
void main()
{
 int x,r;
 cout<<"enter a no"; 
 cin>>x;
 while(x>0)
 {
   a=x%10;//it gets the last charecter
   cout<<a;
   a=x/10;//it removes the charecter displayed
 }
}
You can use arrays, or simply the strtok function.

http://www.cplusplus.com/reference/clibrary/cstring/strtok.html

However, I'm not sure if it works with integers. But with strings.
Last edited on
Topic archived. No new replies allowed.