Repeating Program, bool input twice

Hello, I have been making a number guessing game for a homework assignment, and so far it works wonderfully. However, I have been having a problem in regards to bool retry(). Simply put, the bool asks for an input twice, whether you wish to repeat the game or not, and the second input is the one which is accepted (i.e entering "y, n" is the same as entering "n, n", and entering "n, y" is the same as entering "y, y"). Is there any way to fix this?

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
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <time.h>
#include <random>
using namespace std;
void print_game_instructions()
{
  cout << "Guess a number between 100 and 999!";
}
bool retry()
{
  char answer;
  cout << "Would you like to play again? Enter Y/N\n";
  cin >> setw(0) >> answer;
  switch (answer)
  {
    case 'y':
    {
      return 1;
    }
    case 'Y':
    {
      return 1;
    }
    case 'n':
    {
      return 0;
    }
    case 'N':
    {
      return 0;
    }
    default:
    {
      return 0;
    }
  }
}
int generate_rand_num(int min, int max)
{
  srand((unsigned) time(0));
  int randomNumber;
  failsafe: randomNumber = (rand() % max - min) + 1;
  if (randomNumber < min || randomNumber > max) {
    goto failsafe;
  }
  else {
    return randomNumber;
  }
}
int main()
{
  do{
  repeat: int randNum = generate_rand_num(100, 999);
  int attempt = 0;
  int guess;
    do {
    failsafe2: cout << "debug answer:" << randNum << endl;
    failguess: cout << "You have " << 10 - attempt << " guesses remaining!\n";
    cin >> guess;
    if (guess > 999 || guess < 100) {
      cout << "Your number is invalid!\n";
      goto failsafe2;
    }
    else {
      attempt++;
      if (guess < randNum) {
        cout << "Too low!\n";
        goto failguess;
      }
      else if (guess > randNum) {
        cout << "Too high!\n";
        goto failguess;
      }
      else {
        cout << "You win! You won in " << attempt << " tries, and you had " << 10 - attempt << " guesses remaining!\n";
        retry();
      }
    }
    if (retry() == 1)
    {
      goto repeat;
    }
    else
    {
      abort();
    }
    } while(guess!=randNum && attempt!=10);
    cout << "You lose! You took too many tries! The number was " << randNum << endl;
    retry();
  } while(retry()!=0);
  abort();
}
Last edited on
retry();
if (retry() == 1)

...

retry();
} while(retry()!=0);

Well you call it all over the place, many times.

You also need to try to do this without using goto's.
If you get used to this particular crutch to help you walk, you'll never learn to run or fly.
Turns out the answer was to remove all standalone retry() calls, so that answers things.

As for the goto thing, while I do agree I should not be using it (it does get a bit complicated after a while), it does help in certain situations throughout the code where it simply would not work otherwise (like failsafe calls in order to create a valid answer)

Thanks for your help.
Topic archived. No new replies allowed.