1,105,556 Community Members

c++ setprecision help

Member Avatar
plang007
Junior Poster in Training
68 posts since Feb 2011
Reputation Points: -5 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
#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.

Member Avatar
Red Goose
Junior Poster
115 posts since Jan 2011
Reputation Points: 21 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
0
 

setprecision affects the output stream, not the variable, therefore, it will continue to display the precision until you change it.

Example:

#include <iostream>
#include <iomanip>

using std::cout;
using std::setprecision;

int main()
{
 float x = 5.12384;

 cout << "Variable = " << x << "\n";
 cout << "Precision 2 = " << setprecision(2) << x << " " << x; //note that the precision continues.
 cout << "\nPrecision 3 = " << setprecision(3) << x;
 cout << "\nShowing no precision = " << setprecision(0) << x << "\n";
 return 0;
}
Member Avatar
Sky Diploma
Practically a Posting Shark
878 posts since Mar 2008
Reputation Points: 571 [?]
Q&As Helped to Solve: 138 [?]
Skill Endorsements: 8 [?]
 
0
 

@planq007:Please use code tags the next time you post code on daniweb.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: