how to set a block

why does it need to always return a true/false value from anything that isnt Y or N

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool getYesorNo()
{
  //
  char letsGo;
  bool getYesorNo = true;
  do
  {
    cout <<"Do you want to play again? [Y for yes/N for no] ";
    cin >> letsGo;
    cin.ignore(1000, 10);
    cout << endl;
	if (toupper(letsGo) != 'Y' || toupper(letsGo) != 'N') cout << "Invalid";
    if (toupper(letsGo) == 'N') return false;
    else if (toupper(letsGo) == 'Y') return true;
  }while (toupper(letsGo) != 'Y' || toupper(letsGo) != 'N');
}//playAg 
it forces user to put in 'Y' or 'N' and returns that choice as a boolean.
if it is not one of those it loops until the user complies.
Your question does not make much sense, but maybe that helps.
while (toupper(letsGo) != 'Y' || toupper(letsGo) != 'N');
This bit of code will always return true, since letsGo cannot physically be equal to both 'Y' and 'N'. You mean to change "||" to "&&"
im tried your code but it keeps giving me the same error even tho i have it in another program working just fine. the only way it'll work is it only validates N, (as in if i press n it'll shut down the program, but if i press ANY other key the game will keep going instead of if i only pressed y

overUnder5.cpp: In function 'bool getYesorNo()':
overUnder5.cpp:155:1: warning: control reaches end of non-void function [-Wreturn-type]
155 | }//playAg
| ^

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
//libraries
#include <cctype> //reference to tolower
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;

//Programmer defined data types
//playing cards
struct GuessMade
{
  int chosen;
}; //

//Special compiler dependent definitions
//NONE

//global constants/variables
const int MAX_TRIES = 100;

//Programmer defined functions
void introduction(string obj, string ins);  //user introduction
bool checkValid (int target, int uG, GuessMade*, int n); //input validation
bool dupeCheck (int uG, GuessMade*, int n);  // dupe check
bool getYesorNo();

//main program
int main()
{
  //Data
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  string objective = "play the overUnder game."; //program objective
  string instructions = "I'm thinking of a number between 1 and 100. Guess what it is to win the game"; //user instructions
  int ngameLogs = 0;
  GuessMade userPick[MAX_TRIES];
  int userGuess; //users guess
  int compGuess = 1 + rand() % 100; //random number
  
  //user introduction
  introduction(objective, instructions);

  //game loop
  do
  {
    //user input
    cout << "What is your guess? " << endl;
    cin >> userGuess; 
    cin.ignore(1000, 10);

    //input validation
    if (checkValid(compGuess, userGuess, &userPick[ngameLogs], ngameLogs))
    {
      //update list of non dupe guesses 
        userPick[ngameLogs++].chosen = userGuess;
    
      //user feedback  
      if (userGuess == compGuess)
      {
        cout <<"Thats right -- it's " << compGuess << endl;
      }//guess is correct
      else if (userGuess > compGuess)
      {
        cout <<"Thats too high -- guess again " << endl;
      }//guess is too high  
      else if (userGuess < compGuess)
      {
        cout <<"Thats too low -- guess again " << endl;
      }//guess is too low
    }//checkvalid
  }while (getYesorNo()); // game loop

}//main

// user introduction
void introduction(string obj, string ins)
{
  //data
  //obj is the program objective
  //ins is the user instructions

  //output user introduction
  cout << "Objective: This program will " << obj << endl; 
  cout << "Programmer: Prosper\n"; 
  cout << "Editor(s) used: Notepad\n"; 
  cout << "Compiler(s) used: TDM MinGW\n"; 
  cout << "File: " << __FILE__ << endl; 
  cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl; 
  cout << ins << endl << endl;
}//introduction

//input valid
bool checkValid(int target, int uG, GuessMade *a, int n)
{
  //data
  //target
  //uG
  //gameLog
  //n
  bool result = true; //false is not valid, true is valid

  //input validation  
  if (uG < 1 || uG > 100)  
  {    
    result = false;    
    cout << "not a valid value--guess again [1-100]: ";
  }// if valid  
  if (dupeCheck(uG, a, n))   
  {    
  result = false;    
  cout << "you already guessed " << uG << " -- guess again: ";  
  }//check duplicate  
  return result;
}//checkValid

//check if user guess is a duplicate guess
bool dupeCheck(int uG, GuessMade *a, int n)
{
  //data
  //GuessMade is the collection of non-duplicate guesses
  //n is the size counter of the list
  //uG is the user guess
  bool result = false; //false is not a duplicate, true is a duplicate guess  
  int i; //loop index for traversing the list
  
  //check if duplicate guess
  for(i=0; i < n; i = i + 1)
  {    
    if (uG == a[i].chosen)    
    {
      result = true;
    }//if
  }//for i  
  return result;
}//checkDuplicatebool 

bool getYesorNo()
{
  //
  char letsGo;
  do
  {
    cout <<"Do you want to play again? [Y for yes/N for no] ";
    cin >> letsGo;
    cin.ignore(1000, 10);
    cout << endl;
    if (toupper(letsGo) == 'N') return false;
    if (toupper(letsGo) == 'Y') return true;
  }while (toupper(letsGo) != 'Y' || toupper(letsGo) != 'N');
}//playAg 
Re-read zapshe's post. You might as well just make your loop be "do { ... } while (true);"

But even so, the compiler might not be smart enough to realize that control can't reach line 149. You could just put a "return false;" at the end of the function to shut the compiler up.
Last edited on
I ran your posted code, there was no issue other than the need to #include <string>

The "error" you posted is just a warning that can be ignored, the loop you have will never end until the user inputs n or y so that the function can return true or false.

Even better, you can take Ganado's advice and have a loop on a true condition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool getYesorNo()
{
	char letsGo;

	while (1)
	{
		cout << "Do you want to play again? [Y for yes/N for no] ";
		cin >> letsGo;
		cin.ignore(1000, 10);
		cout << endl;
		if (toupper(letsGo) == 'N') return false;
		if (toupper(letsGo) == 'Y') return true;
	}
}
thank you guys!
Topic archived. No new replies allowed.