If statement syntax error for beginner - c++

I'm teaching myself C++ and was writing some code to calculate grade averages with arrays. Everything works except for when I try to display the results. I get the error "expected primary-expression before '<' token" on this line of code.
if(grade1 >= 0 && < 60)
followed by a single cout statement and a semi-colon. I looked in the book I am using and on c++ forums. My book looks just like my example and online everyone was missing a semi-colon or something else. Is that my case too?
Thanks!

You need to include 'grade1' on both comparisons.
if (grade1 >= 0 && grade1 < 60)

What you want is this:
if(grade1 >= 0 && grade1 < 60)
You need to provide a variable for each condition in your if statement. The grade1 variable will not carry over for other comparisons.

You must write like this
if (grade1 >= 60 && grade1 < 60){ //code here }

Related

What am I misunderstanding when determining if a value is between two numbers?

I have a PDF form that adds up several different answers and displays the sum of these answer at the bottom of the page. I want to take that number, and have a sentence underneath it display three different options depending on the sum.
Greater than 60: Proceed
between 45 & 60: Consult Sales Lead
Less than 45: Decline
I am attempting to run a custom calculation script that takes the sum (which is named "total") and writes the above options, but I'm running into a myriad of errors.
My code I've written is below
var A = this.getField("total").value;
if (A >= 60){
event.value = "Proceed";
} else {
if (A <= 45){
event.value = "Decline";}
} else {
if (A < 60 && A > 45){
event.value = "Proceed, decision made with sales leader";}
}
If I were to only write the below block, I do not get any errors.
var A = this.getField("total").value;
if (A >= 60){
event.value = "Proceed";
}
I'm a newbie when it comes to most JavaScript, so any help is greatly appreciated! Thanks in advance!
I have based most of my code off of different search results from google. My main source
example 1
Below are a few other links I've referenced
example 2
example 3
You can leave away the
if (A < 60 && A > 45) {
line, because that condition is always true after the two previous conditions.
And then, it should work properly.
Just if you want to stay with that last condition, you would have to close it with a curly brace.
I think I managed to figure it out! For some reason, the addition of the "else" factor was causing syntax errors, so I tried it without and it seems to work as intended!
My code, for anyone that happens to find this, is the following:
var A = this.getField("total").value;
if (A >= 60) {event.value = "Proceed";}
if (A <= 59 && A >= 46) {event.value = "Proceed, decision made with sales leader";}
if (A <= 45) {event.value = "Decline";}
if (A == 0) {event.value = " ";}
Thanks to everyone that took a look at this, even if you didn't get to comment before I figured it out!

Are the answers of this C++ quiz correct?

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".

Why am I getting this error in if elif else

I am doing a school program again and when I enter <= on a range eg. >= 95 and <= 100 I get told the less than or = in <= is a syntax error.
since I can't type copy and paste the code without errors here this is the code in full so far
You need the variable in both paths of the expression: if var >= 96 and var <= 100:

C++ Multiple statements for conditional operator

I'm trying to use a conditional statement that does one thing in one condition but does two things if the other condition applies.
Consider the following:
( h >= 0 && h < 24 ? hour = h : hour = 0, cout << "Invalid Hour Detected\n")
If "h" is set to 25, it sets "hour" to 0 correctly.
If "h" is set to 12, it correctly sets "hour" to 12.
The problem is that it outputs "Invalid Hour Detected" for both true and false conditions.
I only want it to output if the conditions aren't met.
Essentially, I'm wondering if it is possible in a conditional statement to do two things for one condition.
Also tried:
( h >= 0 && h < 24 ? hour = h : hour = 0 && cout << "Invalid Hour Detected\n")
but that didn't run the cout on either case.
If you really want to do this, add proper parentheses and invert the order of the assignment and the output insertion (when using the comma operator, the value of the left expression is discarded):
( h >= 0 && h < 24 ) ? ( hour = h ) : (std::cout << "Invalid Hour Detected\n", hour = 0);
However, my advice is to make your code readable and abandon this kind of coding style.
I'm trying to use a conditional statement that does one thing in one condition but does two things if the other condition applies.
That's not a conditional statement, it's a conditional expression+. Conditional statement would be a lot more appropriate here from the readability perspective:
if( h >= 0 && h < 24) {
hour = h;
} else {
hour = 0;
cout << "Invalid Hour Detected\n";
}
+ C++ follows C in allowing use of standalone expressions as statements. That's why you can eventually "shoehorn" your solution into the right syntax by using parentheses and switching the order of operations. The readability of that solution suffers a lot compared to that of a plain, familiar if.
Try
( h >= 0 && h < 24 ? hour = h : (hour = 0 || cout << "Invalid Hour Detected\n"))
Or
( h >= 0 && h < 24 ? hour = h : (hour = 0 & cout << "Invalid Hour Detected\n"))
The comma operator has the lowest precedence of all the operators. Consequently, your expression is evaluated like this:
(( h >= 0 && h < 24 ? hour = h : hour = 0), cout << "Invalid Hour Detected\n")
You could express this in a conditional expression, with grouping and proper usage of the comma operator. But, if at all possible, it would be better to express this in an if/else statement. If you need to use it in an expression, consider placing it in a function.
Well I know that this is probably not the answer you are looking for but if you refactored a little this would be cleared up "naturally".
// Handy as a utility free function (in apt namespace)
bool isValidHour(unsigned int hour) {
return hour >= 0 && hour < 24;
}
isValidHour(h) ? hour = h : handleInvalidHour();
//...more
// If this is not in a class then you should pass "hour" as a reference.
void MyClass::handleInvalidHour() {
hour = 0;
cout << "Invalid Hour Detected\n";
}
Of course you should be using the new std::chrono stuff if possible. It's lovely and expressive.

How to use logical and in c++

if (age > 40 && age < 60)
or
if (age > 40 & age < 60)
As you can see I am not sure if C++ uses two and signs or one.
The first one is the correct Logical AND if (age > 40 && age < 60)
&& is a Logical AND operator
& - could be a Reference ("address of") operator or a Bitwise AND operator (which is the case in your second example) depending on where it appears in expression. Also, see Operators in C and C++.
Use this for the correct results:
if ((age > 40) && (age < 60))
It's &&, single ampersand is a bitwise and function. In a lot of cases it may work out the same if you are using ints, but there will be other data types that cause problems.