Issues with cin input with an int


main() {
bool choice1 = 0;
int morale1;
int morale = 50;

if (morale = 0){
std::cout << " Your clients are upset and regret hiring you, your fired! \n";
/////// Hunting Guides Decisions
std::cout << " Morale: You start with 50 morale, your decisions will increase or decrease morale. If you kill a moose you will win the adventure. If you reach 0 Morale you will fail the adventure and get fired.\n\n";

std::cout << " Its 4:30, your just waking up on the first day of a 5 day moose hunt. You begin preparing breakfast when you realise you only have enough cooking oil for 3 days. \n";

std::cout << " You are faced with 2 decisions, Use some oil today for breakfast, or save the oil and possibly burn your food for the clients. \n";

std::cout << " 1: Use oil \n 2: Dont use oil \n" ;
std::cin >> choice1;

if (choice1 == 1, morale + 20){
std::cout << " You use the oil and the breakfast is delicious +20 Morale \n";

else if (choice1 == 2, morale -10){
std::cout << " You didnt use the oil and the breakfast was sub-par -10 Morale \n";

No matter if I input 1 or 2 i get the answer 1, very confused any help would be appreciated i know its a pea brain issue but figured here is the place for pea brain questions.

Is there meant to be a header file in the first line?

yes its im not sure why its not there
Bool choice1 is now also
int choice1

Did the lesson code start out the way shown above? Should it look more like this:

#include <stdio.h>

Nope started as just iostream


#include <iostream>


We don’t see that in your sample code. Does that in fact correct the problem?

its in there it just didnt paste over

Is that meant to be morale += 20?

Also note that it is a statement, not a condition so would not be expected in the condition.

im adding the ability to lose the text adventure, the morale is just a passive addition that if it reaches 0 the player fails

the issue is with what outputed after cin now matter if u plug in 1 or 2, the answer is always 1

Is that a valid condition?

i havent set that up yet, im trying to debug the character input

If there are any syntax errors the code won’t compile. We have to deal with any of the them, first, before we can test logic. The code has to compile.

1 Like

if (choice1 == 1, morale + 20)
Everything to the left of the comma operator has no effect. Yor condition becomes moral+20 if moral is any value besides -20 the result will be a value other than 0. Values other than 0 are true.
you mean to do
if(choice1==1){morale+=20; std::cout << "blah blah blah";}


Very good point. So the code would compile assuming no other errors?

Yes, it would compile;
moral+20 alone as a statement would compile as well unless [-Wunused-value] were enabled, as it is with -Wall

1 Like

Proper legend thank u!