Could you please tell me how to arrange my code so that the invalid statement like in my displayed output will only show up if some enters a wrong character.
Here is my code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
//(1) Declare some variables to work with
char fare;
char choice='Y';
int hrIn, minIn, hrOut, minOut;
int hours;
int minutes;
int total_minutes;
int additionalTime;
double cost;
double charge;
while(choice=='Y'||choice=='y'){
//(2) Output read info and user message
cout << "\nThis program will calculate a single, couple, or group "
<< "\nfare amount which is then owed by the customer for the trip.\n"
<< "\nTo calculate a single fare amount, input S (or s)"
<< "\nTo calculate a couple fare amount, input C (or c)"
<< " \n\nInput S, C" ";
cin >> fare;
// end of description
//--------------------------------------------------------------------------------
//(3) This begins the if and else statements
if(fare=='S'||fare=='s'){
cout << "\nWhat hour did the single customer depart? ";
cin >> hrOut;
cout << "\nWhat minute did the single customer depart? ";
cin >> minOut;
cout << "\nYou entered for departure: " << hrOut << ":" << minOut << endl;
cout << "\nWhat hour did the single customer arrive? ";
cin >> hrIn;
cout << "\nWhat minute did the single customer arrive? ";
cin >> minIn;
cout << "\nYou entered for arrival: " << hrIn << ":" << minIn << "\n" << endl;
cout << "A rickshaw departed at " << hrOut << ":" << minOut
<< " and arrived at " << hrIn << ":" << minIn << " with a single customer.\n" << endl;
if(hrIn < hrOut && minIn <= minOut){
hours = (hrIn - hrOut) + 23;
cout << "The single customer arrived in " << hours << " hour(s)";
minutes = (minIn - minOut) + 60;
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
else if(hrOut < hrIn && minOut <= minIn){
hours = (hrIn - hrOut);
cout << "The single customer arrived in " << hours << " hour(s)";
minutes = (minIn - minOut);
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
//-----------------------------------------------------//(4) Different if and else but apart of S if
else if(hrIn < hrOut && minOut <= minIn){
hours = (hrIn - hrOut) + 24;
cout << "The single customer arrived in " << hours << " hour(s)";
minutes = (minIn - minOut);
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
else if(hrOut < hrIn && minIn <= minOut){
hours = (hrIn - hrOut) - 1;
cout << "The single customer arrived in " << hours << " hour(s)";
minutes = (minIn - minOut) + 60;
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
//-----------------------------------------------------//(5) Different if and else but apart of S if
else if(hrIn <= hrOut && minOut <= minIn){
hours = (hrIn - hrOut);
cout << "The single customer arrived in " << hours << " hour(s)";
minutes = (minIn - minOut);
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
if(total_minutes <=30){
cout << fixed << showpoint << setprecision(2);
charge = 7.00;
cout << "The single customer will pay $" << charge << " dollars.\n" << endl;
}
else{
additionalTime = total_minutes - 30;
cout << "The single customer will be charged for " << additionalTime << " extra minute(s).\n" << endl;
charge = 7.00 + (((total_minutes) - 30) * 1.50);
cout << fixed << showpoint << setprecision(2);
cout << "The single customer will pay $" << charge << " dollars.\n" << endl;
}
}
//(6) End of S if
//--------------------------------------------------------------------------------
if(fare=='C'||fare=='c'){
cout << "\nWhat hour did the single customer depart? ";
cin >> hrOut;
cout << "\nWhat minute did the single customer depart? ";
cin >> minOut;
cout << "\nYou entered for departure: " << hrOut << ":" << minOut << endl;
cout << "\nWhat hour did the single customer arrive? ";
cin >> hrIn;
cout << "\nWhat minute did the single customer arrive? ";
cin >> minIn;
cout << "\nYou entered for arrival: " << hrIn << ":" << minIn << "\n" << endl;
cout << "A rickshaw departed at " << hrOut << ":" << minOut
<< " and arrived at " << hrIn << ":" << minIn << " with a single customer.\n" << endl;
if(hrIn < hrOut && minIn <= minOut){
hours = (hrIn - hrOut) + 23;
cout << "The single customer arrived in " << hours << " hour(s)";
minutes = (minIn - minOut) + 60;
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
else if(hrOut < hrIn && minOut <= minIn){
hours = (hrIn - hrOut);
cout << "The single customer arrived in " << hours << " hour(s)";
minutes = (minIn - minOut);
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
//-----------------------------------------------------//(7) Different if and else but apart of C if
else if(hrIn < hrOut && minOut <= minIn){
hours = (hrIn - hrOut) + 24;
cout << "The single customer arrived in " << hours << " hour(s)";
minutes = (minIn - minOut);
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
else if(hrOut < hrIn && minIn <= minOut){
hours = (hrIn - hrOut) - 1;
cout << "The single customer arrived in " << hours << " hour(s)";
minutes = (minIn - minOut) + 60;
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
//-----------------------------------------------------//(8) Different if and else but apart of C if
else if(hrIn <= hrOut && minOut <= minIn){
hours = (hrIn - hrOut);
cout << "The single customer arrived in " << hours << " hour(s)";
minutes = (minIn - minOut);
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
if(total_minutes <=20){
charge = 10.50;
cout << fixed << showpoint << setprecision(2);
cout << "The couple will pay $" << charge << " dollars.\n" << endl;
}
else{
additionalTime = total_minutes - 20;
cout << "The couple will be charged for " << additionalTime << " extra minute(s).\n" << endl;
cout << fixed << showpoint << setprecision(2);
charge = 10.50 + (((total_minutes) - 20) * 2.50);
cout << "The couple will pay $" << charge << " dollars.\n" << endl;
}
}
else{
cout << "\nThe character that you have entered was invalid! Please try again!\n" << endl;
}
cout << "Would you like to perform another calculation (Y/N)? ";
cin >> choice;
}
//(13) This is my termintation message!
cout << "\nI am quite surprised that you are leaving my program so soon,\n"
<< "but I will thank you anyways for using my ODUSPORTS calculator!\n" << endl;
system("pause");
return 0;
}
*Output:
This program will calculate a single, couple, or group
fare amount which is then owed by the customer for the trip.
To calculate a single fare amount, input S (or s)
To calculate a couple fare amount, input C (or c)
To calculate a group fare amount, input G (or g)
Input S, C, or G: s
What hour did the single customer depart? 9
What minute did the single customer depart? 25
You entered for departure: 9:25
What hour did the single customer arrive? 10
What minute did the single customer arrive? 30
You entered for arrival: 10:30
A rickshaw departed at 9:25 and arrived at 10:30 with a single customer.
The single customer arrived in 1 hour(s) and 5 minute(s),
or in a total of 65 minutes.
The single customer will be charged for 35 extra minute(s).
The single customer will pay $59.50 dollars.
The character that you have entered was invalid! Please try again!
Would you like to perform another calculation (Y/N)?