Input Number Output Digits

This is my code so far I'm not entirely sure how to get this to work any and all help is appreciated. The objective is to take a positive integer and display each digit on a separate line.

#include <iostream>
using namespace std;
int main()
{
int num, n;
int sum = 0;
int rev = 0;
cout<<"enter an integer: ";
cin>>num;
if (num<0)
cout<<"enter an integer: "
cin>>num;
while (num > 0){
rev*=10;
rev+=(num % 10);
num/=10;
}
cout<<"The digits are: ";
while (rev!=0){
n= rev % 10;
rev/=10;
cout << n << " ";
}
}

system("pause");
return 0;
}
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
#include <iostream>
using namespace std;
int main()
{
int num, n;
int sum = 0;
int rev = 0;
cout<<"enter an integer: ";
cin>>num;
if (num<0)
cout<<"enter an integer: "
cin>>num; 
while (num > 0){
rev*=10;
rev+=(num % 10);
num/=10;
}
cout<<"The digits are: ";
while (rev!=0){
n= rev % 10;
rev/=10;
cout << n << " ";
}
}

system("pause");
return 0; 
}
Last edited on
I just copy and paste your code to highlight the errors in your program with the line number, which is easier.

the if condition in line 10 doesnt have curly braces. If you dont have curly braces, then you need input the integer two times.

so change it to
1
2
3
4
5
if (num<0)
{
cout<<"enter an integer: "
cin>>num; 
}


I suggest to use while loop for this error check, so that you can check the input until the user enters positive integer. To do that just replace the if with while

Another error I found is at line 24. The curly brace at this line will end your main and the remaining statements 26,27,28 are left alone. so just delete the curly brace at line 24.

Now everything works

Thank you so much that really helped.
Topic archived. No new replies allowed.