Nested While Loop Runs Once More Than Expected - c++

I am new to C++ (have been using it for around 2 weeks), and right now I am trying to write a program that will roll a dice and give a random number between 1 and 6, then count how many rolls it takes to get a 6. After that, it will ask user if they want to continue with a positive and exit with 0.
If the input here is 0, then show the average rolls to get a 6 and the number of 6 occurrences that the average was based on, and all of this using do-while and/or while loops.
The problem is, whenever I press 0, the program seems to run the inner loop one more time before exiting and showing the averages.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
float number_rolls, number_sixes;
int x;
number_rolls = 0; // store the number of rolls made
number_sixes = 0; // store the number of sixes
x = 0;
srand(time(0));//initialise random num generator using time
int roll = 0;//declare a variable to keep store the random number
do
{
cout << "possitive to continue, 0 to exit" << endl;
cin >> x;
do
{
roll = rand() % 6 + 1; //generate a random number between 1 and 6
number_rolls = number_rolls++; // add the rolls to a counter
cout << roll << endl; // out put the roll values
} while (roll != 6);
number_sixes = number_sixes++; // adds the sixes to a counter
} while (x != 0);
cout << "average rolls to get a six: " << number_rolls / number_sixes << " you took; " << number_rolls << "rolls and got: " << number_sixes << " sixes" << endl;
Can somebody please help point me in the right direction?

After reading the value of x, you can compare it to 0 and break out of the loop immediately:
do {
cout << "possitive to continue, 0 to exit" << endl;
cin >> x;
// Add this check
if (x == 0)
break; // if this is reached, the inner `do-while` is not executed
do {
// ...
} while (roll != 6);
number_sixes = number_sixes++; // adds the sixes to a counter
} while (x != 0);
Now this makes the check in the outer while is unnecessary, and you can convert the do-while to a while loop with a true condition:
while (true) {
cout << "possitive to continue, 0 to exit" << endl;
cin >> x;
if (x == 0)
break; // if this is reached, the inner `do-while` is not executed
do {
// ...
} while (roll != 6);
number_sixes = number_sixes++; // adds the sixes to a counter
}

Related

Unexpected loop behaviour in c++

// cai.cpp (Computer Assisted Instruction)
// This program uses random number generation to assist students learn multiplication
#include <iostream>
#include <iomanip>
#include <cstdlib> // contains prototypes for srand and rand
#include <ctime>
#include <cctype>
using namespace std;
int main() {
int question();
string status;
int score{0};
cout << "\nThis program will present you with 10 multiplication problems\n"
<< "enter the correct answer after the prompt\n"
<< "Enter Y for YES and N for NO\n"
<< "Do you want to try a game?";
cin >> status;
while(status == "Y" || status == "y") {
for(int x{0}; x < 11; x++) {
question();
score = score + question();
}
// report total score
cout << "\nTotal score is " << score << " out of 10";
cout << "\nWould you like to play again?";
cin >> status;
if(status == "n" || status == "N") {
break;
}
}
cout << endl;
}
int question() {
string responses();
// use srand to generate the random nmber for the various problems
srand(static_cast<unsigned int> (time(0)));
int number1 = 1 + rand() % 12; // initialize random number
int number2 = 1 + rand() % 12; // initialize random number
int total = number1 * number2;
int response;
int score{0};
cout << "\nWhat is " << number1 << + " times " << + number2 << + " ?";
cin >> response;
while (response != total) { // while answer is wrong, repeat question and wait for response
cout << " \nThat is incorrect, try again: ";
cin >> response;
}
if ( response == total) {
cout << responses();
score++; // increment score after each correct answer
}
return score;
}
string responses() {
string res1 = "Well done, that is correct!\n";
string res2 = "Congratulations, that is very accurate!\n";
string res3 = "Wow!, I'm impressed\n";
string res4 = "You're doing great! Keep up the good work.\n";
srand(static_cast<unsigned int> (time(0)));
int select{1 + rand() % 4};
switch(select) {
case 1: return res1;
break;
case 2: return res2;
break;
case 3: return res3;
break;
case 4: return res4;
break;
default: return " ";
}
}
When I compile and run this program, I expect it to loop only 10 times but it loops more than 10 times, I'm thinking it has to do with the switch statement in the responses function but I do not understand why it should be causing a problem. Any explanation would be greatly appreciated. I have modified the while loop condition in the main function to loop different times but it always loops to display all the possible responses in the switch statement. Screen shot of results attached, I modified the while statement to loop only twice but I still had all my responses showing so it ended up looping 4 times.
expect it to loop only 10 times but it loops more than 10 times
In your loop:
for(int x{0}; x < 11; x++)
x goes from 0 to 10, so it loops 11 times.
A few things to note. Your for loop goes up to < 11, meaning 10. So x from 0 to 10 (inclusive) is actually 11 times. You need to change the condition to < 10, or change x to start at 1.
Second issue, inside the for loop, you are actually calling the question function twice; with the first call's result being ignored. This is why you are getting 4 questions, each answered correctly, but only a score of 2.
Third, not so much an issue, but a bit redundant. At the end of the while loop, you check status == "n" || status == "N", however, this is unnecessary, since the while loop's condition checks for y and Y already.
I've cleaned up your while loop a bit here:
while(status == "Y" || status == "y") {
for(int x = 0; x < 10; ++x) {
score += question();
}
// report total score
cout << "\nTotal score is " << score << " out of 10";
cout << "\nWould you like to play again?";
cin >> status;
}
Unrelated thing to also clean up. You don't need to call srand in the question() function, instead just call it once in main. Also, since this is C++, you can use newer random generators from the <random> header.

