In the dark

Jan 20, 2008 at 2:52am
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!
Jan 20, 2008 at 8:02am
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

Jan 20, 2008 at 11:50pm
Right you use arrays. AND for loops. If you don't konw what that is, look it up in the C++ Tutorial Section.
Jan 26, 2008 at 11:37pm
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 Jan 26, 2008 at 11:39pm
Jan 27, 2008 at 4:22am
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
 }
}
Jan 27, 2008 at 6:26am
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 Jan 27, 2008 at 6:29am
Topic archived. No new replies allowed.