Let's see if I can help you through it (hopefully you are looking at the same code you posted last).
Code:
int main()
{
int data;
int number;
ofstream out_file;
cout << "This program is designed to record up to 50 scores between the values of 0 and";
cout << " 100. The scores will then be stored in another file. Please enter all of";
cout << " the needed scores and type -1337 to end the program and store the values.";
cout << endl << endl;
number = 1;
out_file.open("stat_data");
Set everything up, output description and two blank lines, open a file called "stat_data" in the current directory for writing.
Output:
This program is designed to record up to 50 scores between the values of 0 and 100. The scores will then be stored in another file. Please enter all of the needed scores and type -1337 to end the program and store the values.
Code:
while (data != SENTINEL)
Does data equal SENTINEL? Well, data was not initialized yet, so it could be anything. This is not your problem, but you should initialize data to some number other than the SENTINEL just in case. For now, we'll assume you are lucky and data does not equal SENTINEL, so we enter this loop.
Code:
{
number = number++;
number is incremented. Of course, number wasn't initialized either, so I assume you are getting lucky and your compiler is initializing to 0 for you. You shouldn't count on that and instead you should initialize the ints to something at the beginning (like 0).
Code:
cout << "Enter an integer (1337 to end input):\n";
cout << number << ". ";
Output an instruction, the value of number, and a period.
Output:
This program is designed to record up to 50 scores between the values of 0 and 100. The scores will then be stored in another file. Please enter all of the needed scores and type -1337 to end the program and store the values.
Enter an integer (1337 to end input):
1. Wait for user input. Assume you are the user and you type 87 and Enter. Then data is filled with the number 87.
Output:
This program is designed to record up to 50 scores between the values of 0 and 100. The scores will then be stored in another file. Please enter all of the needed scores and type -1337 to end the program and store the values.
Enter an integer (1337 to end input):
1. 87
Code:
while (data < 1 || data > 1000)
Since data is 87, it is not less than 1 or greater than 1000, so the expression is false. The block of code will not be executed at all.
Code:
{
cout << "\nThe number entered was not within the parameters. \n\nPlease try again:\n";
cin >> data;
}
This is not executed because the while above evaluated to false.
Code:
while (data != SENTINEL)
Since 87 does not equal SENTINEL (1337), this expression evaluates to true, so the code inside this while loop is executed.
Code:
{
out_file << data << " ";
output the data (87) to the file.
Code:
cout << "Enter an integer (1337 to end input): ";
Request more data from the user.
Output:
This program is designed to record up to 50 scores between the values of 0 and 100. The scores will then be stored in another file. Please enter all of the needed scores and type -1337 to end the program and store the values.
Enter an integer (1337 to end input):
1. 87
Enter an integer (1337 to end input): Wait for user input. Assume you are the user and you type 90 and Enter. Then data is filled with the number 90.
Output:
This program is designed to record up to 50 scores between the values of 0 and 100. The scores will then be stored in another file. Please enter all of the needed scores and type -1337 to end the program and store the values.
Enter an integer (1337 to end input):
1. 87
Enter an integer (1337 to end input): 90Continue back up to the top of the while loop.
Code:
while (data != SENTINEL)
Since 90 does not equal SENTINEL (1337), this expression evaluates to true, so the code inside this while loop is executed.
Code:
{
out_file << data << " ";
output the data (90) to the file.
Code:
cout << "Enter an integer (1337 to end input): ";
Request more data from the user.
Output:
This program is designed to record up to 50 scores between the values of 0 and 100. The scores will then be stored in another file. Please enter all of the needed scores and type -1337 to end the program and store the values.
Enter an integer (1337 to end input):
1. 87
Enter an integer (1337 to end input): 90
Enter an integer (1337 to end input): Wait for user input. Assume you are the user and you type 5000 and Enter. Then data is filled with the number 5000.
Output:
This program is designed to record up to 50 scores between the values of 0 and 100. The scores will then be stored in another file. Please enter all of the needed scores and type -1337 to end the program and store the values.
Enter an integer (1337 to end input):
1. 87
Enter an integer (1337 to end input): 90
Enter an integer (1337 to end input): 5000Continue back up to the top of the while loop.
Can you see how it is stuck in this while loop until you hit the sentinel, and how it never displays the number anymore?