468,504 Members | 2,006 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

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

Bye!

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;


cin.get();
cout << endl;
cout << "Bye!";
cin.get();

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 5139
Banfa
9,055 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
  2.  
  3. for(;;)
  4. {
  5.     // Input hours
  6.  
  7.     if (hours<0)
  8.         break;
  9.  
  10.     // Input rest of data
  11.  
  12.     // Handle data
  13. }
  14.  
  15. // program exit code
  16.  
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
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.