Please fix my code. The error is that fucntions named takeChoice1 and takeChoice
ID: 3660978 • Letter: P
Question
Please fix my code.
The error is that fucntions named takeChoice1 and takeChoice3 are repeating when they shouldn't be
Also I haven't successfully gotten for data to be passed onto the main function from the reportTotalWin function. thanks a bunches!
-----------
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
double reportTotalWin(); //this function will take winnings from pick1 and pick3, calculate and return grand total winning.
void showMenu(); //this function will display ALL menu choices
int takeChoice1(); //this function will take, validate and return user choice selected from the menu choices from showMenu for pick 1
int takeChoice3(); //this function will take, validate and return user choice selected from the menu choices from showMenu for pick 3
//-------------------------------------------------------------------------------
int main ()
{
showMenu();
takeChoice1();
takeChoice3();
reportTotalWin();
int play;
cout << " Would you like to continue playing? Press 1 to continue or 2 to quit: ";
cin >> play;
if ( (play != 1)&&(play!=2) )
{
cout << " I'm sorry please enter a valid response ";
cout << " Enter 1 to continue or 2 to quit: ";
cin >> play;
}
else
if (play == 1)
{
takeChoice1();
takeChoice3();
reportTotalWin();
}
if (play == 2)
{
cout << " Oh, well this is ackward. See you next time!";
}
return 0;
}
//-------------------------------------------------------------------------------
double reportTotalWin()
{
double total, //the total of all winnings
total1, //the total won in pick 1 game
total2; //the total won in pick 3 game
static int winnings = 0; //everyone starts at $0 won in pick 1
static int winnings3 = 0; //everyone starts at $0 won in pick 3
//total1 = winnings += takeChoice1(); //not sure if these are correct
//total2= winnings3 += takeChoice3(); //not sure if these are correct
//total = total1 + total2; //not sure if these are correct
winnings += takeChoice1(); //winnings for pick 1 is 0 (or current # in winnings) + returned value from takeChoice1()
total1 = winnings; //assigns winnings to a total
winnings3 += takeChoice3(); //winnings for pick 3 is 0 (or current # in winnings3) + returned value from takeChoice3()
total2 = winnings3; //assigns winnings3 to another total
total = total1 + total2; //adds both totals to get grand total winnings of both games
cout << "---------------------------------------------------------------------- ";
cout << "Time for your winnings! ";
cout << "Pick 1: ";
cout << " Your winnings for Pick 1 is: $ " << winnings << endl;
cout << " Pick 3: ";
cout << " Your winnings for Pick 3 is: $ " << winnings3 << endl;
cout << "---------------------------------------------------------------------- ";
cout << "Your total winnings for everytime you played Pick 1 is: $" << total1 << endl;
cout << "Your total winnings for everytime you played Pick 2 is: $" << total2 << endl;
cout << "Your total winnings for all games is: $" << total << endl;
if (total <= 100)
{
cout << " You can continue playing if you please! ";
}
else {
cout << " You have reached the max amount you can win for today. Please play again another day. ";
system("END"); //causes program to end ...?
}
return total;
}
//-------------------------------------------------------------------------------
void showMenu(void)
{
cout << "Welcome to your daily Lucky Pick! ";
cout << "Where you can walk out with $100 daily! ";
cout << "When prompted you can decide if you want to play again. ";
cout << "--------------------------------------- ";
cout << " OFFICIAL RULES ";
cout << "--------------------------------------- ";
cout << " Pick 1: ";
cout << " Guess any number from 0 though 25, then press ENTER ";
cout << " A correct guess results in a winning of $2 ";
cout << "Pick 3: ";
cout << " Pick 1 special number from 1 through 25, but not 7 or 13 ";
cout << " Pick any 2 regular numbers from 1 through 10 ";
cout << " A correct special number guess results in a winning of $5 ";
cout << " Two correct regular number guesses result in a winning of $10 ";
cout << " A correct special number guess AND one regular number guess results in a winning of $50 ";
cout << " A correct guess of all 3 numbers results in a winning of $100 ";
return;
}
//-------------------------------------------------------------------------------
int takeChoice1()
{
int Rpick1=rand()&; //assigns random number for pick 1 with range 0-25
int pick1; //assigns user's pick 1
cout << " ---------------------------------------------------------------------- ";
cout << "Enter your number for pick 1: ";
cin >> pick1;
if(Rpick1 == pick1)
{
cout<<"----------------------------- ";
cout<<" YOU WON $2! ";
cout<<"----------------------------- ";
static int winnings = 2;
return winnings;
}
else {
cout << "Sorry, your guess was not correct." << endl;
cout << "The number was "<< Rpick1 << endl;
return 0;
}
}
//-------------------------------------------------------------------------------
int takeChoice3()
{
int Rpick3n1= 1 + rand(); //assigns random number for pick 3 first number, range 1 through 10
int Rpick3n2= 1 + rand(); //assigns random number for pick 3 second number, range 1 through 10
int Rpick3Special; //sets up the random special number
srand(time(NULL)); //seeds the random number
do
{
Rpick3Special = 1+ rand()%; //assigns random number for pick 3 special number, range 1 though 25
}
while(Rpick3Special == 7 && Rpick3Special == 13); //finds random number again if it is 7 or 13
int pick3n1; //assigns user's pick 3 regular number 1
int pick3n2; //assigns user's pick 3 regular number 2
int pick3Special; //assigns user's pick 3 special number
cout << " Time for pick 3! ";
cout << "Pick your first regular number: ";
cin >> pick3n1 ;
cout << "Pick the second regular number: ";
cin >> pick3n2 ;
cout << "Now pick the special number: ";
cin >> pick3Special;
cout << endl;
cout << "Here are the numbers: "<<endl;
cout << "The 1st number was "<< Rpick3n1 << endl;
cout << "The 2nd number was "<< Rpick3n2 << endl;
cout << "The special number was "<< Rpick3Special << endl;
if((Rpick3n1 == pick3n1)&&(Rpick3n2 == pick3n2)&&( Rpick3Special == pick3Special))
{
cout<<"---------------------------- ";
cout<<" YOU WON $100!";
cout<<"---------------------------- ";
return 100;
}
else
if((Rpick3n1 == pick3n1)&&( Rpick3Special == pick3Special))
{
cout<<"---------------------------- ";
cout<<" YOU WON $50! ";
cout<<"---------------------------- ";
return 50;
}
else
if((Rpick3n2 == pick3n2)&&( Rpick3Special == pick3Special))
{
cout<<"---------------------------- ";
cout<<" YOU WON $50! ";
cout<<"---------------------------- ";
return 50;
}
else
if((Rpick3n1 == pick3n1)&&(Rpick3n2 == pick3n2))
{
cout<<"----------------------------- ";
cout<<" YOU WON $10! ";
cout<<"----------------------------- ";
return 10;
}
else
if( Rpick3Special == pick3Special )
{
cout << "------------------------------ ";
cout << " YOU WON $5! ";
cout << "------------------------------ ";
return 5;
}
else {
cout << " Sorry no matching numbers. ";
return 0;
}
}
Explanation / Answer
problem in not in takeChoice1 and takeChoice3, actually problem is in main function.
/*100% working code*/
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
double reportTotalWin(); //this function will take winnings from pick1 and pick3, calculate and return grand total winning.
void showMenu(); //this function will display ALL menu choices
int takeChoice1(); //this function will take, validate and return user choice selected from the menu choices from showMenu for pick 1
int takeChoice3(); //this function will take, validate and return user choice selected from the menu choices from showMenu for pick 3
//-------------------------------------------------------------------------------
int main ()
{
showMenu();
reportTotalWin();
int play;
cout << " Would you like to continue playing? Press 1 to continue or 2 to quit: ";
cin >> play;
if ( (play != 1)&&(play!=2) )
{
cout << " I'm sorry please enter a valid response ";
cout << " Enter 1 to continue or 2 to quit: ";
cin >> play;
}
else
if (play == 1)
{
takeChoice1();
takeChoice3();
reportTotalWin();
}
if (play == 2)
{
cout << " Oh, well this is ackward. See you next time!";
}
return 0;
}
//-------------------------------------------------------------------------------
double reportTotalWin()
{
double total, //the total of all winnings
total1, //the total won in pick 1 game
total2; //the total won in pick 3 game
static int winnings = 0; //everyone starts at $0 won in pick 1
static int winnings3 = 0; //everyone starts at $0 won in pick 3
//total1 = winnings += takeChoice1(); //not sure if these are correct
//total2= winnings3 += takeChoice3(); //not sure if these are correct
//total = total1 + total2; //not sure if these are correct
winnings += takeChoice1(); //winnings for pick 1 is 0 (or current # in winnings) + returned value from takeChoice1()
total1 = winnings; //assigns winnings to a total
winnings3 += takeChoice3(); //winnings for pick 3 is 0 (or current # in winnings3) + returned value from takeChoice3()
total2 = winnings3; //assigns winnings3 to another total
total = total1 + total2; //adds both totals to get grand total winnings of both games
cout << "---------------------------------------------------------------------- ";
cout << "Time for your winnings! ";
cout << "Pick 1: ";
cout << " Your winnings for Pick 1 is: $ " << winnings << endl;
cout << " Pick 3: ";
cout << " Your winnings for Pick 3 is: $ " << winnings3 << endl;
cout << "---------------------------------------------------------------------- ";
cout << "Your total winnings for everytime you played Pick 1 is: $" << total1 << endl;
cout << "Your total winnings for everytime you played Pick 2 is: $" << total2 << endl;
cout << "Your total winnings for all games is: $" << total << endl;
if (total <= 100)
{
cout << " You can continue playing if you please! ";
}
else {
cout << " You have reached the max amount you can win for today. Please play again another day. ";
system("END"); //causes program to end ...?
}
return total;
}
//-------------------------------------------------------------------------------
void showMenu(void)
{
cout << "Welcome to your daily Lucky Pick! ";
cout << "Where you can walk out with $100 daily! ";
cout << "When prompted you can decide if you want to play again. ";
cout << "--------------------------------------- ";
cout << " OFFICIAL RULES ";
cout << "--------------------------------------- ";
cout << " Pick 1: ";
cout << " Guess any number from 0 though 25, then press ENTER ";
cout << " A correct guess results in a winning of $2 ";
cout << "Pick 3: ";
cout << " Pick 1 special number from 1 through 25, but not 7 or 13 ";
cout << " Pick any 2 regular numbers from 1 through 10 ";
cout << " A correct special number guess results in a winning of $5 ";
cout << " Two correct regular number guesses result in a winning of $10 ";
cout << " A correct special number guess AND one regular number guess results in a winning of $50 ";
cout << " A correct guess of all 3 numbers results in a winning of $100 ";
return;
}
//-------------------------------------------------------------------------------
int takeChoice1()
{
int Rpick1=1+rand()%25; //assigns random number for pick 1 with range 0-25
int pick1; //assigns user's pick 1
cout << " ---------------------------------------------------------------------- ";
cout << "Enter your number for pick 1: ";
cin >> pick1;
if(Rpick1 == pick1)
{
cout<<"----------------------------- ";
cout<<" YOU WON $2! ";
cout<<"----------------------------- ";
static int winnings = 2;
return winnings;
}
else
{
cout << "Sorry, your guess was not correct." << endl;
cout << "The number was "<< Rpick1 << endl;
return 0;
}
}
//-------------------------------------------------------------------------------
int takeChoice3()
{
int Rpick3n1= 1 + rand()%10; //assigns random number for pick 3 first number, range 1 through 10
int Rpick3n2= 1 + rand()%10; //assigns random number for pick 3 second number, range 1 through 10
int Rpick3Special; //sets up the random special number
srand(time(NULL)); //seeds the random number
do
{
Rpick3Special = 1+ rand()%25; //assigns random number for pick 3 special number, range 1 though 25
}
while(Rpick3Special == 7 && Rpick3Special == 13); //finds random number again if it is 7 or 13
int pick3n1; //assigns user's pick 3 regular number 1
int pick3n2; //assigns user's pick 3 regular number 2
int pick3Special; //assigns user's pick 3 special number
cout << " Time for pick 3! ";
cout << "Pick your first regular number: ";
cin >> pick3n1 ;
cout << "Pick the second regular number: ";
cin >> pick3n2 ;
cout << "Now pick the special number: ";
cin >> pick3Special;
cout << endl;
cout << "Here are the numbers: "<<endl;
cout << "The 1st number was "<< Rpick3n1 << endl;
cout << "The 2nd number was "<< Rpick3n2 << endl;
cout << "The special number was "<< Rpick3Special << endl;
if((Rpick3n1 == pick3n1)&&(Rpick3n2 == pick3n2)&&( Rpick3Special == pick3Special))
{
cout<<"---------------------------- ";
cout<<" YOU WON $100!";
cout<<"---------------------------- ";
return 100;
}
else if((Rpick3n1 == pick3n1)&&( Rpick3Special == pick3Special))
{
cout<<"---------------------------- ";
cout<<" YOU WON $50! ";
cout<<"---------------------------- ";
return 50;
}
else if((Rpick3n2 == pick3n2)&&( Rpick3Special == pick3Special))
{
cout<<"---------------------------- ";
cout<<" YOU WON $50! ";
cout<<"---------------------------- ";
return 50;
}
else if((Rpick3n1 == pick3n1)&&(Rpick3n2 == pick3n2))
{
cout<<"----------------------------- ";
cout<<" YOU WON $10! ";
cout<<"----------------------------- ";
return 10;
}
else if( Rpick3Special == pick3Special )
{
cout << "------------------------------ ";
cout << " YOU WON $5! ";
cout << "------------------------------ ";
return 5;
}
else
{
cout << " Sorry no matching numbers. ";
return 0;
}
}