Why is my Else If 'vehicleHeight' code being ignored? [duplicate] - c++

This question already has answers here:
Can you use 2 or more OR conditions in an if statement? [duplicate]
(9 answers)
Closed 2 years ago.
cout << "Are you driving a vehicle onto the ferry? (y/n) ";
cin >> vehicle;
if (vehicle == 'y' || vehicle == 'Y') {
cout << "What is the length of the vehicle in feet? ";
cin >> vehicleLength;
if (vehicleLength > 20) {
extraLengthCharge = (vehicleLength - 20) * extraLengthPrice; }
cout << "Is the vehicle over 7 feet high? (y/n) ";
cin >> vehicleHeight;
if (vehicleHeight == 'y' || 'Y') {
vehiclePrice = 69.00, fuelSurcharge = 10.40;
} else if (vehicleHeight == 'n' || 'N') {
vehiclePrice = 43.00, fuelSurcharge = 4.15;
}
else if (vehicle == 'n' || vehicle == 'N') {
vehiclePrice = 0, fuelSurcharge = 0; }
}
My code will give me the correct 'fare' if the response is yes, but if I type in no it will still assign the 'yes' value. I assume it has something to do with my else if statement format for vehicleHeight.
Thanks and sorry for the novice question.

The conditions vehicleHeight == 'y' || 'Y' and vehicleHeight == 'n' || 'N' will be always true because 'Y' and 'N' will be always true and logical OR is used with them.
You should use conditions vehicleHeight == 'y' || vehicleHeight == 'Y' and vehicleHeight == 'n' || vehicleHeight == 'N' like what you used in other if statements instead.

Related

why is else if function is not working in rock, paper, scissor?

I was asked to make a simple game of rock, paper, scissors in C++ but it turns out that my else if logic is not working. What do you think went wrong?
cout << "choose your pick..." << endl;
cout << "PRESS [ B ] if BATO(ROCK),[ G ] if GUNTING(SCISSOR) or [ P ] if PAPEL(PAPER) " << endl;
cin >> pick;
cin >> picke;
if ( (pick==picke) || (pick=='b' && picke=='B')
|| (pick=='B' && picke=='b') || (pick=='g' && picke=='G')
|| (pick=='G' && picke=='g') || (pick=='p' && picke=='P'))
{
cout << "SORRY TIE";
}
else if (pick=='p' || 'P' && pic­ke=='b' || 'B')
{
cout << "player 1 paper wins";
}
else if (pick=='b' || 'B' && pic­ke=='g' || 'G')
{
cout << "player 1 rocks wins";
}
else if (pick=='g' || 'G' && pic­ke=='p' || 'P')
{
cout << "player 1 scissor wins";
}
else if (pick=='b' || 'B' && pic­ke=='p' || 'P')
{
cout << "player 2 paper wins";
}
else if (pick=='g' || 'G' && pic­ke=='b' || 'B')
{
cout << "player 2 rocks wins";
}
else if (pick=='p' || 'P' && pic­ke=='g' || 'G')
{
cout << "player 2 scissor wins";
}
return 0;
Comparing like this: pick=='p' || 'P' && pic­ke=='b' || 'B' is pretty much useless. This is because the if statements condition will always be true. Why? Let me explain.
You cannot compare and check if pick contains either 'p' or 'P' like that. It sounds right in words but in C++ it is wrong. This is because in programming terms, it's like "is pick equal to 'p' OR is 'pick' equal to 'P'". You need to tell the compiler that you're checking if "is this equal to this". Not "is this equal to this and this" (like what you have right there).
If you have something like ... || 'p' && ..., it'll always be true, because the value of 'p' is not 0, meaning its true in boolean terms.
So to resolve this, we need to say "is pick equal to 'this'" every time we compare. So it may look like this,
//...
else if ((pick == 'p'|| pick == 'P') && (pic­ke == 'b'|| picke == 'B'))
{
cout << "player 1 paper wins";
}
else if ((pick == 'b'|| pick == 'B') && (pic­ke == 'g'|| picke == 'G'))
//...
Or else you can use tolower or toupper as #mch commented, to convert all the characters to a single case. It'll be easier and more efficient when comparing.
Note: It's better if you use brackets to segment the comparisons because it will not only will be easier to understand, it'll be correct logically too.

