Writing 2 programs. ...\n.....output in seconds...

Pages: 12
Alright, then. I'll give you the full tutorial:

'\n'
This is an escape character that represents a newline. It's actual binary value depends on the system and on the output device. It causes the writing cursor to go down one line, then move to the star of the line.

std::cout
This is a function defined in the IOstreams library that serves the same purpose as '\n'. ALSO, it flushes the output buffer, but this is not relevant, right now.

The code you have there is unnecesarily complex.

Integral division by 10 and powers of 10:
What this does is drop the least significant digit in a number. 1234/10==123, because 1234.0/10.0==123.4; when the number is trucated, the result is 123.
Also, 1234/100==12

Modulo operation by 10 and powers of 10:
The modulo operation is defined as the remainder of the integral division between two numbers. x/y+x%y==x
Modulo operation by 10 drops all digits more significant than 0. 1234%10==4 (<-- Ignore my babbling. This is what matters right now.)

Now, about the input. What we have here is a problem in interpretation of instructions. "Input a four digits natural" is different from "input four digits". In the first case you have the user input a number, check that it's >=1000 and <=9999, and if it isn't you have him input it again. In the second the user just inputs four numbers which must be >=0 and <=9.

Output format is not important in this case. It doesn't really matter if you print "1\n2\n3\n4", "1 2 3 4", or "1234". What's important is that you perform the computations necessary to split the integer into separate decimal digits. Don't concern yourself with "oh, but it doesn't have '\n', so it can't be right". Output format, unless otherwise stated, does not matter.
If I understand correctly, you just want the user to input all the digits of the number individually. If this is the case, I would use the following code:
#include <iostream>

using namespace std;

main() {
int dig1;
int dig2;
int dig3;
int dig4;

cout <<"Please enter four digits";
cout <<"\nFirst : ";
cin >> dig1;
cout <<"\nSecond : ";
cin >> dig2;
cout <<"\nThird : ";
cin >> dig3;
cout <<"\nFourth : ";
cin >> dig4;

cout << "\n" << dig1 << "\n" << dig2 << "\n" << dig3 << "\n" << dig4;

return 0;

}
spy4hearts: Please refrain from posting before reading the whole thread. This is already messy enough.
I have read all post and haven't seen anyone saying you cant use strings or a for loop so I would this...

The number the user inputs put it on a string and then access them with a for loop like...

for (int n=0; n<4; n++) {
cout << num[n] << endl;
}

Hopes this help
That's not the point of the exercise, and that's not what the problem asks.
Write a program that prompts the user to input a four-digit positive integer.
Topic archived. No new replies allowed.
Pages: 12