I have already asked this type of question here:
[READ IT FOR MORE DETAILED QUESTION]
http://discuss.codechef.com/questions/54428/newbie-question-c
and i was advised that when there is nothing to be read, the while loop will break.
But that doesnt work for me. When i open the program, type the input, and when im finished with the input I just keep pressing enter and the program doesnt stop. It keeps on accepting nothing.
the inputs are
A 3
B 5
B+ 3
C 3
the expected output is 3.10714
and here’s my code
#include
#include
using namespace std;
int main()
{
string letterGrade;
int units;
float pointValue = 0, prod1 = 0, sum = 0, product = 0;
while(cin >> letterGrade >> units)
{
if(letterGrade[0] == 'A')
{
pointValue += 4;
}
else if(letterGrade[0] == 'B')
{
if(letterGrade[1] == '+')
{
pointValue += 3.5;
}
else
pointValue += 3;
}
else if(letterGrade[0] == 'C')
{
if(letterGrade[1] == '+')
{
pointValue += 2.5;
}
else
pointValue += 2;
}
else if(letterGrade[0] == 'D')
{
pointValue += 1;
}
product = pointValue * units;
pointValue = 0;
prod1 += product;
sum += units;
}
cout << prod1/sum << endl;
return 0;
}
I am not allowed to use advance methods either. Any advice?