I wrote this condition. I am using C.
if ((ch < 'A') || (ch > 'Z'));
But I was wondering, is there another way how to write this condition?
Related
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'))
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.
I have an issue with my for loop. It goes through two arrays and compares the account number and PIN. An if statement checks if the combination of account number and PIN are correct. Since I changed this part (user_Account != accounts[i] || user_Pin != pins[i]) from the original (user_Account == accounts[i] && user_Pin != pins[i]), this happens. Before that it worked perfectly, but I was afraid that if someone types wrong account, then the program might crash so I made that change. Here is part of the code, I can post more if needed. Please have one thing in mind I am in beginning class, so no advanced changes or recommendations, I need something on my current level. I appreciate any help.
for (int i = 0; i < 6; i++)
{
if(user_Account == accounts[i] && user_Pin == pins[i])
{
cout << "You entered correct combination of account and pin number." << endl;
}
else if(user_Account != accounts[i] || user_Pin != pins[i])
{
cout << "You entered wrong account and/or wrong pin number. Please start over." << endl;
return 0;
}
}
When you converted from
(user_Account == accounts[i] && user_Pin != pins[i])
to
(user_Account != accounts[i] || user_Pin != pins[i])
you should have converted to:
(user_Account != accounts[i] || user_Pin == pins[i])
You are actually applying something called De Morgan's laws, and it's worth reading up on them for the history at least :).
Regarding if you should have separate if-else clauses for account fail compared to PIN fail - that's kind of a design decision. If someone was trying to break into your 'bank' and was guessing both the PIN and the account number from, say, looking over someone's shoulder, then seperate error messages would help him/her find the correct combination more quickly. On the other hand, if (more likely) a customer has just make a typing error, it's nice to have a message telling them. It's pretty much up to you.
If there are only two cases:
Both account id and password match
or
At least one doesn't match
I would just save yourself the headache of a second condition and let the check be this:
if (user_Account == accounts[i] && user_Pin == pins[i])
// accounts match message
else
// accounts don't match message
Also, a pointer on your for loop: you might want to make sure the for loop doesn't keep going when one of the arrays runs out, so your for loop signature could be this:
for (int i = 0; (i < accounts::size) && (i < pins::size); i++)
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 ||.
if (checkForRoll == "intellect" && checkForRoll == "Intellect") {//checks for intellect
intellect = intellect + 5;
} else if (checkForRoll == "strength" && checkForRoll == "Strength") {
strength = strength + 5;
}
cout << intellect;
When I execute this, the intellect int does not add by 5. Why?
You are requiring your string to equal both intellect and Intellect which is impossible. Change the "and" (&&) to an "or" (||).
Your variable checkForRoll can't be 'strength' && 'Strength', it can be 'strength' || 'Strength' however.
CheckForRoll cannot be both "intellect" and "Intellect".
Assuming you are using a std::string convert to upppercase, and do one comparision.
std:string checkForRoll = "inTeLleCt";
std::transform(checkForRoll.begin() , checkForRoll.end() , checkForRoll.begin(), ::toupper);
if (checkForRoll == "INTELLECT")
{
....
}
This will only work of checkForRoll is a string
If it is a char* then you can't test for equality with ==. You need to use strcmp() to compare.
To make your life easier, I suggest you use std::toupper or std::tolower to convert the string before you compare. See also std::transform.
This simplifies your life by using only one comparison.
"Try it, you'll like it!"
I think you should replace && with ||
P.S. you could also write intellect += 5