while(cin >> ) input in keyboard does not stop

I have already asked this type of question here:
[READ IT FOR MORE DETAILED QUESTION]

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?

Ur code works fine!
link : http://ideone.com/3whSPD

When I run the program in Windows XP, the command prompt doesnt stop accepting input and it doesnt terminate either. I am quite puzzled that some are saying it works fine.

On online judges or on online compilers(because they use linux complier) your code will run fine but on windows it will not stop at EOF.Here is the link http://stackoverflow.com/questions/7095581/actual-difference-between-end-of-line-and-end-of-file-under-windows

1 Like