candyapplecorn

Use of CIN as a Condition and Using cin.get() to Pause the Program

Aug 12th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. //this simple program shows use of the address operator,
  2. //cin as a condition,
  3. //and cin.get() to pause a program indefinitely
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. void sx(int&);
  9. void output(int&);
  10. void pauseProgram();
  11.  
  12. int main()
  13. {
  14. int x;
  15. sx(x);
  16. output(x);
  17. pauseProgram();
  18. return 0;
  19. }
  20.  
  21. void sx(int &x) //this function demonstrates using cin as a condition. since it takes x as a reference parameter it doesn't need to return x.
  22. {
  23. while(!(cin>>x))
  24. {
  25. cin >> x;
  26. cin.clear();
  27. cin.ignore(100,'\n');
  28. }
  29. return;
  30. }
  31.  
  32. void output(int &x) //this function is a simple cout function to simplify main
  33. {
  34. cout << endl;
  35. cout << "The value of x is: " << x << endl;
  36. cout << "The memory address of x is: " << &x << endl;
  37. return;
  38. }
  39.  
  40. void pauseProgram() //this simple function pauses the program until any key even enter/return is pressed.
  41. {
  42. cin.get();
  43. cin.get();
  44. return;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment