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â
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.