yes/no question

Hi guys, so in my program I need the user to input a "y" or "n" as a yes/no response, but I need the response to be counted as a numerical value. For example, if the user enters "y," then that means they have 1 set of stairs to climb. If they answer "n," then they have 0 set of stairs to climb. Then I want the program to take their response and add it up.
So if they have 1 (meaning they entered "y") set of stairs to climb at their house and then another set of stairs to climb at their destination, they have a total of 2 sets of stairs. Or they have 0 (meaning they entered "n") sets of stairs to climb at their house, and 1 set of stairs to climb at their destination. How do I get the program to add these up?

Let me know if that needs some clarification. Thank you!

Last edited on
How do you think you would do it ?
Rules prevent us from doing homework for you, so show us what you have.
Hint: Sounds like you need a boolean return type for your response and a variable for counting.
This is what I have:

1
2
3
4
5
6
7
8
9
10
11
// Set number of stairs at origin 
		int y = 1; int n = 0;
		
		cout << "Does the origin have more than 15 stairs (y/n)?\n"; // y = 1, n = 0
		cin >> stair_user_origin;

		// Set number of stairs at destination
		cout << "Does the destination have more than 15 stairs (y/n)?\n";
		cin >> stair_user_destination;

		stair_total = stair_user_origin + stair_user_destination; // total set of stairs (0, 1, 2), additional $90 charge for each set of stairs  



And so each set of stairs that they answer "y" for charges them an additional $90. What I don't get is how to take both answers and add them together as the calculator (which is what the program is supposed to be) later determines the total charge for the stairs.
Last edited on
Here is an example, using a char, of a function that gets a Y/N answer to study

1
2
3
4
5
6
7
// get an answer in the form 'y' or 'n'
	char Yn_Answer(){
		while(true) {
			char answer(std::toupper(std::cin.get()));
			if(answer == 'Y' || answer == 'N') return answer;
		};
}
What I don't get is how to take both answers and add them together


one option might look like this
1
2
3
double additionalCharge=0;
if ((stair_user_origin='Y') && (stair_user_destination='Y'))
   {additionalCharge=90;}
Last edited on
That should be ((stair_user_origin=='Y') && (stair_user_destination=='Y'))

Beware of assignment in conditionals
If you want them as a numerical value, what I would suggest is something like

bool isyes(string str){return str=="y"||str=="Y";}

And then in your main, you can do

stair_total=isyes(stair_user_origin)+isyes(stair_user_destination);

because true becomes 1 while false becomes 0. But if you just need to check if both are true, just use &&
Topic archived. No new replies allowed.