Quote:
Originally Posted by Ancient Dragon
The proglem is that when you use cin to input an integer the '\n' (Enter key) remains in the keyboard buffer, so you have to remove it or the next cin will fail. I solve this by adding cin.ignore(). after each cin. This is not a foolproof solution -- you can still break the program by typing junk characters after the number you want to enter and before hitting the Return key.
Here is a working program using Dev-C++ compiler.
#include <iostream> using namespace std; int main() { int length, width; cout << "Enter the length: "; cin >> length; cin.ignore(); cout << "Enter the width: "; cin >> width; cin.ignore(); cout << "The area is: "; cout << length * width; cin.get(); return 0; }
|
Thank you, Ancient Dragon. I will certainly try this. I have used
before, but I guess I just forgot about it.