hi guys. i know the code is long but please try to read it.
if this is well-coded tell me please.but if it 's not,
can i make this code simpler and declare less variables before the main function?
help. i need to make this code simpler to read and understand and to be more performant.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
int menu (void);
usingnamespace std;
string y_o_n;
int placeBet;
int money = 200;
int winner2;
int winner;
int userChoice;
int userNumber;
int pcNumber = 0;
int numofguesses;
string yesorno(void);
int init (void);
int rand_num(void);
int race (void);
int bet (void);
int main (void)
{
cout<<"choose a number \n \n";
menu();
switch (userChoice)
{
case 1 :
bet();
race();
break;
case 2 :
rand_num();
break;
case 3 :
cout<<"you chose to quit"<<endl;
break;
default :
cout<<"invalid choice \n";
}
return 0;
}
int menu ()//menu function//////////////////////////////////////////////////////////////////
{
cout<<"1) race "<<endl
<<"2) random number generator"<<endl
<<"3) Quit the game"<<endl;
cin>>userChoice;
return userChoice;
}
int race ()//if the user chose 1/////////////////////////////////////////////////////////////////
{
srand (time (0));
winner = rand () % 3 + 1;
system ("cls");
cout<<"welcome to the race! \n"
<<"and the snails are off! \n";
Sleep (2000);
cout<<"and the winner is snail "<<winner<<"\n";
if (winner == winner2)
{
cout<<"you won ! \n";
money = money + placeBet;
cout<<"balance : $"<<money<<"\n";
}
else
{
cout<<"you lost \n";
money = money - placeBet;
cout<<"balance : $"<<money<<"\n";
}
yesorno();
return winner;
}
int bet ()//function that asks the user if he wants to continue or not///////////////
{
cout<<"choose your snail number \n";
cin>>winner2;
cout<<"how to much do you want to bet on your snail? "<<endl;
cout<<"you have $"<<money<<endl;
cin>>placeBet;
return winner2;
}
//if the user chose 2///////////////////////////////////////////////
int rand_num(void)
{
init();
cout<<"i have picked a number between 1 and 50"<<endl;
for (numofguesses=0 ; pcNumber != userNumber ; numofguesses++)
{
cout<<"what would you like to guess? \n";
cin>>userNumber;
if(userNumber < pcNumber)//checks if the number picked is low
cout<<"too low \n";
elseif (userNumber > pcNumber)//checks if the number picked is high
cout<<"too high \n";
}
cout<<"you guessed it! it took you "<<numofguesses<<" guesses \n";
yesorno();
return numofguesses;
}
int init ()//seed the generator and choose a random number///////////////////////////
{
std::srand;
std::srand;
srand (time(NULL));
pcNumber = rand () % 50 +1;
return pcNumber;
}
string yesorno()//functions that asks the user if he want to continue .i use it in the 3 choices in the menu.
{
cout<<"do you want to continue? [y]es - [n]o"<<endl;
cin>>y_o_n;
if (y_o_n == "y")
{
Sleep (2000);
system ("cls");
main();
}
elseif (y_o_n == "n")
{
cout<<"press 3 :"<<endl;
Sleep (2000);
system ("cls");
menu();
}
return y_o_n;
}
Making all your data global is frowned upon for many reasons both overt and subtle (but I'm too tired to get into a detailed discussions about the whys and wherefores of that just now). As your programs get larger, these problem will compound themselves. Also, if you're using global data as a way of avoiding the business of learning about passing pointers, references, and suchlike, then you're doing yourself a big disservice since you will never be proficient in the language (or programming in general) until you learn it and become comfortable with it.
In addition to what others have said, Lines 75 and 81 are the same. Do it just once after the if/then/else statement.
Otherwise the program is pretty good. You've broken it down into functional components very well. In particular, the problem with calling main() is easy to solve since you have all the right components: just put a do/while loop in main().
There is no error checking. In particular, a user can bet more than they owe, and if they enter a word instead of a number, things will break horribly.