I have one last question. In my last program that I wrote I had a statement which looked like this:
Code:
stack1.cc = 0;
while(getline(cin, stack1.aa[stack1.cc]))
{
if(stack1.aa[stack1.cc].length() > 80)
cerr "Too long";
else if
etc
else
cerr "Bad input";
}
// end of program
When reading from a file reached eof the while loop would be finished and the program terminal window would close, or if there was no input file specified and the input was coming from the keyboard instead, if the user just hit return without typing anything the program would exit, too. Now, I have what I thought was the exact same loop structure in this program, and it exits fine when reading from a file but when keyboard input is supplied the program won't exit if the user just hits enter (thus submitting a null value).
Code:
int main(int argc, char *argv[])
{
queue myQueue;
string temp, holder;
int lncount;
lncount = 0;
while(getline(cin, temp))
{
lncount++;
if(cin.bad())
{
cerr << "Bad input on line " << lncount;
exit(1);
}
if(temp[0] == '>') // push
{
temp = temp.substr(1, temp.length()); // remove '>'
temp += "\n";
if(!(myQueue.push(temp)))
{
cerr << "No memory for allocating queue element, current queue size is " << myQueue.queueElements << ".\n";
exit(1);
}
}
else if(temp[0] == '<') // pop
{
holder = myQueue.pop();
if(myQueue.err())
cerr << "Empty queue.\n";
else
cout << holder;
}
else // bad input
{
cerr << "Bad input at line " << lncount << ".\n";
}
// reset error status
myQueue.errExists = false;
}
system("Pause");
return 0;
}
Any idea to why it won't exit through keyboard input?