Counting Grade program using switch, but chooses default without entering any grade?

Im using Visual studios here, heres my code:

GradeBook.h:


#include<string>
using namespace std;

class GradeBook
{
public:
GradeBook(string);
void setcoursename(string);
string getcoursename();
void displaymessage();
void inputgrades();
void displayGradeReport();
private:
string coursename;
int aCount;
int bCount;
int cCount;
int dCount;
int fCount;
};

cpp:
#include <iostream>
#include "GradeBook.h"

GradeBook::GradeBook(string name)
{
setcoursename(coursename);
}

void GradeBook::setcoursename(string name)
{
int aCount=0;
int bCount=0;
int cCount=0;
int dCount=0;
int fCount=0;

if(name.length()<=25)
{ coursename=name;}
else
{ coursename=name.substr(0,25);
cout<<"Coursename exceeds 25 characters, limiting to 25";
} }

string GradeBook::getcoursename()
{
return coursename;
}

void GradeBook::displaymessage(){
int grade;
cout<<"Enter Grade Letter, EOF to exit"<<endl;
while ( (grade=cin.get())!=EOF)
{
switch (grade)
{

case 'a':
case 'A':
++aCount;
break;
case 'b':
case'B':
++bCount;
break;
case'c':
case'C':
++cCount;
break;
case'd':
case'D':
++dCount;
break;
case'f':
case'F':
++fCount;
break;
default:
cout<<"Incorrect letter grade, enter a new one."<<endl;
}
}
}


void GradeBook::displayGradeReport()
{
cout
<<"/nA:"<<aCount
<<"/nB:"<<bCount
<<"/nC:"<<cCount
<<"/nD:"<<dCount
<<"/nF:"<<fCount
<<endl;
}




int main()
{
string coursename;
cout<<"Enter Coursename"<<endl;
cin>>coursename;
GradeBook gradebook1("coursename");
cout<<"Welcome to GradeBook for"<<gradebook1.getcoursename();
gradebook1.displaymessage();
gradebook1.displayGradeReport();
system("pause");
}
everytime i run it, right after i input the name of the course it automatically says incorrect grade, and everytime when i type something, such as a,or b it still says incorrect grade. Is there something wrong with my code? thanks
Try changing "int grade" to "char grade" in your displayMessage function
still not working, help?
If we change the loop to:

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
void GradeBook::displaymessage(){
    int grade;
    cout << "Enter Grade Letter, EOF to exit" << endl;
    while ((grade = cin.get()) != EOF)
    {
        switch (grade)
        {
        case 'a':
        case 'A':
            cout << "++aCount\n";
            ++aCount;
            break;
        case 'b':
        case'B':
            cout << "++bCount\n";
            ++bCount;
            break;
        case'c':
        case'C':
            cout << "++cCount\n" ;
            ++cCount;
            break;
        case'd':
        case'D':
            cout << "++dCount\n";
            ++dCount;
            break;
        case'f':
        case'F':
            cout << "++fCount\n";
            ++fCount;
            break;
        default:
            cout << "Incorrect letter grade, enter a new one." << endl;
        }
    }
}


does that help you figure out what's happening?
So what exactly is different?
closed account (j3Rz8vqX)
There is a possibility that cin.get() is retrieving the return_key buffer.
From when you entered the course name using cin.

I believe cin leaves a return_key buffer in the cache.

You should remove the last character buffer.

Try cin.get() after:
1
2
3
cout<<"Enter Coursename"<<endl;
cin>>coursename;
cin.get();


This should work, but I may just be completely wrong.
Last edited on
You'd probably want to use cin.ignore() rather than cin.get(). This shouldn't make a difference, but it is more intuitive (and probably slightly more efficient, though I wouldn't count on it).
Topic archived. No new replies allowed.