CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Count Grades

  1. #1
    Join Date
    Mar 2008
    Posts
    55

    Count Grades

    My assignment was to create a program that inputs letter grades (A, B, C, D, F), counts how many of each they are, and outputs the results.

    Here is what I have so far:

    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {string grade;
    int gradeA=0, gradeB=0, gradeC=0, gradeD=0, gradeF=0;
    cout<<"Please enter a letter grade or '-1' to quit: ";
    cin>>grade;

    while(grade!=-1)
    {if(grade==A)
    gradeA++;
    else if(grade==B)
    gradeB++;
    else if(grade==C)
    gradeC++;
    else if(grade==D)
    gradeD++;
    else
    gradeF++;
    cout<<"Please enter another letter grade or '-1' to quit: ";
    cin>>grade;
    }

    cout<<endl<<"The total number of letter grades are:"<<endl;
    cout<<gradeA<<" As."<<endl;
    cout<<gradeB<<" Bs."<<endl;
    cout<<gradeC<<" Cs."<<endl;
    cout<<gradeD<<" Ds."<<endl;
    cout<<gradeF<<" Fs."<<endl<<endl;
    return 0;
    }

    I believe my problem is with this line:
    while(grade!=-1)

    Can anyone help me understand my mistakes? Thanks!

  2. #2
    Join Date
    Jun 2007
    Posts
    29

    Re: Count Grades

    You need to compare grade, which is a string, to other strings.
    Code:
    while (grade != -1)
    That should be
    Code:
    while (grade != "-1")
    And so on for the letters.

    Also, a more logical keypress for the user to quit would be Q. -1 is thinking in programmer's terms, not end-user terms.

  3. #3
    Join Date
    Mar 2008
    Posts
    55

    Re: Count Grades

    Thanks for the speedy reply. That did the trick. The assignment asked us to use '-1'. Thanks again!

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Count Grades

    Also, use white space for readability. Your code is kind of cluttered. In a bigger project it will be hard to read.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured