C++ looping error
well here is my code what I want it to do is ask the user if they want to
play. Then I want it to output options for the user to input then it
performs random numbers by dice by the number of sides the user inputted.
Then I want it to ask if they want to play again y restarts the program no
exits wit Thank you for playing".
The issue i am having is the first cycle of the program works as it should
but instead of it asking if you want to play again it is blank and if you
hit a number for the dice it will output how many sides the first dice had
no matter what is inputted.
#include <iostream>
#include <ctime>
using namespace std;
int throwDie(int Sides, int &throwResult)
{
throwResult = 1 + rand() % (Sides - 1 + 1);
return throwResult;
}
int main()
{
int dieTot = 0,
throwNumber = 0,
numberSides = 0,
throwResult = 0;
int die1 =4;
int die2 =6;
int die3 =8;
int die4 =10;
int die5 =12;
int die6 =20;
char rollAgain;
srand(unsigned(time(0) ));
START:
cout << "Do you want Play? ";
cin >> rollAgain;
cout << "How many sides? " << endl;
cout << "1 - 4 sided die\n";
cout << "2 - 6 sided die\n";
cout << "3 - 8 sided die\n";
cout << "4 - 10 sided die\n";
cout << "5 - 12 sided die\n";
cout << "6 - 20 sided die\n";
int choice;
cout << "Enter choice: ";
cin >> choice;
switch( choice )
{
case 1:
do
{
cout << "You have choosen a 4 sided die? " << endl;
int numberSides = die1;
cout << numberSides << "-sided die rolled for a value of " <<
throwDie(numberSides, throwResult) << "!" << endl;
dieTot = dieTot + throwResult;
throwNumber++;
cin >> numberSides;
} while (choice != numberSides );
goto START;
break;
case 2:
do
{
cout << "You have choosen a 6 sided die? " << endl;
int numberSides = die2;
cout << numberSides << "-sided die rolled for a value of " <<
throwDie(numberSides, throwResult) << "!" << endl;
dieTot = dieTot + throwResult;
throwNumber++;
cin >> numberSides;
cout << endl << "Total for " << throwNumber << " throws = " << dieTot
<< endl;
} while (choice != numberSides );
break;
case 3:
do
{
cout << "You have choosen a 8 sided die? " << endl;
int numberSides = die3;
cout << numberSides << "-sided die rolled for a value of " <<
throwDie(numberSides, throwResult) << "!" << endl;
dieTot = dieTot + throwResult;
throwNumber++;
cin >> numberSides;
cout << endl << "Total for " << throwNumber << " throws = " << dieTot
<< endl;
} while (choice != numberSides );
break;
case 4:
do
{
cout << "You have choosen a 10 sided die? " << endl;
int numberSides = die4;
cout << numberSides << "-sided die rolled for a value of " <<
throwDie(numberSides, throwResult) << "!" << endl;
dieTot = dieTot + throwResult;
throwNumber++;
cin >> numberSides;
cout << endl << "Total for " << throwNumber << " throws = " << dieTot
<< endl;
} while (choice != numberSides );
break;
case 5:
do
{
cout << "You have choosen a 12 sided die? " << endl;
int numberSides = die5;
cout << numberSides << "-sided die rolled for a value of " <<
throwDie(numberSides, throwResult) << "!" << endl;
dieTot = dieTot + throwResult;
throwNumber++;
cin >> numberSides;
cout << endl << "Total for " << throwNumber << " throws = " << dieTot
<< endl;
} while (choice != numberSides );
break;
case 6:
do
{
cout << "You have choosen a 20 sided die? " << endl;
int numberSides = die6;
cout << numberSides << "-sided die rolled for a value of " <<
throwDie(numberSides, throwResult) << "!" << endl;
dieTot = dieTot + throwResult;
throwNumber++;
cin >> numberSides;
cout << endl << "Total for " << throwNumber << " throws = " << dieTot
<< endl;
}
while (choice != numberSides );
break;
default:
cout << "Not a proper entry.\n";
break;
cout << endl << "Total for " << throwNumber << " throws = " << dieTot <<
endl;
return 0;
}}
No comments:
Post a Comment