Guessing game - setting levels
Jul 6, 2011 at 4:57pm UTC
Hi! i was trying to write a guessing game and i had more or less finished it. but then i decided that i need an option for the user to choose a difficulty level. that tool is defined as an
function (in the very bottom) but it always runs as the level EASY. can't really make out whats the problem. Hope you will excuse me for posting all the program but thats so you understand what am i doing.
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
#include <iostream>
#include <cmath>
#include <fstream>
void choose_level (int );
int level;
int tries_left;
int main ()
{
using namespace std;
int input;
cout << "This is a number guessing game" << endl;
cout << "Choose the difficulty level 1)easy /2)medium /3)hard:" << endl;
cout << endl;
cout << "Easy (press 1) gives you 8 tries" << endl;
cout << endl;
cout << "Medium (press 2) gives you 6 tries" << endl;
cout << endl;
cout << "Hard (press 3) gives you 4 tries" << endl;
cout << endl;
cin >> level;
cout << endl;
if (level !=1 and level !=2 and level !=3)
{
do
{
cout << "use only options - 1 , 2, 3" << endl;
cout << endl;
cin >> level;
cout << endl;
}
while (level !=1 and level !=2 and level !=3);
}
choose_level (level);
cout << "you have " << tries_left << " tries to guess a randomly genetared number (1 - 100)" << endl;
cout << "computer will tell whether your guess is too high or too low. Good luck! " << endl;
cout << endl;
int rand_num;
srand ( time(NULL) ); /* every time generates different number*/
rand_num = rand () % 100 + 1; /* interval 1 - 100 */
//cout << rand_num;
do
{
tries_left = tries_left - 1;
cout << "make your guess " ;
cin >> input;
cout << endl;
if (input > rand_num)
{
if (tries_left> 0)
{
cout << "your guess is too high" << endl;
cout << "you have " << tries_left << " tries left" << endl;
cout << endl;
}
else
{
cout << "your guess is still too high" << endl;
cout << endl;
}
}
else if (input < rand_num)
{
if (tries_left> 0)
{
cout << "your guess is too low" << endl;
cout << "you have " << tries_left << " tries left" << endl;
cout << endl;
}
else
{
cout << "your guess is still too low" << endl;
cout << endl;
}
}
else
{
cout << "Correct! Thats the one! :)" << endl;
break ;
}
}
while ( tries_left> 0);
if (tries_left <1)
cout << "you LOST! GAME OVER" << endl;
if (input != rand_num)
{
char for_losers;
cout << "do you want to find out the number? y/n" << endl;
cin >> for_losers;
if (for_losers == 'y' )
cout << "the number was " << rand_num << endl;
else
cout << "now you will never know! " << endl;
}
cout << endl;
system ("pause" );
return 0;
}
void choose_level (int x)
{
using namespace std;
if (x =1)
{
tries_left =8;
cout << "LEVEL - Easy" << endl;
}
else if (x =2)
{
tries_left =6;
cout << "LEVEL - Medium" << endl;
}
else
{
tries_left =4;
cout << "LEVEL - Hard" << endl;
}
}
Last edited on Jul 6, 2011 at 4:59pm UTC
Jul 6, 2011 at 5:05pm UTC
I didnt read the whole thing. just a quick glance but in your choose level function you are using an assignment operator instead of checking for equality
it should be
1 2 3 4 5 6 7
if (x == 1)
{
//do stuff
}
// ect
Last edited on Jul 6, 2011 at 5:08pm UTC
Jul 6, 2011 at 5:11pm UTC
great! it worked! thanks :)
will have to look into this topic because right now i don't really understand why the computer didn't do what i wanted... why can't i use assignments?
Last edited on Jul 6, 2011 at 5:13pm UTC
Topic archived. No new replies allowed.