You are here:

C++/C++ program help

Advertisement


Question
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
 char fare;
 char choice='Y';
 int hrIn, minIn, hrOut, minOut;          
 int hours;
 int minutes;
 int total_minutes;          
 double cost;
 float charge;
 float additionalTime;
 
 while(choice=='Y'||choice=='y'){
         
 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)"
      << "\nTo calculate a group fare amount, input G (or g)"
      << " \n\nInput S, C, or G: ";
  cin >> fare;

//--------------------------------------------------------------------------------  
   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;    
         }
         
//----------------------------------------------// Different if and else but same field         
     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;    
         }
//-----------------------------------------------------//diffetn if else but same field
     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);++setprecision   
     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);+++setprecison
     cout << "The single customer will pay $" << charge << " dollars.\n" << endl;
     }
}

//--------------------------------------------------------------------------------  
   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;    
         }
         
//----------------------------------------------// Different if and else but same field         
     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;    
         }
//-----------------------------------------------------//diffetn if else but same field
     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);+++setprecison
     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);++setprecision
     charge = 10.50 + (((total_minutes) - 20) * 2.50);
     cout << "The couple will pay $" << charge << " dollars.\n" << endl;  
     
     }
}

//--------------------------------------------------------------------------------
   if(fare=='G'||fare=='g'){
     
     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;    
         }
         
//----------------------------------------------// Different if and else but same field         
     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;    
         }
//-----------------------------------------------------//diffetn if else but same field
     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 <=15){
     
     cout << fixed << showpoint << setprecision(2);++setprecision
     charge = 16.00;
     cout << "The group of customers will pay $" << charge << " dollars.\n" << endl;
         }
     if(total_minutes > 15 && total_minutes <=30 ){
     
     additionalTime = total_minutes - 15;
     cout << "The group of customer will be charged for " << additionalTime << " extra minute(s).\n" << endl;
     cout << fixed << showpoint << setprecision(2);+ setprecision
     charge = 16.00 + (((total_minutes) - 15) * 4.00);
     cout << "The group of customers will pay $" << charge << " dollars.\n" << endl;
     }
     
     if(total_minutes > 30){
     
     additionalTime = total_minutes - 15;
     cout << "The group customers will be charged for " << additionalTime << " extra minute(s).\n" << endl;    
     cout << "The group customers will also be charged an additional $50.00 fee for exceeding the time of 30 minutes.\n" << endl;          
     cout << showpoint << setprecision(2); +++++++++ setprecision
     charge = 16.00 + (((total_minutes) - 15) * 4.00) + 50;
     cout << "Which brings the group of customers will pay a total amount of $" << 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;
}
    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;      
       
       cout << " TTTTTTT H  H     A     N   N K  K  SSSS !! " << endl;
       cout << "    T    H  H    A A    NN  N K K   S    !! " << endl;
       cout << "    T    HHHH   AAAAA   N N N KK    SSSS !! " << endl;
       cout << "    T    H  H  A     A  N  NN K  K     S    " << endl;
       cout << "    T    H  H A       A N   N K   K SSSS !! " << endl;
  
    system("pause");
    return 0;
}

My problem is that the program runs but for setprecision I am getting two decimal places after the minutes when I am just trying to get 2 decimal places after the dollar amount how would i fix that? I pointed out the setprecisions.

Answer
Hello Joe.

Try changing the type of additionalTime from float to int. You are doing only integer arithmetic with it and it does not need to be a floating point value. That will cause it to be formatted with no decimal places.

The other alternative is to use setprecision(0) and noshowpoint, like this:

cout << "The single customer will be charged for " << setprecision(0) << noshowpoint << additionalTime  << " extra minute(s).\n" << endl;

I hope that helps you out.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.