if condition on turbo C++ - c++

I'm having a problem regarding with if statement in C++ this statement is in do-while loop.
gotoxy(27,22);cout<<"Do you want to continue [Y]?";
sub=getche();
if(sub!='y' || sub!='Y')
{
gotoxy(27,24);cout<<"INVALID ANSWER!!";
gotoxy(27,26);cout<<"Closing Program....";
delay(3000);
exit(1);
}else
{
sub=ans;
}
}while(tolower(ans)=='y');
whenever I input y on the variable sub the code on if statement is still executing.. please someone tells me where is the error.. Thanks!

The boolean expression of (sub!='y' || sub!='Y') will always evaluate to true
This line:
if(sub!='y' || sub!='Y')
Needs to be this:
if ( (sub != 'y') && (sub != 'Y') )

Your code if(sub!='y' || sub!='Y') will be true ,no matter what you enter because eithersub!='y' or sub!='Y' will evaluate to true. Hence Use && instead of ||.

Related

Infinite Loop When Using Function Return Value as a Statement [duplicate]

This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 5 years ago.
Was making a small code for a text based game but i tried to do thinks a bit different than the tutorial sort of to test my understanding but i tried to get a function value to make a do while statement but it seems whether the statement false or true the code just keep looping infinitely im a beginner so pls if you have a time explain why the code is faulty and Thank you in Advance
The main function
int main() {
PrintIntroStart();
do {
PlayGame();
AskToPlayAgain();
} while (AskToPlayAgain() == true);
return 0;
}
the Bool Function
bool AskToPlayAgain(){
//Asking The Player Whether To Play Again Or Not
std::string PlayerResponse = "";
std::cout << "Do You Want To Play Again? (Yes/No)" << std::endl;
std::cin >> PlayerResponse;
if (PlayerResponse[0] == 'y' || 'Y')
return true;
else
return false;
}
Also, here you are asking twice to play again
do {
PlayGame();
AskToPlayAgain();
} while (AskToPlayAgain() == true);
this should be
do {
PlayGame();
} while (AskToPlayAgain() == true);
if (PlayerResponse[0] == 'y' || 'Y') this one should be
if (PlayerResponse[0] == 'y' || PlayerResponse[0] == 'Y')
Otherwise your if condition is always true, because 'Y' itself is non-zero.
And in fact you don't need this if statement, just
return PlayerResponse[0] == 'y' || PlayerResponse[0] == 'Y';
So first of all, you are only checking the outcome of the game every second play.
You should follow #ziza s answer and remove the function call inside the loop.
The second problem, that causes the infinite loop is your input check.
A bool value in c++ is not a single bit, but just anoter regular value that is evaluated as false if it is 0 and to true if it has any other value. In your case the first comparison will evauate to 1 if the player input is 'y' and the second part of the condition will be the value of the 'Y' character (which is not 0). So your condition will always be true like #liliscent stated.

Error expected primary-expression before '||' token

Currently trying to complete my C++ Assignment, the code isnt complete yet, but I was just trying to compile it and I ran into this error. I cant seem to figure out how to fix it, any pointers? Please excuse this super noobie question.. im still a beginner in an intro course.
Code
Your parentheses are messed up. An if statement condition in C++ should be wrapped in a parenthesis like so:
if ( animal == 'D' || animal == 'd' )...
if ( status == 'Y' || status == 'y' )...
else if ( status == 'N' || status == 'n')...
You if condition must be enclosed by a single bracket
if ((animal == 'D') || (animal == 'd'))
&
if((status == 'Y) || (status == 'y))
&
else if((status == 'N') || (status == 'n'))
Your If syntax is wrong, sweetie. You have missed the round brackets.
Should be:
if((status == 'N') || (status == 'n'))

Program works fine in Debug, but not in Release

