GPA Calculator!

In my programming class we are making a program to calculate GPA by different inputted semesters. i can't figure out how to get the user to input the name of the semester, like " Fall 2008 ". I know there are errors in my code right now with the string but i can't figure out what to do! I've never worked with striong before and I know I've used it in the wrong context. Your help is greatly appreciated. Here are my errors:

hw2.cpp: In function âint main()â:
hw2.cpp:31: error: no match for âoperator!=â in âSemName != -0x00000000000000001â
hw2.cpp:81: error: could not convert âSemName.std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](-0x00000000000000001)â to âboolâ
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

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

int main ()
{

cout << endl <<  "Please enter all the grades you wish to use for your GPA." << endl;

cout << endl <<  "When you have finished, enter N to indicate that you have no more grades." << endl;

cout << endl << "The highest acceptable grade is 100%, if you got higher than this congratulations but please enter only 100 as grade."  << endl;

cout << endl;

float Grade = 0;
int NumClass = 0;
const int Stop = -1;
float GPAVal = 0;
float TotGPAVal = 0;
float GPA = 0;
string SemName;

while (SemName != -1)
        {
        cout << "Please enter name of the semester of grades you want to enter." << endl;
        cin >> SemName;
        cout << SemName << endl;


        do {
                cout << endl << "Please enter grade: ";
                cin >> Grade;
                        if  ( Grade <=100 && Grade >= 90 )
                                {
                                GPAVal = 4.0;
                                cout << "Grade: " << Grade << "%; GPA Value: 4.0" << endl;
                                TotGPAVal = TotGPAVal + GPAVal;
                                }
                        else if ( Grade < 90 && Grade >= 80 )
                                {
                                GPAVal = 3.0;
                                cout << "Grade: " << Grade << "%; GPA Value: 3.0" << endl;
                                TotGPAVal = TotGPAVal + GPAVal;
                                }
                        else if ( Grade < 80 && Grade >= 70 )
                                {
                                GPAVal = 2.0;
                                cout << "Grade: " << Grade << "%; GPA Value: 2.0" << endl;
                                TotGPAVal = TotGPAVal + GPAVal;
                                }
                        else if ( Grade < 70 && Grade >= 60 )
                                {
                                GPAVal = 1.0;
                                cout << "Grade: " << Grade << "%; GPA Value: 1.0" << endl;
                                TotGPAVal = TotGPAVal + GPAVal;
                                }
                        else if ( Grade < 60 && Grade >= 0 )
                                {
                                GPAVal = 0.0;
                                cout << "Grade: " << Grade << "%; GPA Value: 0.0" << endl;
                                TotGPAVal = TotGPAVal + GPAVal;
                                }
                        else if ( Grade = Stop )
                                {
                                GPA = (TotGPAVal / NumClass);
                                cout << endl<< "Your overall GPA, after " << NumClass << " courses, is " << GPA << "." << endl << endl;
                                }
                NumClass = NumClass + 1;
                }
        while (( Grade >= 0 ) && ( Grade <=100 ));


        if (SemName = -1)
                {
                cout << "Thank you for using this program!" << endl;
                }
        }


return (0);
}

Firstly, you are comparing a string to a negative integer.

Secondly, in your program you have stated that "When you have finished, enter N to indicate that you have no more grades." and yet there is no check for 'N' anywhere in the code.

Thirdly, you are mixing buffered/unbuffered input. When using strings use the following syntax: getline(cin, SemName);

Lastly, line 75 is not evaluating whether SemName is equal to -1, its assigning (or trying to) -1 to SemName. Although, as you're using strings, you don't need to use "==" you can use SemName.compare() ... http://www.cplusplus.com/reference/string/string/compare/
Were you trying to do stringOne.compare(stringTwo) ? Then maybe there could be some sense in why you're comparing a string to a negative integer. Thinking about it though, that still wouldnt make sense. You will want stringOne.compare(stringTwo) == 0 to denote that they are the SAME string. Keep in mind that this comparison is case sensitive.


if (SemName = -1)
{
cout << "Thank you for using this program!" << endl;
}


This block will never execute as it appears to be interior to the block of code with while (SemName != -1) as its logic statement. Also, in that line of code you are actually ASSIGNING SemName to -1 in C++. You mean to write if(SemName == -1)

To make your life (and anyone else who reads your code) easier, place inline comments at the END of your logic statements like so:

if (SemName = -1)
{
cout << "Thank you for using this program!" << endl;
}//end if

SemName also doesnt ever appear to be initialized, so the comparison wont work. Try:

cout << "Please enter name of the semester of grades you want to enter." << endl;
cin >> SemName;

on the outside of your while statement, so that your while statement has something to actually consider.
Last edited on
cout << "Please enter name of the semester of grades you want to enter." << endl;
cin >> SemName;

on the outside of your while statement, so that your while statement has something to actually consider.

Don't use cin >> SemName;
Last edited on
getline(cin, SemName);
Topic archived. No new replies allowed.