I was wondering if someone in the C++ community could help me out with a problem. I'm currently in the process of re-teaching myself C++. The textbook I'm currently using is Bjarne Stroustrup's Principles and Practice Using C++. My question concerns the use of while loops like the following:
#include "std_lib_facilities.h"
int main(void)
{
int num = 0;
int sum = 0;
cout<<"Please enter an integer or a non-numerical character to stop entering numbers.\n";
while (cin>>num) {
sum+=num;
cout<<"\nPlease enter another integer or a non-numerical character to stop entering numbers.\n"; }
if (sum == 0)
cout<<"\nThere is no sum because you didn't enter any numbers.\n";
else
cout<<"\nThe sum of the integers you entered is "<<sum<<".\n";
keep_window_open();
return(0);
}
Stroustrup is fond of using these kinds of while loops to enter data. The problem with the loop, however, is that once you enter a non-numerical character to exit the loop, the entire program shuts down and doesn't bother to execute any of the code after the while loop. The only exception occurs when the user enters the Control-Z command (cited in the textbook for Windows operating systems) to exit the loop. Strangely enough, doing this allows the rest of the program to execute smoothly, allowing the keep_window_open() function to keep the program window open as it was intended to.
So my question has two parts: (1) Why is this happening? and (2) What is the solution to the problem? I realize that there are other ways of constructing a while loop so the input statement isn't directly in the while loop condition. Unfortunately, however, Stroustrup encourages his readers to use the methods he's outlined throughout each chapter of his textbook. If someone could please provide a solution to this problem and explain in simple terms what's going on, I would greatly appreciate it. Being directed toward an online resource that explains what's going on would work as well. Thank you in advance.
> the entire program shuts down and doesn't bother to execute any of the code after the while loop.
wrong, the program terminates successfully.
I would suggest to execute your console programs from a console
when you input a non-numerical character the input fails.
the streams remains in an invalid state, so it's cleared to recover
However, the character was not processed, it remains in the buffer. So the next reading would capture it, instead of asking you for another one.
#include <bits/stdc++.h>
usingnamespace std;
//http://www.stroustrup.com/Programming/std_lib_facilities.hinlinevoid
keep_window_open()
{
cin.clear();
cout << "Please enter a character to exit\n";
char ch;
cin >> ch;
return;
}
int main() {
int num = 0;
int sum = 0;
cout<<"Please enter an integer or a non-numerical character to stop entering numbers.\n";
while (cin>>num) {
sum+=num;
cout<<"\nPlease enter another integer or a non-numerical character to stop entering numbers.\n";
}
if (sum == 0) cout<<"\nThere is no sum because you didn't enter any numbers.\n";
else cout<<"\nThe sum of the integers you entered is "<<sum<<".\n";
keep_window_open();
return(0);
}
Why is this happening?
This has to do with this statement cout<<"\nPlease enter another integer or a non-numerical character to stop entering numbers.\n"
What is the solution to the problem?
Change line 27 to contain the following: cin.clear(); cin.ignore(numeric_limits<int>::max(), '\n');
EXPLANATION:
When you entered the "non-numerical character to stop entering numbers" that character was left in the input buffer for use by the next call to cin >>. So in the keep_window_open function, you can see that it tries to read in a value, but since the character that was not extracted is still in the input buffer, it instead read that and continued execution. By implementing the solution I gave, you are telling the computer to clear any error flags raised by cin >> when reading values, then the next command beside it says to ignore the next 2147483647 characters until it hits a newline character. Hopefully the person did not enter more than 2147483647 characters otherwise this would not work; but if the user was not having finger spasms then this should safely allow the keep_window_open function to fully execute with you entering a character to exit the program.
> Change line 27 to contain the following: cin.clear(); cin.ignore(numeric_limits<int>::max(), '\n');
however if you indicate the end of the stream with ^Z (or ^D) then you'll need to enter a character twice to exit.
> but if the user was not having finger spasms
because only a user would be able to use your program, there are no such things as pipes or stream redirection.