I made a tic-tac-toe game which works good. Recently I decided to add a reset game feature.
Here is the part where I take input from user
void playerMove() //Gets the player move and updates the box variables
{
int boxnumber;
while(1) //Set's loop if the player enters a invalid input
{
cout << "\n Player " << player << "'s turn!(Press 0 to reset the game!): ";
cin >> boxnumber;
if(box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O' || boxnumber > 9 || boxnumber < 0) // Checks if the input is valid or not
{
system("CLS");//If invalid, show error and loop back to start to get new input
displayBoard();
cout << "\n Invalid Input! Try again!\n";
}else if(boxnumber == 0)
{
refreshGame();
displayBoard();
}else
{
box[boxnumber-1] = player;//If inputs are fine, set the box variable's and break out of loop!
break;
}
}
}
Now, in debug mode, when I press 0, everything runs fine and the game is reset, but in release build, when I press 0, it gives me the "Invalid Input! Try again!"
Things I have tried the didnt work:
-Rebuild the entire release and debug version.
-Making a new project and copy-pasting my code. Same thing, debug works, release doesnt.
For anyone wondering, I am using code::blocks IDE. The compiler is GNU GCC.
Thanks for the help! :)
You have undefined behavior.
In the "first" if you have box[boxnumber-1], so when you enter 0 (as you have stated in your question), you're trying to access element with index -1. That's UB, as you're reading "invalid" memory.
You should check for 0 first (and for negative numbers also).
In the if statement, put the range checks in front of the value checks. That is, change
if(box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O' || boxnumber > 9 || boxnumber < 0)
to
if(boxnumber < 0 || box number > 9 || box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O')
That takes advantage of short-circuiting: if the input value is less than 0 or greater than 9 the value checks won't be executed. That will avoid checking things like box[10], which isn't valid.
That still leaves a problem: if the user inputs 0, this code will happily check box[-1], which is also out of range. To get rid of this, move the branch of the if statement that tests box number == 0 in front of this part:
if (box number == 0)
// whatever
else if (box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O' || boxnumber > 9 || boxnumber < 0)
// whatever else

C++ setGender method reverts to default values

I've written the code bits below. I have a constructor which takes five arguments. Unfortunately, the setGender method spits out a default 'M' for all instances of a class rather than setting the gender to the specified parameter. What am I doing incorrectly? Any advice would be greatly appreciated. Thank you.
DateProfile::DateProfile(char gdr,
char searchGdr, int romanceScale, int financeScale, string theName)
bool DateProfile::setGender(char gdr)
{
if (gdr != 'M' || gdr != 'F')
return false;
gender = gdr;
return true;
}
if (gdr != 'M' || gdr != 'F') is always true, irrespective of input. If you're passing 'M', the second part of the expression is true. If you're passing anything else, the first part of the expression becomes true.
What you meant to write is if (gdr != 'M' && gdr != 'F') instead.
Increasing the warning level of your compiler may have helped you spot the error. Most compilers will warn about expressions always evaluating to a single value, or at least about the unreachable code following it.

if statement with multiple condition not working...

here is my code :
string function1( string input)
{
string output;
int i=0;
if (input.at(i)!='A' || input.at(i)!='a'|| input.at(i)!='E' || input.at(i)!='e' || input.at(i)!='I' || input.at(i)!='i' || input.at(i)!='O'||input.at(i)!='o' || input.at(i)!='U' || input.at(i)!='u')
{
char x=input[i];
input.erase(input.begin()+i);
output=input+x;
}
else
{
output=input+"yay";
}
return output;
}
but its not doing what i want it to do.. can't figure out where its going wrong...
can any1 help?
Basically the issue is that its never going in to the else statement..
if i pass in BJ it should return BJYAY right..
but its giving me JB
Thanks!
Change the || operators of the if statement to &&. The statement you have written is always true. What you want is that the first character is not a vowel, i.e. it does not match 'A' AND it does not match 'E', etc.
Changing the != to == will give you the result requested.