View Single Post
Old Feb 26th, 2008, 5:10 PM   #19
wannabe7
Newbie
 
Join Date: Dec 2007
Posts: 16
Rep Power: 0 wannabe7 is on a distinguished road
Re: My programs aren't running...( no errors)

Quote:
Originally Posted by Ancient Dragon View Post
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.

cplusplus Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int length, width;
  7.  
  8. cout << "Enter the length: ";
  9. cin >> length;
  10. cin.ignore();
  11.  
  12. cout << "Enter the width: ";
  13. cin >> width;
  14. cin.ignore();
  15. cout << "The area is: ";
  16. cout << length * width;
  17. cin.get();
  18. return 0;
  19. }
Thank you, Ancient Dragon. I will certainly try this. I have used
cin.ignore();
before, but I guess I just forgot about it.
__________________
Aspiring Game Designer
wannabe7 is offline   Reply With Quote