I wrote this before the edit, so refer to your original code to see the differences. I added comments to explain what I did.
One of the problems is the lack of {}
around the code after the if
statements. Without them only the first statement after the if
is conditional.
if(newLocation == 0)
cout << "\nPerson zero has the ball\n"; // Executes based on the condition
t0 = true; // executes always
Some languages, like Python, use indenting to match things up but C++ does not, so you need braces. I recommend getting into the habit of always using them even when there is only a single line after the conditional. IMO it makes the code easier to read, shows explicit intent, and will never turn into a bug when you add another line that should be conditional but isn’t because you didn’t have the braces.
The other changes are pretty simple. I cleared all of the boolean variables to false before assigning the correct one to true and I used %
(mod) to wrap the value around when it is increased past the end.
I don’t use using namespace std;
so I added the explicit std::
prefix where needed. I recommend you get in that habit too. It may seem harmless and save you some typing, but someday you’ll regret it, especially if you ever put it in a header.
I’m hoping that your next assignment teaches you how to do this using an array for the variables since it would be so much cleaner and simpler.
#include <iostream>
int main()
{
bool t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
int num1 = 0, num2 = 0;
// loop forever. The loop will exit when a negative number is entered.
for(;;)
{
// Set all of the variables to false
t0 = t1 = t2 = t3 = t4 = t5 = t6 = t7 = t8 = t9 = false;
std::cout << "Enter a number:\n";
std::cin >> num1;
// negative number ends the game
if (num1 < 0)
{
std::cout << "Mom says dinner is ready!\n";
break; // exit loop
}
// advance the ball
// add the new value and use % to wrap it around
num2 += num1;
num2 %= 10;
if (num2 == 0)
{
std::cout << "\nPerson zero has the ball\n";
t0 = true;
}
if (num2 == 1)
{
std::cout << "\nPerson one has the ball\n";
t1 = true;
}
if (num2 == 2)
{
std::cout << "\nPerson two has the ball\n";
t2 = true;
}
if (num2 == 3)
{
std::cout << "\nPerson three has the ball\n";
t3 = true;
}
if (num2 == 4)
{
std::cout << "\nPerson four has the ball\n";
t4 = true;
}
if (num2 == 5)
{
std::cout << "\nPerson five has the ball\n";
t5 = true;
}
if (num2 == 6)
{
std::cout << "\nPerson six has the ball\n";
t6 = true;
}
if (num2 == 7)
{
std::cout << "\nPerson seven has the ball\n";
t7 = true;
}
if (num2 == 8)
{
std::cout << "\nPerson eight has the ball\n";
t8 = true;
}
if (num2 == 9)
{
std::cout << "\nPerson nine has the ball\n";
t9 = true;
}
}
return 0;
}
It’s also possible to replace the if
statements with a switch
if you wanted to. For something like this it doesn’t really matter, but there are cases where it might be preferred. That would look like this:
switch (num2)
{
case 0:
std::cout << "\nPerson zero has the ball\n";
t0 = true;
break;
case 1:
std::cout << "\nPerson zero has the ball\n";
t0 = true;
break;
}
...
1
solved Program to track the passing of a ball in a circle of 10 people using boolean