Reputation: 13
The program prompt basically asks for a program where you have to get two inputs from the person: the time (based one military time i.e 1800 is 6:pm) and minutes long for the call. you then take those two inputs and using boolean if then and else statements, you apply discounts, and then get a net amount. here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ // some user variables that need inputing based on their call
int starttime;
int minutes;
const float tax = 0.04;
const float flatrate = 0.35;
const float mindiscount = 0.15;
const float timediscount = 0.5;
float callrate = float (minutes * flatrate); // the standard call rate stored in a variable.
float gross; // the gross cost before discount
float net; // the net cost
cout << "enter start time: " << endl;
cin >> starttime;
if (starttime > 2499) // a check put inplace so someone doesn't enter an invalid time integer
{cout << "You entered an invalid time. Should only be based on the 24 hour clock " << endl;
return 1;}
cout << "enter total minutes: " << endl;
cin >> minutes;
gross = callrate;
cout << "gross cost: " << gross << setprecision(2) << endl;
{if ((minutes < 60 ) && (starttime < 1800 && starttime > 800)) // if no discounts are made, this is the final price before tax
net = gross;
else if (starttime > 1800 || starttime < 800) // if the call is between 6:00 PM and 8:00 AM, it gets a 50% discount
net = gross - (gross * timediscount);}
if (minutes > 60 ) // if the call lasted longer than 60 minutes, it gets a 15% discount
net = net - (net * mindiscount);
cout << "net cost: " << float (net + (net * tax)) << setprecision(2) << endl; // the final net cost with the 4% tax added.
return 0;
}
i keep getting a crazy output, such as:
enter start time:
1400
enter total minutes:
40
gross cost: 802795
net cost: 8.3e+005
Process returned 0 (0x0) execution time : 3.740 s
Press any key to continue.
When even the gross cost should be a very basic small number based on minutes * 0.35(35 cent rate per minute) but shows up as 802795. what am i doing wrong here?
Upvotes: 0
Views: 1690
Reputation: 25936
Here:
float callrate = float (minutes * flatrate);
minutes
is being used uninitialized, so you're getting whatever garbage value happened to be in there. I'm not following your program logic entirely, but you may want to move that line after this:
cin >> minutes;
when (presuming the input was correctly obtained from cin
, which you don't verify) minutes
will have a real initialized value.
You also don't use float
in that way, would be something like:
float callrate = (float) minutes * flatrate;
but the cast is not here necessary anyway, since flatrate
is a float to begin with, and minutes
will be automatically promoted for the purposes of the calculation. C-style casts are also considered fairly bad form in C++, so avoid them if you can.
EDIT: The same issue is cropping up here:
{
if ((minutes < 60 ) && (starttime < 1800 && starttime > 800))
net = gross;
else if (starttime > 1800 || starttime < 800)
net = gross - (gross * timediscount);
}
if ( minutes > 60 )
net = net - (net * mindiscount);
If neither the first if
or the corresponding else if
conditions are true, then net
never gets assigned a value, so your second if
statement can be using it uninitialized. This will always happen if minutes >= 60
and starttime
is between 800 and 1,800.
You have to be careful with initializing variables, since C++ is very unforgiving in this regard. Whenever you're getting weird or garbage values, chances are very high that this is what's going on.
Upvotes: 3