Problem!!

Here is the problem

Write a program that prompts the user for a positive integer n = didi-1...d1 and then displays in one column each of the digits in the number n. The rightmost digit, d1, should be listed at the top of the column.

Example:

Enter a positive number: 3704

The digits of 3704 are:

4

0

7

3

Hint: The value of the first digit can be computed by using

digit = n % 10;

Next, continue on the number 370, obtained by dividing 3704 to 10.


and here is my solution: ( it did not work :'[ )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
	int number, _new, result;

	cout<<"Enter positive number: ";
	cin>>number;

	result = number % 10;
	cout<<result<<endl;

	do
	{
		_new = number / 10;
		result = _new % 10;
		cout<<result<<endl;
	}while(_new >= 0);
	
	system("pause");
	return 0;


I need some help with please...
Last edited on
Help you want, Help you get.
First replace all occurings of _new to number. (Delete the declaration though)
In the while condition rewrite the >=0 to >0.

And your problem is solved.
What were the mistakes? I'll leave it to you to figure it out. And there is one more problem. I'll leave it to you too. Enjoy!!
Your program as posted above doesn't even compile. You need to fix that first.

I'd agree that you probably want to rename your _new variable. Beginning application symbols with an underbar is not good form, and may result in symbol collisions.

You don't need three variables for this problem; just the number input by the user, and the digit you wish to print.
thx, but if you mean like this

}while((number >=0) && (number >0));

its gonna be an infinite loop :S
Your test for number doesn't have to be ">="...it can be ">". Technically, this would result in the printing of any leading zeroes in the number, so that might be a flaw.

If so, you can correct it by having an accumulator that reconstructs the original number, and checking against that to see if you're done. You'll have to save the original number in a different variable, so you will need more than two variables for this solution.
it keeps giving me an infinite loop even if it did not, it would not work i am confused i do not know what or where is the problem!! :S

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
	int number, digit;

	cout<<"Enter positive number: ";
	cin>>number;
	do
	{
		digit = number % 10;
		cout<<digit<<endl;

		number = number / 10;
		digit = number % 10;
		cout<<digit<<endl;

	}while(digit >=0);
	
	system("pause");
	return 0;
mzimmers
hmm
how do i save it? what type of declaration should i use?
You have excessive code in your do loop. You don't need two couts, nor do you need to calculate digit twice.

And, if you replace the ">=" with ">" it should work for all numbers without leading zeroes.

Edit: after looking at it some more, you can't solve the leading zeroes issue in the manner I suggested above. Once the input line is interpreted as an int, the leading zeroes are gone.

If your instructor wants you to account for leading zeroes, you'll have to read it in as a string, at which point the program gets a bit more complex.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
	int number, digit;

	cout<<"Enter positive number: ";
	cin>>number;

	digit = number % 10;
	cout<< digit<<endl;

	do
	{
		number = number / 10;
		digit = number % 10;
		cout<<digit<<endl;
	}while(number >0);
	
	system("pause");
	return 0;


it worked 8) Thx alot man 8D
Glad to help. Reducing the number of variables usually simplifies an algorithm, and simple is good in coding.

I still don't see a closing brace on your program, though. I assume you're just not including it, since it shouldn't compile without one.
its there ;) but every time i copy it i forget to include it in XD
@Eyad
So, you got it working eh!
And have you figured out the next problem with your code??
Last edited on
Topic archived. No new replies allowed.