How to use a counter with a 'do while' loop in C++?

I am trying to get this 'do while' loop to run 3 times and then display the amount in the accumulator contain within a while loop inside the 'do while' loop.
It seems to be counting correctly, but only runs the while loop on the first. When run, instead of going on to ask for the next set of numbers, it just displays the first batch (added up correctly). I have tried switching some of the code around and searching google, but can't find the answer.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int storeNum = 1;
int payRollAmount = 0;
int totalPayroll = 0;
do
{
cout << "Store " << storeNum << ":" << endl;
while (payRollAmount <= -1)
{
cout << "Enter Store's Payroll Amount (-1 to exit): ";
cin >> payRollAmount;
totalPayroll += payRollAmount;
}
storeNum++;
} while (storeNum <= 3);
cout << "The Total Payroll is: " << totalPayroll << endl;
system("pause");
return 0;
}
The code should take in an unknown amount of "payrolls," allow you to exit using -1, and then continue on to the next stores payrolls. It should do this 3 times, and then display the total amount (all numbers entered added together.
Hi perhaps reset payRollAmount at each iteration? That way it will continue to request the input.
for (int amount = 0; amount != -1; ) {
cout << "Enter Store's Payroll Amount (-1 to exit): ";
cin >> amount;
totalPayroll += amount;
}

|| operator in while condition not working as expected

So I'm a new programmer and I decided to try and make this game. It is a basic, let's say person v. person game. You enter the amount of each team, in my case Ninjas and Samurai, and then it randomizes the attack chance of each and outputs the winner. Every time I run the program I input the number of each type, and I always get an output of the Ninjas having 0 health, and the Samurai having negative health. How would I be able to have the while loop end when one team gets to 0 health? I've tried using totalNinjaHealth != 0 || totalSamuraiHealth != 0 but the program runs infinitely.
#include <iostream>
#include <random>
#include <string>
#include <ctime>
using namespace std;
int main() {
int ninjas;
int NINJA_HEALTH = 2;
int NINJA_ATTACK = 2;
int samurai;
int SAMURAI_HEALTH = 3;
int SAMURAI_ATTACK = 1;
default_random_engine randomGen(time(NULL));
uniform_real_distribution<float> attackChance(0.0f, 1.0f);
cout << " *** Ninjas V Samurai *** " << endl;
cout << "Input amount of Ninjas" << endl;
cin >> ninjas;
cout << "Input amount of Samurai" << endl;
cin >> samurai;
int totalNinjaHealth = NINJA_HEALTH * ninjas;
int totalSamuraiHealth = SAMURAI_HEALTH * samurai;
cout << totalNinjaHealth << endl;
while (totalNinjaHealth > 0 == true || totalSamuraiHealth > 0 == true)
{
if (attackChance(randomGen) > 0.5f) {
totalSamuraiHealth -= NINJA_ATTACK;
cout << totalSamuraiHealth << endl;
}
else if(attackChance(randomGen) < 0.5f) {
totalNinjaHealth -= SAMURAI_ATTACK;
cout << totalNinjaHealth << endl;
}
}
if (totalNinjaHealth == 0) {
cout << "Ninjas lost all " << ninjas << " Ninjas. Samurai remaining " << totalSamuraiHealth << endl;
}
else if (totalSamuraiHealth == 0)
{
cout << "Samurai lost all " << ninjas << " Samurai. Ninjas that remain " << totalNinjaHealth / 2 << endl;
}
cin.get();
cin.get();
return 0;
}
Screenshot of what happens when ran:ConsoleWhenRan
Am I using the || operator incorrectly? I thought the || operator waits until one condition is true and then stops, but when I run the code It seems to wait until both either pass, or equal 0 giving the negative output.
You need to use the && operator. The || is the "or" operator and will evaluate to true when either one or both of the conditions are true. In your case, the while loop will continue to evaluate as long as one of your teams has health > 0. The "and" operator (&&) requires both conditions to be true for the statement to be evaluated as true.
What happens is:
You run the loop until either of the teams has positive total health. So the total health is for both of the teams is less/equal to zero.
Then you check the healths and you get your answer. It just so happens that in your case one of the teams died with exactly 0 health. In some cases though, the output should show neither the messages.

C++ simple game using a Loop program

This program will play a game with the user, called Odds and Evens. The computer will play Evens, and the human user will play Odds. For a round of the game, each player picks an integer in the range [1,10]. The players pick their numbers independently: neither player knows the other player's number before choosing its own number. If the sum of the numbers is even, then Evens (the computer) wins that round; if the sum of the numbers is odd, then Odds (the human) wins that round. The game continues for as many rounds as the user want to play; the user ends the game by typing a non-# or a number outside [1,10] for the input. At the end of the game, the program summarizes the score.
I am having trouble properly looping this question. Randomizing the number pc chooses is not working as every round in the game the pc chooses the same number. Also i do not know how I would have the program summarize the score. Help would be much appreciated as I have another problem for homework that is similar to this!
Here is my code:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <ctime>
using namespace std;
bool die(const string & msg);
int main(){
srand(static_cast<unsigned>(time(0)));
unsigned num1 = 0, num = 0, sum = 0;
bool userTurn = true;
cout << "Welcome to the Odds and Evens game!";
num = rand() % 10 + 1;
while (num){
if (userTurn){
cout << " Your # in [1,10] is ";
cin >> num1;
}
else {
cout << "My number is " << num;
sum = num1 + num;
if (sum % 2 == 0){
cout << " I win!";
}
else {
cout << " You win!";
}
}
userTurn = !userTurn;
}
}
bool die(const string & msg){
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
Randomizing the number pc chooses is not working as every round in the game the pc chooses the same number.
You don't have code to re-set the value of num when it's the computer's turn.
After the line
userTurn = !userTurn;
add
if ( !userTurn )
{
num = rand() % 10 + 1;
}
Also i do not know how I would have the program summarize the score.
Keep two counters that indicate how many times the human won and how many times the computer won.
int computerWinCount = 0;
int humanWinCount = 0;
and then, update the loop to use:
if (sum % 2 == 0){
cout << " I win!";
++computerWinCount;
}
else {
cout << " You win!";
++humanWinCount;
}
The conditional of the while loop is such that your program will never terminate. Update it to something like below.
while (true) {
if (userTurn){
cout << " Your # in [1,10] is ";
cin >> num1;
// If the user entered a number that is not
// within range or the user did not input a number,
// then break out of the loop.
if ( !cin || num1 < 1 || num1 > 10 )
{
break;
}
}
else {
cout << "My number is " << num;
sum = num1 + num;
if (sum % 2 == 0){
cout << " I win!" << endl;
++computerWinCount;
}
else {
cout << " You win!" << endl;
++humanWinCount;
}
}
userTurn = !userTurn;
if ( !userTurn )
{
num = rand() % 10 + 1;
}
}
To report the summary, add the following lines before the end of the main.
cout << "Number of times I won: " << computerWinCount << endl;
cout << "Number of times you won: " << humanWinCount << endl;
Here:
num = rand() % 10 + 1;
while (num){
... // never change num
}
Do you see the problem? The computer player chooses num randomly, but only once. Just put another num = rand() % 10 + 1; inside the main loop.
(Also, you don't seem to have a way for the user to terminate the game.)
So you want a simple loop that will do the following things.
get the user input.
get the computer input
check to see who win's the current round
update scores.
this happens until the user chooses an option not from 1 to 10
after this you want to display the score.
Here is a complete example.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
int mynum, compNum, myScore(0), compScore(0);
srand(time(NULL));
cout << "Welcome to the Odds and Evens game!" << endl;
cout << "Your # in [1,10] is ";
while ((cin >> mynum) && mynum > 0 && mynum <= 10){
compNum = rand()%10 + 1;
if ((mynum + compNum)%2){
cout << "You win" << endl;
++myScore;
} else {
cout << "Computer Wins" << endl;
++compScore;
}
cout << "Your # in [1,10] is ";
}
cout << "You won " << myScore << " games" << endl;
cout << "The computer won " << compScore << " games" << endl;
return 0;
}
Your problem with the computer's number not changing is due to the fact you do not update its value within the loop.
If you want to keep track of the score, you can simply keep two integers that keep track of how many times the user has won and how many times the computer has won. Then at the end (after the while loop) cout each of their scores.
Overall your code is pretty close.
You just need to make sure you update the computer's guess inside the while loop and when you decide who's won the round increment that person's score.
The whole loop condition in your original code will always evaluate to true. As num will always be to a number 1 to 10. You'll want to use the user's input in the while loop condition.
The while condition in my code will do the following:
get the user's input. cin >> mynum will evaluate to false if cin fails to read a number. If it did read a number the condition will check to see if the number is between 1 and 10 inclusive.

How could I exit from do-while loop?

I have these block of codes that belong to a NIM subtraction game. The thing that I would like to implement is that user is going to be able play the game as long as he/she wants. Simply if user enters 999 program will exit, otherwise user will be playing until he/she enters 999. Here is my block of codes. I am not sure that I make a logical mistake or I need to add some specific exit code. Thanks for your time and attention.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int total, n;
while(true){
cout << "Welcome to NIM. \nEnter 999 to quit the game!\nPick a starting total: ";
cin >> total;
if(total==999)
break;
while(true){
//pick best response and print results.
if ((total % 3) == 2)
{
total = total - 2;
cout << "I am subtracting 2." << endl;
}
else
{
total--;
cout << "I am subtracting 1." << endl;
}
cout << "New total is " << total << endl;
if (total == 0)
{
cout << "I win!" << endl;
break;
}
// Get user’s response; must be 1 or 2.
cout << "Enter num to subtract (1 or 2): ";
cin >> n;
while (n < 1 || n > 2)
{
cout << "Input must be 1 or 2." << endl;
cout << "Re-enter: ";
cin >> n;
}
total = total - n;
cout << "New total is " << total << endl;
if (total == 0)
{
cout << "You win!" << endl;
break;
}
}
}
return 0;
}
You are modifying total inside the loop. Just test after cin>>total at the beginning if total==999 and break if true, i.e.
if(total==999)
break;
and replace the do-while loop by a while(true){}
In the do-while loop you are trying to compare character literal '999' with variable total that has type int.
}while(total!='999');
Though this code is valid its result can be something else than you are expecting. Values of character literals with more than one symbol are implementation defined.
You have to write
} while ( total != 999 );
Also if the player will enter 999 you start to play with him though you have to exit the game.
So in my opinion it is better to use while loop. For example
while ( true )
{
cout << "Welcome to NIM. \nEnter 999 to quit the game!\nPick a starting total: ";
cin >> total;
if ( total == 999 ) break;
// ...
}
you have to do three corrections in your code to make it right
first you have to check if total is equal to 999, then break in your do loop just after getting the total from user
second - you have to put same condition in your first while loop
and lastly - instead of while(total!='999') u shall write while(total!=999) because it is integer