So I have a project for school that requires me to wait for the user to input Ctrl + Z to end a loop. The teacher said the best way to do this is to check for cin >> x
and if this is true than they haven't entered in Ctrl + Z yet. Well after a bit of testing and going back and forth I couldn't figure out what the problem was, so I made a super simple version of the code to see if I could fix it. Nothing really changed at all. Anyway here is the simple code:
#include "Proj_02.h";
vector<int> dog;
string entry = "";
int value = 0;
void main()
{
Menu();
}
void Menu()
{
do
{
//Ask the user to enter a number
cout << "Enter a number: ";
//Save the number to a vector
do
{
cout << "k";
getline(cin, entry);
value = atoi(entry.c_str());
}while(value == 0);
while (cin >> value)
{
cout << "L";
dog.push_back(value);
}
//when the user presses Ctrl + Z stop asking
}while(cin >> entry);
//Display all of the numbers
for(int i = 0;i < dog.size();i++)
{
cout << dog.at(i) << endl;
}
system("PAUSE");
}
So what happens when this is run is the code waits for me to enter 2 more values before even doing anything after any entry is made at all. My guess is it has something to do with the while cin >> entry
causing some sort of buffer interference, but I don't really have any solid ideas on how to fix this. If anyone could help that would be amazing.