none
Homework Help RRS feed

  • Question

  • Hello I am trying to write a code that would allow the program to give me a letter grade for an average of exam scores from A-F but I keep getting these 2 errors. Can someone help me figure this out?

    // This program calculates an average
    // of four exam grades.
    #include <iostream>
    #include <cmath>
    using namespace std;

    int main ()
    {
        double test1, test2, test3, test4; //To hold the grades
        double average; //To hold the average

        //Get the four exam scores.
        cout << "Please enter the first exam grade: ";
        cin >> test1;
        cout << "Please enter the second exam grade: ";
        cin >> test2;
        cout << "Please enter the third exam grade: ";
        cin >> test3;
        cout << "Please enter the fourth exam grade: ";
        cin >> test4;
        
        //Calculate the average of the exam grades.
        average = (test1 + test2 + test3 + test4) / 4.0;

        //Display the average.
        cout << "The average grade is: "<< average << endl;



    //This program uses an if/else if statement to assign a
    //letter grade (A, B, C, D, or F) to the average test score.



        //Constants for grade thresholds
        const int A_SCORE = 90,
                  B_SCORE = 80,
                  C_SCORE = 70,
                  D_SCORE = 60,
                  F_SCORE = 50;

        int average;  //To hold a numeric test score

        //Get the average test score.
        cout << "Enter the average test score and I will\n"
             << "tell you the letter grade: ";
        cin >> average;

        //Determine the letter grade.
        if (average >= A_SCORE)
            cout << "The grade is A.\n";
        else if (average >= B_SCORE)
            cout << "The grade is B.\n";
        else if (average >= C_SCORE)
            cout << "The grade is C.\n";
        else if (average >= D_SCORE)
            cout << "The grade is D.\n";
        else
            cout << "Your grade is F.\n";

        return 0;
    }

    The errors are:

    Error    1    error C2371: 'average' : redefinition; different basic types  

    Error    2    error C2088: '>>' : illegal for class   

    Sunday, September 22, 2013 9:42 PM

Answers

  • Search for "average" in your code and you'll see that you've defined it twice, first as a double, later as int.
    Sunday, September 22, 2013 10:10 PM

All replies

  • Search for "average" in your code and you'll see that you've defined it twice, first as a double, later as int.
    Sunday, September 22, 2013 10:10 PM
  • Waht part of error 1 is unclear.  You define a variable named average as a double at the top of your function and you attempt to define another variable with the same name as an int in the middle of the function.  You cannot redefine a variable or define it twice in the same scope.  Change the name of one of the variables in its definition and wherever it is referenced.

    Next time you post a question, please include the complete text of the error message.  You use the >> operator in several locations.  Without the line number it can be difficult to tell which of those the compiler is complaining about. 

    Sunday, September 22, 2013 10:10 PM
  • Sorry I forgot about that, the error is talking about this section of the code

    //Get the average test score.
        cout << "Enter the average test score and I will\n"
             << "tell you the letter grade: ";
        cin >> average;

    Sunday, September 22, 2013 10:48 PM
  • the error is talking about this section of the code

    //Get the average test score.
        cout << "Enter the average test score and I will\n"
             << "tell you the letter grade: ";
        cin >> average;

    The second error message is a result of the first error.
    Fix the first error and the second should disappear.

    - Wayne

    Sunday, September 22, 2013 11:19 PM
  • Thanks everyone for their help, I really appreciate it :)
    Sunday, September 22, 2013 11:29 PM