May 9, 2014 at 8:40pm UTC
I have spent hours on this program, and I cannot seem to get the game to run properly. The Random number generated for game1 is ALWAYS 24, and it is always 1 for the Special number in game3. Can someone please fix this for me?
Also, how would I go about using a quit function to terminate the program? I have tried multiple ways but always seem to get errors. Please Help!
#include<iostream>
#include<ctime> //for time functions
#include<cstdlib> //for srand and rand
using namespace std;
double Pick1();
double Pick3();
//double totalWins();
int main()
{
int choice;
cout<<"To play, Press 1 for Pick1, 2 for Pick2, or 3 to terminate:"<<endl;
cin>>choice;
while (choice != 3) {
if (choice==1) {
Pick1();
}
else if (choice==2)
Pick3();
cout<<"Enter 1 for Game1, 2 for Game3, or 3 to quit"<<endl;
cin>>choice;
}
cout<<"Pick 1:"<<endl;
cout<<"----------------"<<endl;
static double winnings = 0;
winnings += Pick1();
cout<<"Your winnings for \"Pick 1\" is: $ "<< winnings <<endl;
cout<<endl;
cout<<"Pick 3:"<<endl;
cout<<"----------------"<<endl;
static double winnings3 = 0;
winnings3 += Pick3();
cout<<endl;
cout<<"Your winnings for \"Pick 3\" is: $ "<< winnings3 <<endl;
//cout<<"Total amount winnings is: $ "<< totalWins() <<endl;
}
double Pick1()
{
srand(static_cast<unsigned int> ((0)));
//double w;
double randNum=rand()%26;
double userNum=rand()%26;
cout<<"Enter a number between 0-25:";
cin>>userNum;
srand(userNum);
if(randNum == userNum)
{
cout<<"-----------------------------";
cout<<" !!!!You Won $2.00!!!!\n";
cout<<"-----------------------------";
cout<<endl;
return 2;
}
else
{
cout<<"You lost"<<endl;
cout<<"The number was "<< randNum<<endl;
return 0;
}
}
double Pick3()
{
srand(static_cast<unsigned int> ((0)));
double randNum=rand()%11;
double userNum=rand()%11;
double userNum2=rand()%11;
double specNum=rand()%26;
cout<<"Enter two numbers between 1-10:"<<endl;
cout<<"first number: ";
cin>>userNum;
cout<<"second number: ";
cin>>userNum2;
cout<<"Now enter a special number between 1-25 (except 7 and 13):"<<endl;
cin>>specNum;
srand(userNum);
srand(userNum2);
srand(specNum);
if(randNum == specNum)
{
cout<<"-----------------------------";
cout<<" !!!!You Won $5.00!!!!\n";
cout<<"-----------------------------";
return 5;
}
if((randNum == userNum)&&(randNum == userNum2))
{
cout<<"----------------------------";
cout<<" !!!!You Won $10.00!!!!\n";
cout<<"----------------------------";
return 10;
}
if((randNum == userNum)&&(randNum == specNum))
{
cout<<"---------------------------";
cout<<" !!!!You Won $50.00!!!!\n";
cout<<"---------------------------";
return 50;
}
if((randNum == userNum)&&(randNum == userNum2)&&(randNum == specNum))
{
cout<<"---------------------------";
cout<<" !!!!You Won $100.00!!!!\n";
cout<<"---------------------------";
return 100;
}
else
{
cout<<"You lost"<<endl;
cout<<"The number was "<<randNum<<endl;
return 0;
}
}
double totalWins()
{
double total, total1, total2, winnings=0;
total1= winnings += Pick1();;
total2= winnings += Pick3();;
total = total1 + total2;
system ("PAUSE");
return total;
}
May 9, 2014 at 9:43pm UTC
I am extremely new to all this,
So please bare with me.
Should I just delete that line then? Or how can I go about Not seeding it with the same value everytime
May 10, 2014 at 1:54am UTC
Understood mate, That seemed to have worked.
How do i go about including a function to terminate when user presses quit?
I've tried
declaring
int quit();
then calling the function later if choice is 3
If (choice == 3)
quit();
That does not seem to work though. Pardon me for my ignorance!
May 10, 2014 at 2:24am UTC
You declared the function but you never defined the function.
May 11, 2014 at 1:37am UTC
#include<iostream>
#include<ctime> //for time functions
#include<cstdlib> //for srand and rand
using namespace std;
double Pick1();
double Pick3();
void quit();
//double totalWins();
int main()
{
int choice;
cout<<"To play, Press 1 for Pick1, 2 for Pick2, or 3 to terminate:"<<endl;
cin>>choice;
while (choice != 3) {
if (choice==1) {
Pick1();
}
else if (choice==2)
Pick3();
if (choice==3) {
quit ();
}
cout<<"Enter 1 for Game1, 2 for Game3, or 3 to quit"<<endl;
cin>>choice;
}
cout<<"Pick 1:"<<endl;
cout<<"----------------"<<endl;
static double winnings = 0;
winnings += Pick1();
cout<<"Your winnings for \"Pick 1\" is: $ "<< winnings <<endl;
cout<<endl;
cout<<"Pick 3:"<<endl;
cout<<"----------------"<<endl;
static double winnings3 = 0;
winnings3 += Pick3();
cout<<endl;
cout<<"Your winnings for \"Pick 3\" is: $ "<< winnings3 <<endl;
//cout<<"Total amount winnings is: $ "<< totalWins() <<endl;
}
void quit()
{
cout<<"Goodbye, Thanks for playing!"<<endl;
}
I have defined it below, but the problem is that the program keeps running as if the user never pressed 3
May 11, 2014 at 2:11am UTC
You declared Pick1() and Pick3() but you never defined them.
May 11, 2014 at 2:36am UTC
Yes mate I have. I did not want to include the whole program but here it is....
I wanted to focus on the exit function
#include<iostream>
#include<ctime> //for time functions
#include<cstdlib> //for srand and rand
using namespace std;
double Pick1();
double Pick3();
void quit();
//double totalWins();
int main()
{
int choice;
cout<<"To play, Press 1 for Pick1, 2 for Pick2, or 3 to terminate:"<<endl;
cin>>choice;
while (choice != 3) {
if (choice==1) {
Pick1();
}
else if (choice==2)
Pick3();
if (choice==3) {
quit ();
}
cout<<"Enter 1 for Game1, 2 for Game3, or 3 to quit"<<endl;
cin>>choice;
}
cout<<"Pick 1:"<<endl;
cout<<"----------------"<<endl;
static double winnings = 0;
winnings += Pick1();
cout<<"Your winnings for \"Pick 1\" is: $ "<< winnings <<endl;
cout<<endl;
cout<<"Pick 3:"<<endl;
cout<<"----------------"<<endl;
static double winnings3 = 0;
winnings3 += Pick3();
cout<<endl;
cout<<"Your winnings for \"Pick 3\" is: $ "<< winnings3 <<endl;
//cout<<"Total amount winnings is: $ "<< totalWins() <<endl;
}
void quit()
{
cout<<"Goodbye, Thanks for playing!"<<endl;
}
double Pick1()
{
srand(static_cast<unsigned int> (time(0)));
//double w;
double randNum=rand()%26;
double userNum=rand()%26;
cout<<"Enter a number between 0-25:";
cin>>userNum;
srand(userNum);
if(randNum == userNum)
{
cout<<"-----------------------------";
cout<<" !!!!You Won $2.00!!!!\n";
cout<<"-----------------------------";
cout<<endl;
return 2;
}
else
{
cout<<"You lost"<<endl;
cout<<"The number was "<< randNum<<endl;
return 0;
}
}
double Pick3()
{
srand(static_cast<unsigned int> (time(0)));
double randNum=rand()%11;
double userNum=rand()%11;
double userNum2=rand()%11;
double specNum=rand()%26;
cout<<"Enter two numbers between 1-10:"<<endl;
cout<<"first number: ";
cin>>userNum;
cout<<"second number: ";
cin>>userNum2;
cout<<"Now enter a special number between 1-25 (except 7 and 13):"<<endl;
cin>>specNum;
srand(userNum);
srand(userNum2);
srand(specNum);
if(randNum == specNum)
{
cout<<"-----------------------------";
cout<<" !!!!You Won $5.00!!!!\n";
cout<<"-----------------------------";
return 5;
}
if((randNum == userNum)&&(randNum == userNum2))
{
cout<<"----------------------------";
cout<<" !!!!You Won $10.00!!!!\n";
cout<<"----------------------------";
return 10;
}
if((randNum == userNum)&&(randNum == specNum))
{
cout<<"---------------------------";
cout<<" !!!!You Won $50.00!!!!\n";
cout<<"---------------------------";
return 50;
}
if((randNum == userNum)&&(randNum == userNum2)&&(randNum == specNum))
{
cout<<"---------------------------";
cout<<" !!!!You Won $100.00!!!!\n";
cout<<"---------------------------";
return 100;
}
else
{
cout<<"You lost"<<endl;
cout<<"The number was "<<randNum<<endl;
return 0;
}
}
double totalWins()
{
double total, total1, total2, winnings=0;
total1= winnings += Pick1();;
total2= winnings += Pick3();;
total = total1 + total2;
system ("PAUSE");
return total;
}
May 11, 2014 at 2:50am UTC
Your code would be much easier to read if you end it with "[/code]" and start it with "[code]", alternatively you can click the "<>" button on the right under format when you post and have those tags inserted automatically. Can't get much simpler than that.