In the dark

How can I write a program where the user enters a four digit number: ex (5321) and have the answer read one digit per line. What I'm trying to say is that the program has to show the numbers like this;
5
3
2
1
you just have to output it with a newline or endline.. =) like this:
1
2
3
cout << "The numbers are: \n\n";
cout <<num1 <<"\n" << num2 <<"\n" <<num3 << "\n"
     << num4  << endl;
Last edited on
in that way I did used the newline.. hope it did help you..
You can convert first the number in string and then print it byte by byte this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream.h>

// this function converts long to string
string tostring(long n) {
	char aux[20];
	itoa(n, aux, 10);		// this C function converts long to char[] (10 specifies decimal base)
	return (string) aux;	// this casts char[] to string
}

void main() {
	long n;
	string s;

	cout << "number:";
	cin >> n;

	s = tostring(n);

	cout << "number by columns: " << endl;
	for(unsigned int i = 0; i < s.length(); i++)
		cout << s[i] << endl;
}
Thank you.
mmmmmmmmmmm y should you convert it alex79roma? if you can just simply cout it?
You should input it as a string, then loop through it and (in much the same way Alex did) loop through it byte-by-byte and output it with newlines in between. There's no point in inputting it to a long if you know it's going to be 4 chars long.

EDIT: Alex, he meant converting it from a long to a string.
Last edited on
I only tried to give the most general solution AzraelUK but naturally you can do an ad-hoc solution for 4 digit numbers.

ried_azmel i don't understand whitch part of my code you mean
HI ALL ! i think all here are newbies and need not only some help but the most important is to benefit and learn, i appreciat what did. look boat26guy , i know that u r new in programing and it is not a problem but u should depend on urself in most cases as beginner, i mean the way u ask for help is somehow wrong( sorry for this) if u got a program to solve, solve it first in ur way then if u got any difficult ask for help this way i garanty ur to improv so fast in programing, but if u depend on the other code then do urs :(.....
so as a friend i just wanna advice to try to solve the code first then if u face any problem we can discus it or show us what u did, this way u ll realy undrestand and compare the ohter code and ur so u ll see what are u mistake.
sorry for this but just an advice as a friend hope no feeling hurt ;).
:).

loook first if u have a program try to think about it, forget that it is a progarm and try to solve it in simple way, i mean their is something call ALGORITHM if u realy wanna learning programing,
ur program is so simple
in this program thier are only 4-digits to be display 1per line so u should be careful here and u want to display each digits per line, first think how to get the digits how to separat them and, their is difference btwn 4-digits and 4 numbers...4-digit ==1234 (the 4 number should not be separeted) ,but 4 number == 1 2 3 4
i ll help u in this code but promess me next time to solve and try first befor ask ok :).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespace std;
int main()
{

int num ,n1, n2, n3, n4, remainder;

cout<<"Enter the four-integer digit   ";
cin>> num;

if (num>9999 || num <1000)
cout<<"sorry enter 4 integer digit please "<<endl;
else
{
n1 = num / 1000;
remainder = num % 1000;
n2 = remainder / 100;
remainder = remainder % 100;
n3 = remainder /10;
remainder = remainder %10;
n4 = remainder;

cout<<"The 4-digit u enter are  "<<num<<endl;
cout<<"The 4-digit each per line \n";
cout<<n1 <<endl;
cout<<n2 <<endl;
cout<<n3 <<endl;
cout<<n4 <<endl;
}
return 0;


i will not explain anything to see if u realy undrestand or no just to show that u depend more on urself. look im doing this jus to help u being good enough so hope that u ll not misundrestand me ;).

i ll be more happy if u ask for any detail or explantion about the things u didnt undrestand in my code :)

hope i was helpful :).
Last edited on
Kia, that's completely the wrong way of doing it. What if you enter 0999?

The better way to do it would be to input it as a char[] (or a string) and then loop through the characters. If you need to access these characters individually, then add '#include <cstdlib>' and then use the atoi() function to convert them to integers.

1
2
3
4
5
6
7
8
9
10
#include <iostream>

using namespace std;

int main () {
	cout << "x = ";
	char x [5];
	cin >> x;
	for(int i = 0; i <= strlen(x); i++) cout << x[i] << endl;
}
i think AzzaelUK means that you'll use a string object.. hehehe
Last edited on
try this.. :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main ()
{

    string fourDig;

    cout << "Enter a 4 digit number: ";
    getline (cin, fourDig);




    cout << "The numbers are: \n\n";
    for (int i=0; i < 4; i++)
    {
        cout << fourDig[i] <<endl;
    }
    return 0;

}
Last edited on
Here's a recursive solution that works for numbers (not strings).
It works for all positive numbers, but not for n==0. (It would have
to be made a little ugly to work for n==0.)
1
2
3
4
5
6
void print_vertically(int n)
{
    if (n == 0) return;
    print_vertically(n / 10); // recurse on all but rightmost digit
    cout << n % 10 << endl;   // print rightmost digit
}
Topic archived. No new replies allowed.