Please Help My program!

Hey all! I need help with my first program! I made a guess game and i want to have 5 tries and when all 5 tries are false to ask me if i want to play again or no. Thanks!

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

enum DIFFICULTY {EASY=1, MEDIUM, HARD, EXPERT, IMPOSSIBLE};

int diff, rand_num, guess, chances=0 ;
char answer;

int get_difficulty( )
{
int difficulty;

// Get difficulty
cout << " \t\t\tWelcome to the number guessing game! Pick a difficulty:" << endl;

cout << "--------------------------------------------------------" << endl;
cout << " 1) Easy -- recommended\n" << endl;
cout << " 2) Medium -- intermediate\n" << endl;
cout << " 3) Hard -- advanced\n" << endl;
cout << " 4) Expert -- only 2% can do\n"<< endl;
cout << " 5) Impossible -- Nobody has ever done this\n" << endl;
cout << " 0) Exit -- For pussies\n" << endl;


cout << "--------------------------------------------------------" << endl;


cin >> difficulty;
cout<< "---------------------------------------------------------" << endl;

if(difficulty==1){
cout << "You've chosen Easy difficulty" << endl;
}else if(difficulty==2){
cout << "You've chosen Medium difficulty" << endl;
}else if(difficulty==3){
cout << "You've chosen Hard difficulty" << endl;
}else if(difficulty==4){
cout<< "You've chosen Expert difficulty" << endl;
}else if(difficulty==5){
cout<< "You've chosen Impossible difficulty" << endl;
}

cout << "-------------------------------------" << endl;




switch (difficulty)
{
case (EASY):
return 20;
break;
case (MEDIUM):
return 50;
break;
case (HARD):
return 100;
break;
case (EXPERT):
return 500;
break;
case (IMPOSSIBLE):
return 1000;
break;
default:
exit( 0 );
}
}


int main( )
{
system("color 2");
chances = 5;
diff = get_difficulty( );
guess;

srand(time(0));
rand_num = rand() % diff + 1;

while ( true )
{
cout << "My number is between 1 and " << diff << ": " << endl;

cout << chances << " CHANCES LEFT!!" << endl;
cout << "-------------------------------" << endl;

cin >> guess;


cout << "-------------------" << endl;

if(guess > rand_num) {
cout << "Too high! Try again.\n";
} else if(guess < rand_num) {
cout << "Too low! Try again.\n";
} else {
break;
}
chances--;
}
if(chances == 0) {
cout << "You ran out of tries!\n\n";
} else{
cout << "You guessed the correct number!" << endl;
cout << "My secret number was" << rand_num <<endl;
}

while(true) {
cout << "Would you like to play again (Y/N)? ";
cin >> answer;
cin.ignore();

// Check if proper response.
if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
break;
} else {
cout << "Please enter \'Y\' or \'N\'...\n";
}


// Check user's input and run again or exit;
if(answer == 'n' || answer == 'N') {
cout << "Thank you for playing!";
break;
} else {
cout << "\n\n\n";
}
}

// Safely exit.
cout << "\n\nEnter anything to exit. . . ";
cin.ignore();
return 0;
}
Last edited on
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/


Your int get_difficulty() shows that you can create and use functions. Good.

Make a new function, like void guess(); and move the code of one 5-guess game run into it.

If you main() would have only
1
2
3
4
int main() {
  guess();
  return 0;
}

it would run the game exactly once.

Then it is easier to add the "ask if want to play again" loop into the main(), without the code of game distracting you.
Yeah sorry! Kinda new here. Can you give me an example with that new dunction and where exactly to put it in? I would be more than happy!
I just copy-pasted your code around (kind of), to make an untested mock-up example:
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

enum DIFFICULTY {EASY=1, MEDIUM, HARD, EXPERT, IMPOSSIBLE};

