Array question

hi , I am trying some practice problems and I am stuck
The output should look like this
Enter the number of students
Enter 4 scores :
Student 0 score is 40 and grade is C
Student 1 score is 55 and grade is B
Student 2 score is 40 and grade is A
Student 3 score is 55 and grade is B

This is my code but I cant to get the letter part , What am I doing wrong??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main(){

int student=10;
int number[student]; 
char grade; 

cout << " Enter the number of students: "; 
cin >> student; 
cout << "Enter" <<" " << student << " " <<"scores: "; 
for (int i=0;  i<student; i++ ){
cin >> number[i]; 
}
  
for (int i=0;  i<student; i++ ){
  grade = number[i];
if (number[i]<=40){
(grade =='C');

}
cout<<"Student "<<i<<" score is "<<number[i]<<" and grade is "<<grade<<endl;
}
  return 0; 
}
Last edited on
Use =, not ==
1
2
3
if (number[i]<=40){
    grade ='C';
ok so I did some fixin's but confused why " else if " is causing errors!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

int main(){

int student=10;
int number[student]; 
char grade; 

cout << " Enter the number of students: "; 
cin >> student; 
cout << "Enter" <<" " << student << " " <<"scores: "; 
for (int i=0;  i<student; i++ ){
cin >> number[i]; 
}
  
for (int i=0;  i<student; i++ ){
  grade = number[i];
if (number[i]<=40){
grade =  'C';
else if  (number[i]>40 || number[i]<=55)
grade =  'B';
else if (number[i]>55 || number[i]<=70)
grade =  'A'; 
}
cout<<"Student "<<i<<" score is "<<number[i]<<" and grade is "<<grade<<endl;
}
  return 0; 
}
You need to match your braces up properly.

You need to indent your code so you can see how things flow.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int main()
{

  int student = 10;
  int number[student];
  char grade;

  cout << " Enter the number of students: ";
  cin >> student;
  cout << "Enter" << " " << student << " " << "scores: ";
  for (int i = 0; i < student; i++) {
    cin >> number[i];
  }

  for (int i = 0; i < student; i++) {
    grade = number[i];
    if (number[i] <= 40) {
      grade = 'C';
    }
    else if (number[i] > 40 || number[i] <= 55) {
      grade = 'B';
    }
    else if (number[i] > 55 || number[i] <= 70) {
      grade = 'A';
    }
    cout << "Student " << i << " score is " << number[i] << " and grade is " << grade << endl;
  }

  return 0;
}


I'll leave you to figure out the difference between || and &&
haha || is or and && is and , anyways thanks dawg.
@Sunnycoder

and along with or are legal keywords in C++, if you find that easier.
To paraphrase:

"its C++, but not as we know it"
Student 0 score is 40 and grade is C
...
Student 2 score is 40 and grade is A


Oh come on! That ain't fair!
Topic archived. No new replies allowed.