0

My code:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
double Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, Num10, Num11, Num12;
int StudentID;


 Num1= 97 * 0.3;
 Num2= 79 * 0.3;
 Num3= 86 * 0.4;
 Num4= 91 * 0.3;
 Num5= 78 * 0.3;
 Num6= 79 * 0.4;
 Num7= 73 * 0.3;
 Num8= 77 * 0.3;
 Num9= 82 * 0.4;
 Num10= Num1 + Num2 + Num3;
 Num11= Num4 + Num5 + Num6;
 Num12= Num7 + Num8 + Num9;

 cout << "Please enter a Student ID "<< endl;
 cin >> StudentID;
 {
 if (StudentID= 2046)
    cout << "The grade for student " << StudentID << " is: " << Num10 << endl;


else if (StudentID= 7634)
    cout << "The grade for student " << StudentID << " is: " << Num11 << endl;


else if (StudentID= 8120)
    cout << "The grade for student " << StudentID << " is: " << Num12 << endl;

else if (StudentID != 2046, 7634, 8120)
    cout << "I'm sorry that is not a valid StudentID " << endl;
}



return 0;
}

Needed output (when 2046 is input): Student id: 2046 Grade for student is 87.2

Output received: Student id: 2046 Grade for student is 87.2 I'm sorry that is not a valid id

What might the issue here be? Sorry if it's obvious I'm kind of a novice coder.

JRich29
  • 13
  • 3
  • 4
    `StudentID != 2046, 7634, 8120` ... do you know what the `,` means here? Anyhow you simply need an `else` in place of that last `else if` – 463035818_is_not_an_ai Dec 01 '17 at 14:58
  • 1
    Possible duplicate of [How does the Comma Operator work](https://stackoverflow.com/questions/54142/how-does-the-comma-operator-work) – 463035818_is_not_an_ai Dec 01 '17 at 14:58
  • 4
    `if (StudentID= 2046)` should be `if (StudentID == 2046)` - you're assigning the value 2046 to `StudentID` and then testing the result (same in a few other places) – Steve Dec 01 '17 at 14:59
  • 1
    If someone types "banana" as their student ID, older versions of C++ would not assign StudentID to anything - meaning attempting to read it is undefined behaviour. You could resolve this by either assigning a value to StudentID in its declaration (like you did with Num1 & others...) or by checking the cin error bit – UKMonkey Dec 01 '17 at 15:01
  • Upvoting because OP tried and posted a fairly minimal piece of code that demonstrated the problem – UKMonkey Dec 01 '17 at 15:03

1 Answers1

2

This condition:

if (StudentID = 2046)

always evaluates to true because the expression of StudentID = 2046 uses an assignment operator and (the entire expression) is always implicitly converted to true. It does not check if StudentID is equal to 2046. For that you need to use the equal to operator that is ==:

if (StudentID == 2046)

This condition:

if (StudentID != 2046, 7634, 8120)

is a comma separated expression that evaluates to the right-most value which is 8120 which is implicitly convertible to true. It does not check if the StudentID is not equal to all the listed numbers. To do that you would need to store the values inside a std::vector, std::array or similar and check if it matches any of them.

Ron
  • 14,674
  • 4
  • 34
  • 47