I'm making a program and I am making an 'if' statement, which I want to have many boolean expresions altogether using 'or' between them. But I can't make out how. My code:
if (input == "hello"* || input == "hi"*) {
output = "Hi!";
};
and the error I get:
Roxanne.vala:33.31-33.32: error: syntax error, expected identifier
if (input == "hello"* || input == "hi"*) {
^^
Compilation failed: 1 error(s), 0 warning(s)
What am I doing wrong?
There's a * after the strings "hello" and "hi". The compiler thinks you are trying to do multiplication.
Related
In the university I had a quiz today. The quiz is over but I can't understand some of its questions are their correct answers.
Note: I am not asking this to solve my quiz. Quiz is over I am just confused in some questions.
Question 1:
Consider the following variable declarations:
int catHeight = 6;
int dogHeight = 7;
string dogName = "Rover";
string catName = "Sylvester";
float catWeight = 15.0;
float dogWeight = 20.0;
bool dogRabies = true;
bool catRabies = false;
Choose Boolean expressions equivalent to following statements.
the cat has rabies and does not weigh 20 pounds or less
catRabies && catWeight > 20
!( catRabies && catWeight <=20)
! catRabies && catWeight >=20(This was marked as correct. I think the first option is correct)
the cat height and the dog height are not 10 (Hint: more than 1 answer)
catHeight > 10 && dogHeight >10
(catHeight && dogHeight) != 10
catHeight !=10 && dogHeight != 10
2nd and third are were marked as correct in result. But I think that only third one is correct. Please explain if I am wrong.
Question 2:
if numNeighbors >= 3 || numNeighbors = 4
++numNeighbors;
cout << "You are dead" << endl;
else
--numNeighbors;
What is wrong with the following if statement (there are at least 3 errors). The
indentation indicates the desired behavior
syntax error; else without previous if(marked as correct)
syntax error; value required of left operand (marked as correct)
syntax error; Parenthesis missing (marked as corrent)
syntax error; statement missing
I understand why 1 and 3 are correct but can't get the meaning of second one. Kindly explain it.
3 errors in question 2:
missing ( ) around the if condition
in the second part of the if condition there must be double ==
{ } are missing
To be a valid code it must be set like this:
if (numNeighbors >= 3 || numNeighbors == 4)
{
++numNeighbors;
cout << "You are dead" << endl;
}
else
--numNeighbors;
This was marked as correct. I think the first option is correct
Yes, you're right.
But I think that only third one is correct.
You're also right here.
Question 2
This one does not make sense unless you are trying to parse like a compiler. For instance, "else without previous if" only makes sense if you consider the current state of the code and not what you are trying to achieve. But the question tells you what you are trying to achieve.
syntax error; value required of left operand (marked as correct)
This means the condition is being parsed as (numNeighbors >= 3 || numNeighbors) = 4; which makes clear that the left side is not something you can assign to.
Your understanding of (1.1) and (1.2) seems to be correct.
In (2), if you fix the other errors,
if (numNeighbors >= 3 || numNeighbors = 4)
will be parsed as
if ((numNeighbors >= 3 || numNeighbors) = 4)
For this GCC outputs error: lvalue required as left operand of assignment, which reads similar to "value required of left operand".
program test
logical :: check
check = 2 < 3 < 5
print *, check
end program test
When trying to compile, it will give this error:
Error(s): source_file.f:5:0:
check = 2 < 3 < 5 1 Error: Unclassifiable statement at (1)
where is the problem?
you can test it online here: https://rextester.com/l/fortran_online_compiler
If you want to check that 2 is less then 3 and 3 is less then 5 then you really need two comparisons and one Boolean operator
check = (2 < 3) .and. (3 < 5)
I want to use one AND operator and 2 OR operators combined in a While loop, but I am getting an error in CPP.
while(vLessonNames.size>=1 && (log=='Y' || log=='y'))
I want to proceed when vector size is one or greater and log = Y or y
Error: invalid use of member (did you forget the '&' ?)|
I think your problem is that .size might be a function. So try to rewrite the statement like:
while( ( (vLessonNames.size() >=1) && (log=='Y' || log=='y') ) )
{...}
I am asking the user to input an expression which will be evaluated in postfix notation. The beginning of the expression is the variable name where the answer of the evaluated expression will be stored. Ex: A 4 5 * 6 + 2 * 1 – 6 / 4 2 + 3 * * = where A is the variable name and the equal sign means the answer to the expression will be stored in the variable A. The OUT A statement means that the number stored in the variable A will be printed out.
What I need help with is that when I input the second expression, I do not get the right answer. For example, my first expression A 4 5 * 6 + 2 * 1 – 6 / 4 2 + 3 * * = will evaluate to 153 and then when I input my second expression B A 10 * 35.50 + =, it has to evaluate to 1565.5, but it doesn't. It evaluates to 35.5. I cannot figure out why I am getting the wrong answer. Also, I need help with the OUT statement.
else if (isalpha(expr1[i]))
{
stackIt.push(mapVars1[expr1[i]]);
}
Will place the variable, or zero if the variable has not been set, onto the stack.
else if (isalpha(expr1[i]))
{
map<char, double>::iterator found = mapVars1.find(expr1[i]);
if (found != mapVars1.end())
{
stackIt.push(found->second);
}
else
{
// error message and exit loop
}
}
Is probably better.
Other suggestions:
Compilers are pretty sharp these days, but you may get a bit out of char cur = expr1[i]; and then using cur (or suitably descriptive variable name) in place of the remaining expr1[i]s in the loop.
Consider using isdigit instead of expr1[i] >= '0' && expr1[i] <= '9'
Test your code for expressions with multiple spaces in a row or a space after an operator. It looks like you will re-add the last number you parsed.
Test for input like 123a456. You might not like the result.
If spaces after each token in the expression are specified in the expression protocol, placing your input string into a stringstream will allow you to remove a great deal of your parsing code.
stringstream in(expr1);
string token;
while (in >> token)
{
if (token == "+" || token == "-'" || ...)
{
// operator code
}
else if (token == "=")
{
// equals code
}
else if (mapVars1.find(token) != mapVars1.end())
{
// push variable
}
else if (token.length() > 0)
{
char * endp;
double val = strtod(token.c_str(), &endp);
if (*endp == '\0')
{
// push val
}
}
}
To use previous symbol names in subsequent expressions add this to the if statements in your parsing loop:
else if (expr1[i] >= 'A' && expr1[i] <= 'Z')
{
stackIt.push(mapVars1[expr[i]]);
}
Also you need to pass mapVars by reference to accumulate its contents across Eval calls:
void Eval(string expr1, map<char, double> & mapVars1)
For the output (or any) other command I would recommend parsing the command token that's at the front of the string first. Then call different evaluators based on the command string. You are trying to check for OUT right now after you have already tried to evaluate the string as an arithmetic assignment command. You need to make that choice first.
I have absolutely no idea why my compiler is throwing this error:
"Error 1 error C2106: '=' : left operand must be l-value" with this line of code:
while ((input != 1 && input != 2 && input != 3 && choice = 1) ||
(input != 1 && input != 2 && input != 3 && input != 4 && input != 5 && input != 6 && choice = 1) ||
std::cin.fail()){}
The error appears to want input, which is a short, to be modifiable, but != is not a modifier, it's a checker. What is going on?
Operator precedence dictates that your first sub-condition
input != 1 && input != 2 && input != 3 && choice = 1
is parsed as
(input != 1 && input != 2 && input != 3 && choice) = 1
The left-hand side of = is not an lvalue, just like your compiler told you.
What were you trying to say by that strange combination of operators?
Your left side of (=) must be l-value, which means it must have adress in memory