make this code simlpler

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

int menu (void);

using namespace 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";

else if (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();
}
else if (y_o_n == "n")
{
    cout<<"press 3 :"<<endl;
    Sleep (2000);
    system ("cls");
    menu();
}
return y_o_n;
}

Last edited on
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.
Also calling main() is not allowed in a C++ program.
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.


Topic archived. No new replies allowed.