Outputting on multiple lines

I wrote a program that prompts the user to enter a four digit positive integer. What I need is the program to output the four digit integer one digit on one line at a time. For example if the output is 4444:
4
4
4
4

any help would be appreciated.

Last edited on
cout << endl; or cout << '\n'; will make the output go to a new line,

cout << 5 << endl << 6 << '\n' << 7;
Will produce
5
6
7
Last edited on
C:
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h> // C standard input/output library
int main()
{
 printf("Please enter a four digit number: "); // prompt the user to enter a number
 char input[4]; // create a new array of characters
 scanf("%s",&input); // get four numbers into input
 printf("%c\n%c\n%c\n%c\n",input[0],input[1],input[2],input[3]); // output each character on a different line
 fflush(stdin); // flush stdin
 getchar(); // wait for user to press enter, so they see the output
 return 0; // exit program
}


C++:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream> // C++ input/output library
using namespace std; // std namespace functions being used
int main()
{
 cout<<"Please enter a four digit number: "; // prompt the user to enter a number
 char input[4]; // create a new array of characters
 cin>>input; // get four numbers into input
 cout<<input[0]<<endl<<input[1]<<endl<<input[2]<<endl<<input[3]; // output each character on a different line
 fflush(stdin); // flush stdin
 cin.get(); // wait for user to press etner, so they see the output
 return 0; // exit program
}


Hope this helps, need any more help, just ask.
Last edited on
wow, thanks guys
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

int main( int argc, char * args[] )
{
    string number;
    cin >> number;
    copy( number.begin(), number.end(),
          ostream_iterator<char>( cout, "\n" ) );
    return 0;
}
To print out a 4 digit number using an integer data type only you can use integer division.

First divide the number by 1000 and store the result. Then subtract the amount from the number.

Then divide the number by 100 and store the result. Subtract that from the number.

Divide the number by 10 and store.... and on and on.

btw: I have heard that question somewhere before. Is that from Stroustrup's book?
maliks book
Topic archived. No new replies allowed.