Code Modification

closed account (o1pz6Up4)
I wrote a roll dice program for a C++ class, and it works perfectly when run. However, my professor is asking me to do the following modification:

"Please move the modification for your loop control variable
counter ++;
into the while loop test expression."

I can't seem to understand exactly what my professor is asking of me here. Below is my driver code. I would appreciate if anyone can help me understand what I'm supposed to change.

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
int main()
{
	srand(time(NULL));  //seed random number generator
	int count = 1;      //counter for while loop
	int first, second;  //store dice values
					    //to roll 10 times

	while (count <= 10)
	{
		rollDice(first, second); //get values
		display("Roll Results", first, second);
			
		//if first less than second
		//swap then display values
		if (first < second)
		{
			//swap values and display
			swapValues(first, second);
			display("Swapped", first, second);
		}

		cout << endl;
		count++;
	}

	system("pause");
	return 0;
}
Last edited on
Your count variable is on line 23, while your while loop is on line 8. Since your count variable gets incremented at the end of the while loop, that's equivalent to adding a post-increment operator within the while loop condition itself.

Change to:
while (count++ <= 10)
And delete line 23.

(Note that I am neither agreeing nor disagreeing with your professor's design. Just showing you what he means.)
Last edited on
The loop control variable (the variable used in the while-loop condition) is called count.
Your professor is asking you to modify that variable in the condition itself:
1
2
3
4
5
int count = 0; 
while (count++ < 10) {
  int first, second; 
  // ... 
}
closed account (o1pz6Up4)
Okay, I understand now. Thank you both for the great explanations! :)
Topic archived. No new replies allowed.