int get_difficulty( )
{
  int difficulty;

  // Get difficulty
  cout << " \t\t\tWelcome to the number guessing game! Pick a difficulty:" << endl;

  cout << "--------------------------------------------------------" << endl;
  cout << " 1) Easy -- recommended\n" << endl;
  cout << " 2) Medium -- intermediate\n" << endl;
  cout << " 3) Hard -- advanced\n" << endl;
  cout << " 4) Expert -- only 2% can do\n"<< endl;
  cout << " 5) Impossible -- Nobody has ever done this\n" << endl;
  cout << " 0) Exit -- For pussies\n" << endl;

  cout << "--------------------------------------------------------" << endl;
  cin >> difficulty;
  cout<< "---------------------------------------------------------" << endl;

  if(difficulty==1){
    cout << "You've chosen Easy difficulty" << endl;
  }else if(difficulty==2){
    cout << "You've chosen Medium difficulty" << endl;
  }else if(difficulty==3){
    cout << "You've chosen Hard difficulty" << endl;
  }else if(difficulty==4){
    cout<< "You've chosen Expert difficulty" << endl;
  }else if(difficulty==5){
    cout<< "You've chosen Impossible difficulty" << endl;
  }
  cout << "-------------------------------------" << endl;

  switch (difficulty)
  {
    case (EASY):
      return 20;
      break;
    case (MEDIUM):
      return 50;
      break;
    case (HARD):
      return 100;
      break;
    case (EXPERT):
      return 500;
      break;
    case (IMPOSSIBLE):
      return 1000;
      break;
    default:
      return 0;
  }
}


void guess5()
{
  int chances = 5;
  int diff = get_difficulty();
  if ( diff == 0 ) return;

  int rand_num = rand() % diff + 1;

  while ( true )
  {
    cout << "My number is between 1 and " << diff << ": " << endl;
    cout << chances << " CHANCES LEFT!!" << endl;
    cout << "-------------------------------" << endl;
    int guess = 0;
    cin >> guess;
    cout << "-------------------" << endl;

    if(guess > rand_num) {
      cout << "Too high! Try again.\n";
    } else if(guess < rand_num) {
      cout << "Too low! Try again.\n";
    } else {
      break;
    }
    chances--;
  }

  if(chances == 0) {
    cout << "You ran out of tries!\n\n";
  } else{
    cout << "You guessed the correct number!" << endl;
    cout << "My secret number was" << rand_num <<endl;
  }
}


int main()
{
  srand(time(0));
  system("color 2");

  while(true)
  {
    guess5(); // play one game

    cout << "Would you like to play again (Y/N)? ";
    char answer;
    cin >> answer;
    cin.ignore();
    // Check if proper response.
    if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
      break;
    } else {
      cout << "Please enter \'Y\' or \'N\'...\n";
    }

    // Check user's input and run again or exit;
    if(answer == 'n' || answer == 'N') {
      cout << "Thank you for playing!";
      break;
    } else {
      cout << "\n\n\n";
    }
  }

  // Safely exit.
  cout << "\n\nEnter anything to exit. . . ";
  cin.ignore();
  return 0;
}
keskiverto i still can't manage to do that.... Still going with -1 -2 -3 and when i guess the number then it says YOU RAN OUT OF TRIES. Please somebody rebuild if possible :( been stuck here for 2 3 days already
Last edited on
Sorry, my bad. You did not actually state what problem you do have and I assumed that it was on making the "Would you like to play again". To help that I did suggest separating code for handling of one game from code that repeats game. Further, I did assume that one game "goes right".


Now that you have pointed out the observed problem -- unexpected output, we can focus on the right part of the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  int chances = 5;
  while ( true )
  {
    int guess = 0;
    cin >> guess;

    if ( guess > rand_num )
    {
      cout << "Too high! Try again.\n";
    }
    else if ( guess < rand_num )
    {
      cout << "Too low! Try again.\n";
    } else {
      break;
    }
    chances--;
  }

When does that loop end?

* The condition on line 2 cannot change. It is usually the loop condition that terminates the loop.

* Line 15 does break out from the loop, but only if guess == rand_num.


Is it true that you want the loop to repeat while the chances is larger than 0?
Topic archived. No new replies allowed.