Trying to test if input is "TF" or "MC". The while condition keeps coming out as true even though argument is written: line != "TF" || line != "MC"
Not understanding how the loop keeps repeating even though I input TF or MC. I have also verified that the tranform method is making the string capital.
do {
cout << "\nEnter the Question type (TF) for True/False or (MC) for Multiple Choice:\n";
getline(cin, line);
transform(line.begin(), line.end(), line.begin(), ::toupper);
} while (line != "TF" || line != "MC");
I expected the loop to only initiate once and exit.
If the person types "MC", the expression
line != "TF"
will be set to true, which will make the OR statement true.
(And thus repeating the while even though the person typed a valid answer)
What you are looking for is to check if the answer is neither one of the options, which can be checked as follows:
(line != "TF" && line != "MC")
The opposite would be reasonable too. That is, to check if the person typed a valid answer, and keep repeating it while it is not the case:
while(!(line == "TF" || line == "MC"))
Both statements are equivalent, as stated in the comments, by the De Morgan's Laws.
This might be overkill, but it does explain the relationships between the OR, AND, and NOT operators: https://en.wikipedia.org/wiki/Boolean_algebra
Related
I'm trying to under the parse function for creating a formatter for a custom type in fmt. In their documentation (https://fmt.dev/dev/api.html) there is this line that has some sort of loop construct I haven't seen before:
auto it = ctx.begin(), end = ctx.end();
if (it != end && (*it == 'f' || *it == 'e')) presentation = *it++;
It's obviously a loop using iterators, presumably something new in C++17. What is it? Full example here: https://godbolt.org/z/fEGvaj
The formatter::parse function takes a parse context ctx and checks if the range [ctx.begin(), ctx.end()) contains format specifiers f or e in this example.
if (it != end && (*it == 'f' || *it == 'e')) presentation = *it++;
^ ^ ^
check if the check if the first
range is empty character is 'f' or 'e'
There is nothing particularly novel here, this code is compatible with C++98.
Excuse me for the use of the term "component", there probably is a better term to use in such context.
But moving on to my question, I want to use the else statement to execute a statement block based on the truth of the test statement. Here is the test statement I tried using:
else:
if ((reply != a) && (reply != c) && (reply =! e)):
I'm getting a syntax error, and the carrot is pointing at the first set of ampersands. I'm assuming now that I might be improperly using '&&'.
With this statement, my goal is to execute the statement block only if the test statement is true, meaning further, that 'reply' must not be equal to a, c, or e.
I know that I can use nested if's under the else statement, but I'm hoping StackExchange knows a better way. Thank you.
parenthesis check :
if (((reply != a) && (reply != c)) && (reply =! e))
I need to create a program that reads in a file, counts the words inside of it, and lists unique words with their frequency. The program considers any series of characters without spaces a word (so things like "hello." "hello" and ",.?" are all different words). I am having difficulty with using an if statement and adding a word at the end of the line to my word count. It counts the words that have spaces after them but not '/n'. This is the code I have for counting the words:
in.get(last);
in.get(current);
while(!in.eof())
{
if((current == ' ' && last != ' ') || (current == '/n' && last != ' ' && last != '/n'))
count++;
last = current;
in.get(current);
}
This is a painful way to do it... You are better off reading strings, which are automatically delimited by whitespace.
string word;
map<string,int> freq;
while( in >> word ) {
freq[word]++;
}
Note that in the example you gave, you used '/n', which should be '\n'. In my example, you don't even need it.
I would createca map,http://www.cplusplus.com/reference/map/map/, and if the word exists increment frequency otherwise add the word to the map.
This way you quickly check if the word exists, to have a unique list.
The following while loop has two conditions
cin >> user;
while (( user != 'X') || ( user != 'O'))
{
cout << "Please enter either X or O " << endl;
cin >> user;
}
After I enter either X or O, it keeps asking for a new input. I don't understand why? But if I remove on of the conditions it works properly.
Think about the logic of "this thing is not X, or this thing is not Y" — barring overlaps between X and Y, such a condition is always true, even in English!
You've been misled by the colloquial and subtly different "this thing is neither X nor Y", but your code is not "neither X nor Y", but "not X or not Y".
What you meant was "not X and not Y".
while (( user != 'X') && ( user != 'O'))
Use && (and) instead of || (or).
Your condition is always true...
while (( user != 'X') || ( user != 'O'))
If user is 'X' then user is not 'O' thus, even if the first part of the condition is not satisfied, the second part of the condition is satisfied. So the whole condition is true.
Same thing if useris 'O'.
Try this with a "logical and" (a.k.a. &&) instead of a "logical or".
It looks like you want logical AND (&&) instead of logical OR (||).
If you want to understand what happens more intuitively, reverse the logic of your expression:
!= becomes ==, and || (or) becomes && (and).
So what you wrote is:
Quit the loop if user equals 'X' AND user equals 'O'.
As you can see it's impossible for user to have both values at the same time.
What you want is:
while (( user != 'X') && ( user != 'O'))
{
cout << "Please enter either X or O " << endl;
cin >> user;
}
Well, || means or. If you read the condition aloud to yourself, it would sound like "While user is not X or user is not O. If you think about it, it will always be true - when it is X, it is also not O, and when it is O, it is not X. What you probably need is &&:
while (( user != 'X') && ( user != 'O'))
That way, the loop will stop when user is neither X nor O.
Thats because you're using the OR operator.
You're asking if user is not X or user is not O keep asking for input.
change it to && operator
I have a string vector of user-input data containing strings. Now I need to make sure program won't execute if strings are different than specified few. Vector contains 4 fields and every has different condition:
vector[0] can only be "1" or "0"
vector[1] can only be "red" or "green
vector[2] can only be "1", "2" or "3"
vector[3] can only be "1" or "0"
I tried writing if for every condition:
if(tokens[0]!="1" || tokens[0]!="0"){
decy = "error";
}
else if(tokens[1]!="red" || tokens[1]!="green"){
decy = "error";
}
else if(tokens[2]!="1" || tokens[2]!="2" || tokens[2]!="3"){
decy = "error";
}
else if(tokens[3]!="1" || tokens[3]!="0"){
decy = "error";
}
else{
switch(){} //working code
}
return decy;
It always enters first if and returns error. I tried with if instead of else if but it doesn't work either. I checked vector[i] contents and it returns correct strings. No " " at the end of it etc. Removing else and releasing switch just makes program check first condition and ignore rest of it.
I'm probably doing something terribly wrong, but I can't find an answer on internet so I decided to ask here.
This line:
if(tokens[0]!="1" || tokens[0]!="0")
should be:
if(tokens[0]!="1" && tokens[0]!="0")
^^
The same goes for the rest of the if statements as well.
The conditions are invalid.
Any distinct value can satisfy your conditions.
You should use && instead of ||.
For example:
if (tokens[0] != "1" || tokens[0] != "0") {
Consider this line. If tokens[0] is "1", which is valid input, it will not satisfy the first condition, but it will satisfy the second. You only want to throw an error when the value is neither of the valid possible inputs.
This means that your condition should be:
if (tokens[0] != "1" && tokens[0] != "0") {
Same goes for all the others.
You should turn those || into &&. If the input can only be X or Y, this means that it is illegal when it is not X and not Y:
if (tokens[0] != "1" && tokens [0] !="0")
// ^^
The first if:
if(tokens[0]!="1" || tokens[0]!="0")
ALWAYS evaluates to true.