I am a puzzled regarding the purpose of the countDown = coundDown -1; part of this program code. I mean I do not know why I should subtract the countDown by 1. What is it for??? Here is the program source code, the one highlighted is the one I'm confused with.


1 #include <iostream>
2 using namespace std;
3 int main( )
4 {
5 int countDown;
6 cout << "How many greetings do you want? ";
7 cin >> countDown;
8 do
9 {
10 cout << "Hello ";
11 countDown = countDown - 1;
12 }while (countDown > 0);
13 cout << endl;
14 cout << "That’s all!\n";
15 return 0;
16 }

FrancisLazo
Deleted Member

countDown is used to limit the number of times the loop runs. Notice the while statement condition right after that line. If you never subtracted from countDown, that statement would always be true (unless the user entered a negative number in line 7) and the loop would run forever.

Comments
Answers the question
Not specific enough.

countDown is used to limit the number of times the loop runs. Notice the while statement condition right after that line. If you never subtracted from countDown, that statement would always be true (unless the user entered a negative number in line 7) and the loop would run forever.

It's a loop; it iterates(keeps going)until it reaches a desired goal. By using a variable name "countDown" = "countDown" - 1 explicity gives information to decrement the current value of the variable "countDown" by 1 minus its self, which is the current value the variable is holding.

Loops iterate, and increment and decrement methods, such as variable = variable - 1 or variable++(for increment, adding)or variable--(for decrement, subtracting).

The point of iterating(to keep going)is to keep changing the variables' values until they reach a certain point in which the loop will end or you can break from it and the result will have been completed(the result being whatever you wish it to be). This, of course, applying to a loop with a certain value that must be reached and not other tasks that can be done in a loop.

Hope I was some what useful.

Edited 5 Years Ago by spoonlicker: n/a

This is code for the game Yahtzee but it shows a good do-while loop. Well, I think anyway.

do
	{
	    cout << "Roll Again?? (y/n) ";
	    cin >> again;
		cout << endl;
		if (again != 'y')
	    {
			cout << "Next player's turn!!" << endl;
			cout << "_______________________________________________________________________" << endl;
			cout << endl;
		}
		else if (again == 'y')
		{
		    cout << "How Many Die do you want to Roll Again? (1-5) ";
		    cin >> roll;
			cout << endl;
		    for (int i = 0; i < roll; ++i) // Tells the player his results from his dice roll
			{
				iRoll = (rand() % 6);
				cout << "You rolled a " << Roll[iRoll] << endl; // Tells him what he got on the 1st roll
				rolldie.push_back(roll);
			}	
			cout << endl;
		}
	}while (again == 'y');

This is code for the game Yahtzee but it shows a good do-while loop. Well, I think anyway.

do
	{
	    cout << "Roll Again?? (y/n) ";
	    cin >> again;
		cout << endl;
		if (again != 'y')
	    {
			cout << "Next player's turn!!" << endl;
			cout << "_______________________________________________________________________" << endl;
			cout << endl;
		}
		else if (again == 'y')
		{
		    cout << "How Many Die do you want to Roll Again? (1-5) ";
		    cin >> roll;
			cout << endl;
		    for (int i = 0; i < roll; ++i) // Tells the player his results from his dice roll
			{
				iRoll = (rand() % 6);
				cout << "You rolled a " << Roll[iRoll] << endl; // Tells him what he got on the 1st roll
				rolldie.push_back(roll);
			}	
			cout << endl;
		}
	}while (again == 'y');

That's a...um...pretty bad example there. Mine was much better.

How come this doesn't work for my assignment? This is my only flaw. 2010 says so.

cout << "What numbers do you want to keep on this roll? ";
	cin >> number;

	for ( int i = 0; i < 7; i++) // Yahtzee... (-_-")
	{
		cout << rolldie.push_back(number);
	}

What's your example??

Sorry, it's in another thread. Click my profile and you can see it in my previous threads and compare it if you'd like.

This article has been dead for over six months. Start a new discussion instead.