Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
if (score[0] >= gradeA){
      cout << "student 0 score is " << score[0] << " and grade is A";
  }
  if (score[0] <=gradeA & >= gradeB){
      cout << "student 0 score is " << score[0] << " and grade is B";
  }
  if (score[0] <=gradeB & >= gradeC){
      cout << "student 0 score is " << score[0] << " and grade is C";
  }
  if (score[0] <=gradeC & >= gradeD){
      cout << "student 0 score is " << score[0] << " and grade is D";
  }
  if (score[0] <= gradeD){
      cout << "student 0 score is " << score[0] << " and grade is F";
  }


What I have tried:

tried putting && still got the same message
Posted
Updated 5-May-20 7:04am
v2

The boolean AND oprator is &&, not &.
& is the bitwise AND operator, which is not the same.
And you cannot infer the left operand for the second test, like you did. You have to write it nfor each test.
C++
if (score[0] >= gradeA) {
   cout << "student 0 score is " << score[0] << " and grade is A";
}
else if (score[0] >= gradeB) {
   cout << "student 0 score is " << score[0] << " and grade is B";
}
else if (score[0] >= gradeC) {
   cout << "student 0 score is " << score[0] << " and grade is C";
}
else if (score[0] >= gradeD) {
   cout << "student 0 score is " << score[0] << " and grade is D";
}
else {
   cout << "student 0 score is " << score[0] << " and grade is F";
}
 
Share this answer
 
v8
Comments
Bahooka 5-May-20 12:24pm    
just tried that and it still said the same message
phil.o 5-May-20 12:27pm    
Please see my modified answer.
CPallini 5-May-20 13:21pm    
5.
phil.o 5-May-20 13:33pm    
Thanks Carlo :)
Patrice T 5-May-20 13:22pm    
my 5 too
Quote:
Why do I get an expected primary expression before >=

Because there is no implicit repeat of score[0]
C++
// your code
if (score[0] <=gradeA & >= gradeB){
// is like writing
if ((score[0] <=gradeA) & ( >= gradeB)){
// and the correct syntax is
if (score[0] <=gradeA & score[0] >= gradeB){
// and you need to fix also the logical AND
if (score[0] <=gradeA && score[0] >= gradeB){

Another problem in your code: if score[0]=gradeA
C++
// if score[0]=gradeA
if (score[0] >= gradeA){ // this test will match
    cout << "student 0 score is " << score[0] << " and grade is A";
}
if (score[0] <=gradeA & score[0] >= gradeB){ // and this one too
    cout << "student 0 score is " << score[0] << " and grade is B";
}
// so code will say that student is grade A and gradeB

Code of solution 2 is a cure to this problem.
 
Share this answer
 
Comments
CPallini 5-May-20 13:21pm    
5.
Patrice T 5-May-20 13:22pm    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900