What I have here is a loop that is supposed to read the output on a piece of equipment every 500 milliseconds. This part works fine. However, when I try to introduce cin.get to pick up the key "n" being pressed to stop the loop, I only get as many outputs as the number of key presses up to this point. If I press any keys (apart from 'n') several times followed by enter, I will get a few more outputs. What I need is the loop keep looping without any interaction until I want it to stop.
Here's the code:
for(;;)
{
count1++;
Sleep(500);
analogInput = ReadAnalogChannel(1) / 51.0;
cout << count1*0.5 << " " << analogInput << endl;
outputFile << count1*0.5 << ", " << analogInput << endl;
if (cin.get() == 'n') //PROBLEM STARTS WITH THIS INTRODUCED
break;
};
My output is as follows (there are 2 key presses to get to this stage in the program) unless I press a few more keys followed by enter:
0.5 0 // as expected
1 2 // as expected
should be more values until stopped
I have no particular preference in which type of loop to use, as long as it works.
Thanks!