471,873 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,873 software developers and data experts.

Create a discount calculating program

I too am new to the whole C++ language, and I am an eager beaver and wish I could absorb it all instantly, but I can't. The first program I wrote (very simple) was easy enough, using your basic cin and cout's, no complex formulas or anything. We recently learned about loops and if statements, and those have me a bit confused as to how to use them.

Here is the program:
To encourage usage of the computing resources outside of the peak hours, a 20% discount is offered to computer users at off peak hours. OFF peak hours are before 8am and on or after 6pm. The program must accept hours and usage charge and print out if it's a normal charge or a discounted charge, and the final charge. User enters a negative number to exit the program. Assume that there is no input error. User enters hours in 24 hour format.
The Input is time of day based on a 24 hour clock, and the usage charge.
the output is the function of the program, instructions on how to exit the program, proper promts for each input and normal or discoutned charge with proper description.

I have 3 test cases to use. Here is an example of a sample session.

Cost Adjuster.

Enter a negative time to quit.

Hour -> 8
Charge -> 6.5
Normal charge: 6.50
Peak hours - no discount

Next hour -> 7
Charge -> 2.54
Normal charge: 2.54
Discounted charge: $2.03

Next hour -> -2


Here is what I have so far:
#include <iostream>
#include <iomanip>

using namespace std;

void main()
int hour; // Hour entered by the user in 24 hour format
int charge; // Charge entered by the user
int discharge; // Discounted charge during off peak hours

cout << fixed << showpoint;

// Inform user what the the purpose of the program is and how to quit the program
cout << "Cost Adjuster" << endl;
cout << endl;
cout << "Enter a negative time to quit." << endl;
cout << endl;

//Promt user to input the hour and usage charge.
cout << setprecision(2);
cout << "Hour -> ";
cin >> hour;
cout << "Charge -> ";
cin >> charge;
cout << "Charge -> " << charge;

cout << endl;
cout << "Bye!";

I am so lost and frustrated right now. I know I have to use a while loop and an if statement. I am just not sure how to word those and where to put them. Can anyone help?
Oct 14 '06 #1
1 5331
9,065 Expert Mod 8TB
Well deciding where the loop goes is relatively easy, it goes round the bit of the code you want to repeat.

Since you have to perform the loop break in the middle of the code block (i.e. after inputing hours but before everything else I would use an infinite loop if a loop break condition in the appropriate place, pseudo coded as

Expand|Select|Wrap|Line Numbers
  1. // initialise program
  3. for(;;)
  4. {
  5.     // Input hours
  7.     if (hours<0)
  8.         break;
  10.     // Input rest of data
  12.     // Handle data
  13. }
  15. // program exit code
Oct 16 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by peter | last post: by
3 posts views Thread by Protoman | last post: by
6 posts views Thread by asif929 | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.