Game Programming



Visit Base Classics - Gaming Chairs

 
game programming
Game Programming :: Programming :: General Programming :: Code Examples :: [C++] Implementing a while loop - View Topic
Topic Rating: *****
Printable View
Andy
Administrator
*****

Posts: 40
Status: Offline
Gender: Male
Joined:  
pm

Reputation: 0%

[C++] Implementing a while loop (11th Jan 08 at 1:59am UTC)
Level: Beginner

This code is a very basic tally system. It begins by giving you instructions on how to use the program, then receives the input, and then displays the results of the tally.

Note: The while loop is used when it is not known how many times a program must perform a certain action (or block of statements).

Code:
 
  1. #include<iostream>
  2.  
  3. int main()
  4. {
  5.  
  6. //declaring variables
  7. short int age=0;
  8. unsigned long int student=0;
  9. unsigned long int adult=0;
  10. unsigned long int senior=0;
  11.  
  12. //program instructions
  13. cout << "Tally System has been launched." << endl;
  14. cout << endl;
  15.  
  16. cout << "Please enter the age of each customer riding the subway."<< endl;
  17. cout << "(Make sure to press the enter key after each age input.)"<<endl;
  18. cout << endl;
  19.  
  20. cout << "When the subway closes for the day, enter -1 to end the data entry process." << endl;
  21. cout << endl;
  22.  
  23. cout << "The screen will then display the fee classification tallies." << endl;
  24. cout << endl;
  25.  
  26.  
  27. //data entry process
  28. while(age!=-1)
  29. {
  30. cout<<"Customer's age: ";
  31. cin>>age;
  32.  
  33. cout<<endl;
  34.  
  35. if (age>=65)
  36. {
  37. ++senior;
  38. }
  39. else if (age>=18)
  40. {
  41. ++adult;
  42. }
  43. else if (age>0)
  44. {
  45. ++student;
  46. }
  47. else if (age==0)
  48. {
  49. cout << "!Error! Please enter a valid age value." << endl;
  50. cout << endl;
  51. }
  52. else if (age<-1)
  53. {
  54. cout << "!Error! Please enter a valid age value." << endl;
  55. cout << endl;
  56. }
  57. //end of if statements
  58.  
  59. }//end of while loop
  60.  
  61.  
  62. //fee classification tallies
  63. cout << "Total customers in student fee class: ";
  64. cout << student << endl;
  65. cout << endl;
  66. cout << "Total customers in adult fee class: ";
  67. cout << adult << endl;
  68. cout << endl;
  69. cout << "Total customers in senior fee class: ";
  70. cout << senior << endl;
  71. cout << endl;
  72. cout << "This program will now terminate." << endl;
  73.  
  74. }//end of program
  75.  
 


 Printable View

All times are GMT+0 :: The current time is 7:53am
Page generated in 0.1024 seconds
Purchase Ad-Free Credits
This Forum is Powered By vForums (v2.4)
Create a Forum for Free | Find Forums