Conditional Statement Simplification - C++

What would the best way be of reducing my conditions in this code, is it to simply implement the Boost library for a string comparison or is there another way of doing it? I would much more prefer to avoid using boost if possible.
How could said question be implemented on the following code?
cout << " Please Enter An Answer Of Yes(Y) or No(N):";
string question; cin >> question;
if (question == "yes" || question == "Yes" || question == "Y" || question == "y" || question == "YEs" || question == "YES" || question == "yeS" || question == "yES")
{
cout << "You said yes!" << endl;
return 1;
}
else if (question == "No" || question == "NO" || question == "nO" || question == "N" || question == "n")
{
cout << "You said no!" <<endl;
return 0;
}
else
{
AskAQuestion();
}
It might not be the most efficent solution but if you convert the entire string to lower case then you only need to check against the word and the letter instead of all the possible permutation. So if you have
void make_lowercase(std::string& data)
{
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
}
Then
cout << " Please Enter An Answer Of Yes(Y) or No(N):";
string question; cin >> question;
if (question == "yes" || question == "Yes" || question == "Y" || question == "y" || question == "YEs" || question == "YES" || question == "yeS" || question == "yES")
//...
Becomes
cout << " Please Enter An Answer Of Yes(Y) or No(N):";
string question;
cin >> question;
make_lowercase(question);
if (question == "yes" || question == "y")
//...
The following does not do exactly the same since you did not check 'yEs' as a possible case in the first if statement, but this is probably along the lines what you are looking for. It is based on lower casing the answer of the user first before checking the answer, to reduce the number of cases to check.
#include <algorithm>
#include <cctype>
...
std::cout << "Please Enter An Answer Of Yes(Y) or No(N): ";
std::string answer; std::cin >> answer;
std::transform(answer.begin(), answer.end(), answer.begin(), std::tolower);
if (answer == "yes" || answer == "y")
{
std::cout << "You said yes!\n";
return 1;
}
else if (answer == "no" || answer == "n")
{
std::cout << "You said no!\n";
return 0;
}
else
{
AskAQuestion();
}
I took the liberty of renaming question to answer, seems more logical in this context.

Chars in do while loop conditions

I am having trouble with the last do while loop in my code. I have conditions set to stop the look when Y, N, y, or n are entered but even if those values are entered the loops continues to run and continue to ask for a Y or N. In debugging it seems that the Ascii value for the character is also stored in the variable? What do I need to change to have the do while loop end when there is an input of any of those 4 characters?
#include <string>
#include <iostream>
#include <iomanip>``
using namespace std;
int main()
{
int numberOfShapes, i, j, k, rectangleBase, rectangleHeight;
char star = '*';
char filled;
do
{
cout << "Enter the integer between 6 and 20 that you would like to be the base of the rectangle: ";
cin >> rectangleBase;
}while (rectangleBase < 6 || rectangleBase > 20);
rectangleHeight = rectangleBase / 2;
do
{
cout << "Enter the number of shapes you would like to draw(Greater than 0 and less than or equal to 10: ";
cin >> numberOfShapes;
} while (numberOfShapes <= 0 || numberOfShapes > 10);
do
{
cout << "Would you like a filled shape? [Y or N]: ";
cin >> filled;
} while (filled != 'Y' || filled != 'N' || filled != 'y' || filled != 'n');
Your loop end condition is wrong:
while (filled != 'Y' || filled != 'N' || filled != 'y' || filled != 'n');
consider that the value is 'y' then your condition will be:
(true || true || false || true)
which evaluates to true.
Change to:
while (filled != 'Y' && filled != 'N' && filled != 'y' && filled != 'n');
Then it will be:
-> 'y' (true && true && false && true) -> false
-> 'l' (true && true && true && true) -> true
You need to use && not ||:
} while (filled != 'Y' && filled != 'N' && filled != 'y' && filled != 'n');
If you write it as you say it, perhaps it would be more clear and will help to avoid these mistakes:
do
{
cout << "Would you like a filled shape? [Y or N]: ";
cin >> filled;
if (filled == 'Y' || filled == 'N' || filled == 'y' || filled == 'n')
break;
}
while (true);

Need to add a loop to exit or repeat in C++

I want to make a code that asks user for input either to quit or repeat. I am not getting the desired output.
int main()
{
char yes,yn;
do
{
cout << "Hello world!" << endl;
while(yn != 'y' || yn != 'Y' || yn != 'n' || yn != 'N')
{
cout<<"enter (Y/N): ";
cin>>yes;
yn = yes;
}
}while(yn == 'y' || yn == 'Y');
}
Program asks for user input, when I input "Y,y,N or n", it should get out of the loop and then decide whether exit or repeat. But it isn't getting out of the loop and asking me for input again and again. I am not getting what is stopping to get out of the loop.
I didn't test, but your issue seems to be here:
while(yn != 'y' || yn != 'Y' || yn != 'n' || yn != 'N')
This condition is always true (yet shouldn't). It should be :
while(yn != 'y' && yn != 'Y' && yn != 'n' && yn != 'N')
or
while(!(yn == 'y' || yn == 'Y' || yn == 'n' || yn == 'N'))
Moreover, yn isn't initiliazed, you may want to do a do { ... } while(...); again.
while(yn != 'y' || yn != 'Y' || yn != 'n' || yn != 'N')
Is always true. It should be
while(yn != 'y' && yn != 'Y' && yn != 'n' && yn != 'N')
Firstly, this expression:
(yn != 'y' || yn != 'Y' || yn != 'n' || yn != 'N')
is always true, as yn is always not equal to one of those things.
(yn != 'y' && yn != 'Y' && yn != 'n' && yn != 'N')
is probably what you meant.
Secondly, your life would be easier if you did
yes = toupper (yes);
(i.e. put it in upper case), then you'd only have to look for one thing.
Thirdly, and most seriously, your inner while examines the value of yes before it's been initialized. Either use a do while() construct, or intialize the value to something before the loop.
Fourthly, it's not clear what you are trying do to here. Do you really need two loops at all?
I think do while loop is all you need for the execution...
int main()
{
char yes,yn;
do
{
cout << "Hello world!" << endl;
cout<<"enter (Y/N): ";
cin>>yes;
yn = yes;
if(yn=='N'||yn=='n')
{
break;
}
}while(true);
}
your loop does not exit becuse your while loop is always true..
while(yn != 'y' || yn != 'Y' || yn != 'n' || yn != 'N')
Now I have added a break statement in your loop on a if condition and changed the do while condition
in this case it will run the loop on any condition and will exit the loop only when the user enter n or N
You did not initialize variable yn. So this statement
while(yn != 'y' || yn != 'Y' || yn != 'n' || yn != 'N')
has no sense and in fact it is unnecessary.
Try the following code
#include <iostream>
int main()
{
char yes_no;
do
{
std::cout << "\nHello world!" << std::endl;
std::cout<< "Do you want to repeat? (enter Y/N): ";
std::cin >> yes_no;
} while ( yes_no == 'y' || yes_no == 'Y' );
}
Thank you all for help. I have got what i wanted from all of you. Here is the final code running successfully. I have improved my code using suggestion from everyone of yours opinion.
int main()
{
char yes,yn;
do
{
.......... (main content of program, do things here.)
do
{
cout<<"Do again? (Y/N):";
cin>>yes;
yn = toupper(yes);
if(yn!='N' && yn != 'Y')
{
cout<<"Invalid selection"<<endl;
}
if(yn=='N' || yn == 'Y')
{
break;
}
}
while(true);
}
while(yn == 'Y');
}

Evaluating if statement with single char C++

I am trying to evaluate a single char:
bool repeat = true;
while (repeat)
//code
char x;
cout << "Would you like to tansfer another file? Y/N ";
cin >> x;
if (x == 'y' || x == 'Y')
repeat = true;
if (x == 'n' || x == 'N')
repeat = false;
else
throw "Input error";
I keep getting Input Error as my console output. Any ideas why? I can't get the while loop to repeat.
You are missing an else here:
if (x == 'n' || x == 'N')
should be:
else if (x == 'n' || x == 'N')
and you need to add braces after the while to encompass the input and if statements.
You forget braces {} after while:
while (repeat)
{
char x;
cout << "Would you like to tansfer another file? Y/N ";
cin >> x;
if (x == 'y' || x == 'Y')
repeat = true;
else
if (x == 'n' || x == 'N')
repeat = false;
else
throw "Input error";
}