View Single Post
Old Feb 24th, 2008, 5:40 PM   #17
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 544
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: My programs aren't running...( no errors)

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. }
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr.
Ancient Dragon is offline   Reply With Quote