Still having problems with this one assignment loop/if/else statement

Here is the assignment below.

Topics



Loops

while Statement



Description

Write a program that computes the letter grades for the students in a class given their test scores. A student test score varies from 0 to 100. For each student, the program first asks the student’s name and then the student’s test score. It then displays the student name, test score and the letter grade. It repeats this process for each student.

The program calculates the letter grade from the test score as below:

A is 90 to 100 points

B is 80 to 89 points

C is 70 to 79 points

D is 60 to 69 points

F is 0 to 59 points.



/*

Java

*/

The user indicates the end of data by pressing the cancel button in the input dialog box when asked for a student name.



/*

C++

*/

The user indicates the end of data by entering two consecutive forward slashes ( // ) when asked for the name.





At the end, the program displays a summary report including the following:



· The total number of students.

· The total number of students receiving grade “A”.

· The total number of students receiving grade “B”.

· The total number of students receiving grade “C”.

· The total number of students receiving grade “D”.

· The total number of students receiving grade “F”.



Testing



Test Run 1

(User input is shown in bold).



Enter Student Name

Alan

Enter Student Score

75



Alan 75 C



Enter Student Name

Bob

Enter Student Score:

90



Bob 90 A





Enter Student Name

Cathy

Enter Student Score

80



Cathy 80 B



Enter Student Name

Dave

Enter Student Score:

55



Dave 55 F





Enter Student Name

Eve

Enter Student Score

85



Eve 85 B



Enter Student Name

(Java users, push the Cancel Button)

(C++ users, enter two consecutive forward slashes for the name).



Summary Report

Total Students count 5

A student count 1

B student count: 2

C student count 1

D student 0

F students 1


And here is the code below.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
 #include <string>

 using namespace std;


 int main( )
  {
    int acount=0, bcount=0, ccount=0, dcount=0, fcount=0;
    double score, totalStudents;
    string name;


    cout <<"Enter a name "<< endl;
    cin>> name;
    cout <<"Enter a score" << endl;
    cin>> score;


    while(name!="//")
    {



    if(score >=90.0)
     {


        acount = acount+1;
        cout<<name<<score<<endl;
     }


        else(score>=80.0)
       {


        bcount = bcount+1;
        cout<<name<<score<<endl;
       }



         else(score>=70.0)

         {


         ccount = ccount+1;
         cout<<name<<score<<endl;

         }


            else(score>=60.0)

            {


            dcount = dcount+1;
            cout<<name<<score<<endl;

            }


             else(score>=0-59.0)

             {


             fcount = fcount+1;
             cout<<name<<score<<endl;
             }
         }




        totalStudent = name



    cout << "Total Student count " << totalStudents <<endl;
    cout << "A Student count " << acount <<endl;
    cout << "B Student count " << bcount <<endl;
    cout << "C Student count "  << ccount <<endl;
    cout << "D Student count "  << dcount <<endl;
    cout << "F Student count "  << fcount <<endl;

  }


So, what exactly am I doing wrong? I must admit, some things about looping and if/else statement confuse me. My professor recommended the brackets wrapping around a piece of if/else code, but I get an error message from the compiler. Any thoughts? Suggestions?
this else(score>=80.0)

should be else if(score>=80.0)

Same for all the other ones

E: You are not incrementing totalStudents, so that will return some random value

Line 79:
You used a variable that was not declared totalStudent = name

E2: Line 14-17 should be inside your while loop and use getline(cin, name) instead of cin >> name
Last edited on
Ok, fixed some of the errors, but it is having a numerical conflict between 'double' and 'constant' char to binary operator<<

Not sure what to make of it. LOL, I'm totally lost.
Paste the new code :D
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
 #include <string>
 using namespace std;

 int main( )
  {
    int acount=0, bcount=0, ccount=0, dcount=0, fcount=0;
    double score;
    string name;
 

    while(name!="//")
    {
    cout <<"Enter a name "<< endl;
    cin>>name;
    cout <<"Enter a score" << endl;
    cin>>score;

    if(score>=90.0)
     {

        acount = acount+1;
        cout<<name<<""<<score<<"A"<<endl;
     }

        else if(score>=80.0)
       {

        bcount = bcount+1;
        cout<<name<<""<<score<<"B"<<endl;
       }
 
         else if(score>=70.0)
         {

         ccount = ccount+1;
         cout<<name<<""<<score<<"C"<<endl;
         }

            else if(score>=60.0)
            {

            dcount = dcount+1;
            cout<<name<<""<score<<"D"<<endl;
            }

             else if(score>=0-59.0)
             {

             fcount = fcount+1;
             cout<<name<<""<score<<"F"<<endl;
             }
         }
 
 
 
    cout << "Summary Report"<<endl;
    cout << "Total Students count 5"  <<endl;
    cout << "A Student count " << acount <<endl;
    cout << "B Student count " << bcount <<endl;
    cout << "C Student count "  << ccount <<endl;
    cout << "D Student count "  << dcount <<endl;
    cout << "F Student count "  << fcount <<endl;
  }


Here is the more recent code.
Inside the d and f count else-if blocks you're missing a < right before the word "score".

Also, your last else-if probably wants to be score < 60.
Last edited on
I don't know if this will be of any help but I wanted to give this assignment a go so here's the code:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <sstream>
#include <string>
#include <istream>
using namespace std;


int main(){

int acount=0, bcount=0, ccount=0, dcount=0, fcount=0;

double score;
string name;
int pamount = 0;
while(name != "//"){
    cout << "Enter a name: ";
    cin >> name;
    if(name == "//"){

        break;

    }
    cout << endl << "Enter a score: ";
    cin >> score;



    if(score >= 90.0){
            pamount++;
        acount++;
        cout << name << " " << score << " A" << endl;


    }
    else if((score <= 89.0), (score >=80.0)){
        pamount++;
        bcount++;
        cout << name << " " << score << " B" << endl;

    }
    else if((score <= 79.0), (score >= 70.0)){
    pamount++;
            ccount++;
            cout << name << " " << score << " C" << endl;


    }
    else if((score <= 69.0), (score >=60.0)){
    pamount++;
            dcount++;
            cout << name << " " << score << " D" << endl;

    }

    else if(score <=59.0){
    pamount++;
        fcount++;
        cout << name << " " << score << " F" << endl;

    }


}


cout << "Summary Report" << endl;
cout << "Total number of students: " << pamount << endl;
cout << "Total students receiving grade A: " << acount << endl;
cout << "Total students receiving grade B: " << bcount << endl;
cout << "Total students receiving grade C: " << ccount << endl;
cout << "Total students receiving grade D: " << dcount << endl;
cout << "Total students receiving grade F: " << fcount << endl;

}


I first tried using getline(cin,name); but it made the program skip the name input after the first input. No idea why :)
Also some of the includes may not be required ;)

I think that it works as it should.
Topic archived. No new replies allowed.