1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
const int MAX_SPEED = 50;
const int MIN_SPEED = 0;
const int ACCELERATE = 5;
const int DECELERATE = -5;
class Bus
{
public:
string busDriverName;
int speed;
int numberOfSeats;
int busIDNumber;
Bus (string);
Bus (int, int, int);
int busMenuOptions () // menuOptions function declaration, and initialization
{
int choice;
do
{
cout << setfill('*'); // instructs the program to insert a * whenever the code setw() is present
cout << "Make a selection from the menu below:" << endl; // Instructs the user to enter a menu option
cout << "*" << setw(29) << "*" << endl; // Blocks the menu out in *
cout << "MAIN MENU \n"; // outputs the text in ""
cout << "1. Set the bus ID number \n";
cout << "2. Set the number of seats on the bus \n";
cout << "3. Set the bus driver's name \n";
cout << "4. Accelerate or decelerate\n";
cout << "5. display bus information \n";
cout << "6. Quit Program \n" ;
cout << "*" << setw(29) << "*" << endl;
cout << "Please enter your selection now: ";
cin >> choice; // accepts the user input and saves it as choice
do
{
if (!cin)
{
cout << "Invalid input \n" << endl;
cin.clear(); // Clears the input stream.
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Clears the input buffer.
cout << "Please enter a valid menu selection: "; // tells the user to to enter the value
cin >> choice;
}
}
while (!cin);
cout << endl;
switch (choice) // switch structure to compare the user input choice with the switch statement
{
case 1: // if 1 is selected case 1 is initialized
Bus::setBusID(busIDNumber);
break; // terminates the switch structure
case 2: // if 2 is selected case 2 is initalized
Bus::setSeats(numberOfSeats);
break; // terminates the switch structure
case 3: // if 3 is selected case 3 is initialized
Bus::setBusDriver(busDriverName);
break; // terminates the switch structure
case 4:
Bus::accelerateDecelerate(speed);
break;
case 5: // if 4 is selected case 4 is initialized
displayBus (busIDNumber, numberOfSeats, busDriverName, speed);
break; // terminates the switch structure
case 6: // if 5 is selected case 5 is initialized
cout << "Thanks for using the bus program!" << endl;
system ("Pause");
return 0;
default :
cout << "Invalid selection, please try again. \n" << endl; // if anything other than the choices of the switch structure is entered the switch loop will repeat and "Invalid selection, please try again will be displayed"
}
}
while (choice != 6);
}
void setBusDriver(string busDriverName)
{
cout << "Enter the name of the bus driver: \n" << endl;
cin >> busDriverName;
if (!cin)
{
cout << "Invalid input \n" << endl;
cin.clear(); // Clears the input stream.
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Clears the input buffer.
cout << "Please enter a name for the bus driver : "; // tells the user to to enter the value
cin >> busDriverName;
cout << endl << "Thank you!" << endl;
}
}
void setBusID (int busIDNumber)
{
cout << "Enter the id number of the bus: ";
cin >> busIDNumber;
cout << endl;
}
void accelerateDecelerate (int choice)
{
cout << "To accelerate the bus press 1, to decelerate press 2";
cin >> choice;
switch (choice)
{
case 1:
if (speed < MAX_SPEED)
speed += ACCELERATE;
break;
case 2:
if (speed > MIN_SPEED)
speed += DECELERATE;
break;
default:
cout << "Invalid input, please try again" << endl;
}
}
void setSeats (int & numberOfSeats)
{
cout << "Please enter the total seats on the bus: ";
cin >> numberOfSeats;
if (!cin)
{
cout << "Invalid input \n" << endl;
cin.clear(); // Clears the input stream.
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Clears the input buffer.
cout << "Please enter the number of seats : "; // tells the user to to enter the value
cin >> numberOfSeats;
}
cout << endl;
}
void displayBus (int & busIDNumber, int & numberOfSeats, string busDriverName, int & speed)
{
cout << "The bus id number: " << busIDNumber << " is driven by " << busDriverName << " , with a maximum seating of\n " << numberOfSeats << ". Current speed is: " << speed << endl;
}
};
Bus::Bus (string busDriverName)
{
busDriverName = "Dave";
}
Bus::Bus (int speed, int busIDNumber, int numberOfSeats)
{
speed =0;
busIDNumber =0;
numberOfSeats=0;
}
int main()
{
Bus info;
info.busMenuOptions();
return 0;
}
| |