bool, output true for yes and false for no

Hi everyone, trying to figure out 2 things.

-How can I get the bool function to read the yes or no, basically if the user inputs a yes, then return 1 and no return 0 and link it back to the main, yes is true and no is false.

-to count the users input other than yes and no n times, the output should just return false(possible to use a increment loop for greater than 2 times)?

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
//using bool return type
#include<iostream>
using namespace std;
bool YesNo(float); // declare 1 variable

int main()

{
	
	float num; //ok
	cout<<"Enter yes, or no: "; // ok
	cin>>num; //ok
	if(num!=yes ||no ) //ok
	cout<<"answer again";
	
	if (YesNo(num)) //ok
	
	cout<<"True"; //ok
	else //ok
	cout<<"False "; //ok
	
}
bool YesNo(float n) // ok
{
	char yes, no;
	if (n==yes) // confused?
	return 1; // true
	else
	return 0; //false
	
}
The problem with your code is that you are trying to input a float (which is a number) rather than a string or character. But other than that, most of your code was correct except for a few things; when you check the value of a string you need to remember to put speech marks around it. In line 13 you aren't allowed to write it like that, you have to write the full thing out again unfortunately as you can see in my code.

I also made it repeat for you until the user enters the word "yes" or "no" and counts the amount of times they fail to do so.

Here's my code:

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
#include <iostream>
#include <string>
using namespace std;
bool YesNo(string); // declare 1 variable

int main()
{
	int tries = 0; // the attempts it took to enter valid input
	string num; //ok
	bool invalid = false; // whether their input was invalid or not
	do {
		tries++;
		cout << "Enter yes, or no: "; // ok
		getline(cin, num);
		if(num != "yes" && num != "no") {
			cout << "answer again" << endl;
			invalid = true;
		} else {
			invalid = false;
		}
	} while (invalid);
	if (YesNo(num)) //ok
		cout << "True"; //ok
	else //ok
		cout << "False "; //ok
}
bool YesNo(string n) // ok
{
	if (n == "yes") // confused?
		return 1; // true
	else
		return 0; //false
}
Topic archived. No new replies allowed.