Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › cin and looping problem, C++
New Posts  All Forums:Forum Nav:

cin and looping problem, C++

post #1 of 2
Thread Starter 
*dont post, barbaroti helped*



hello, overclock.net
i am doing problems from my textbook and one of them requires me to make a program that asks for the input of ones grades and units and using these two things, it will give them their gpa. to exit the program the user has to press '*'. however there is a problem. when the user enters 1 grade, such as 'a' it works perfect, but if the user accidentally types "abc" or "fekq2eqwda", then things get weird. in the book we are only able to compare characters at this point and we can't use arrays. the output and formula is correct since the output matches up with the book's, its just that part that i can't fix.

here is the code
Code:
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

int main()
{
        char letter_grade = '0';
        float units = 0, unit_counter = 0, numerical_grade = 0, sum = 0;
        string full_name;

        cout<<fixed<<setprecision(2)<<"Enter the student's name: ";
        getline(cin, full_name);

        while(letter_grade != '*')
        {
                cout<<"Enter a grade or '*' to terminate: ";
                cin>>letter_grade;
                switch(letter_grade)
                {
                        case 'a':
                        case 'A':       numerical_grade = 4;
                                                break;
                        case 'b':
                        case 'B':       numerical_grade = 3;
                                                break;
                        case 'c':
                        case 'C':       numerical_grade = 2;
                                                break;
                        case 'd':
                        case 'D':       numerical_grade = 1;                                                    
                                                break;
                        case 'f':
                        case 'F':       numerical_grade = 0;
                                                break;
                        case '*':       numerical_grade = -1;
                                                break;
                        default:        cout<<"You entered an invalid letter grade."<<endl;
                                                numerical_grade = -1;
                                                break;
                }

                if(numerical_grade >= 0 && numerical_grade <=5)
                {
                        units = 0;
                        while(units < 1 || units > 5)
                        {
                                cout<<"Enter number of units: ";
                                cin>>units;
                                if(!cin)
                                {
                                        cout<<"You entered the wrong data type for units."<<endl;
                                        return 0;
                                }
                                if(units < 1 || units > 5)
                                {
                                        cout<<"Please enter an amount of units between 1 and 5 inclusive."<<endl;
                                }
                        }
                        unit_counter+=units;
                        sum += (units * numerical_grade);
                }
        }
        if(unit_counter > 0)
        {
                cout<<"The GPA for "<<full_name<<" is "<<sum/unit_counter<<endl;
        }
        return 0;
}



Edited by ironman1478 - 2/18/12 at 12:05am
my computer
(13 items)
 
  
CPUMotherboardGraphicsRAM
phenom II X4 955 Gigabyte GTX 480 ZOTAC AMP! 4gigs G.Skill ddr3 1333mhz 
Hard DriveOSMonitorPower
500 gb WD caviar Black Windows 7 premium 64 bit 20 inch Acer 750w corsair 
Case
coolermaster cm690 
  hide details  
Reply
my computer
(13 items)
 
  
CPUMotherboardGraphicsRAM
phenom II X4 955 Gigabyte GTX 480 ZOTAC AMP! 4gigs G.Skill ddr3 1333mhz 
Hard DriveOSMonitorPower
500 gb WD caviar Black Windows 7 premium 64 bit 20 inch Acer 750w corsair 
Case
coolermaster cm690 
  hide details  
Reply
post #2 of 2
Well, the actual problem is that letter_grade is a char, which can only store 1 letter/number, for example 'a', if you input "abc", then it would take 'a' as letter grade and then 'b' as the number of units which is the next variable read it does.

To make the code work better, I would change letter_grade to a string, so if the user inputs "abc", then it will be read as such, however that would mean you will need to change your switch statement for if/else statements as c++ doesn't support a switch with string.

EDIT: This should work, altough I'm not sure if you MUST use switch/char
Code:
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

int main()
{
        float units = 0, unit_counter = 0, numerical_grade = 0, sum = 0;
        string full_name, letter_grade = "0";

        cout<<fixed<<setprecision(2)<<"Enter the student's name: ";
        getline(cin, full_name);

        while(letter_grade != "*")
        {
                cout<<"Enter a grade or '*' to terminate: ";
                cin>>letter_grade;
                if(letter_grade == "a" || letter_grade == "A")
                    numerical_grade = 4;
                else if(letter_grade == "b" || letter_grade == "B")
                    numerical_grade = 3;
                else if(letter_grade == "c" || letter_grade == "C")
                    numerical_grade = 2;
                else if(letter_grade == "d" || letter_grade == "D")
                    numerical_grade = 1;
                else if(letter_grade == "f" || letter_grade == "F")
                    numerical_grade = 0;
                else if(letter_grade == "*")
                    numerical_grade = -1;
                else
                {
                    cout<<"You entered an invalid letter grade."<<endl;
                    numerical_grade = -1;
                }
                if(numerical_grade >= 0 && numerical_grade <=5)
                {
                        units = 0;
                        while(units < 1 || units > 5)
                        {
                                cout<<"Enter number of units: ";
                                cin>>units;
                                if(!cin)
                                {
                                        cout<<"You entered the wrong data type for units."<<endl;
                                        return 0;
                                }
                                if(units < 1 || units > 5)
                                {
                                        cout<<"Please enter an amount of units between 1 and 5 inclusive."<<endl;
                                }
                        }
                        unit_counter+=units;
                        sum += (units * numerical_grade);
                }
        }
        if(unit_counter > 0)
        {
                cout<<"The GPA for "<<full_name<<" is "<<sum/unit_counter<<endl;
        }
        return 0;
}

Edited by Barbaroti - 2/18/12 at 12:08am
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › cin and looping problem, C++