Calculate price of a phone call
Hello everyone, I am new to this forum and am looking for some assistance, any help would be greatly appreciated! I'm trying to calculate the price for a phone call under these requirements,
Price per min 4 dollars.
VAT 25%.
Between 8:00 and 18:30 you pay full price
Prior to 8:00, and after 18:30 are given 65% discount on price per min.
For conversations longer than 30 minutes are given a discount of 15% of the total price.
There is something wrong with my calculations but I can't figure out what it is.
Here is some examples for prices if the calculations work as they should,
07:45 - 8:15 ---> 101.25 $
07:45 - 8:16 ---> 90,31 $
07:30 - 07:38 ---> 14,00 $
07:55 - 08:10 ---> 58,75 $
12:03 - 12:53 ---> 212,50 $
17:58 - 18:37 ---> 146,41 $
07:59 - 18:31 ---> 2680,48 $
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
double convert(string start, string end);
double priceCalc(int startHour, int startMinute, int endHour, int endMinute);
bool errorCheck(int startHour, int startMinute, int endHour, int endMinute);
int main()
{
char ch;
do
{
string callStart,callEnd;
cout<<"What time the call start? (hh:mm): ";
getline(cin, callStart);
cout<<"What time did the call end? (hh:mm): ";
getline(cin, callEnd);
cout<<convert(callStart,callEnd)<<" $" << endl;
cout << "Run again(y/n)? ";
cin >> ch; cin.ignore(250,'\n');
do
{
ch = toupper(ch);
}while( !(ch == 'Y' || ch == 'N'));
}while(ch == 'Y');
return 0;
}
double convert(string start, string end)
{
int startHour = 0, startMinute = 0, endHour = 0, endMinute = 0;
string::size_type startPos = start.find(":");
if(startPos != string::npos)
;
else
{
cout << "Only : is allowed" << endl;
return 0;
}
string::size_type endPos = end.find(":");
if(endPos != string::npos)
;
else
{
cout << "Only : is allowed" << endl;
return 0;
}
start.replace(start.find(":"), 1, " ");
end.replace(end.find(":"), 1, " ");
istringstream iss(start);
iss >> startHour >> startMinute;
iss.clear();
iss.str(end);
iss >> endHour >> endMinute;
if (errorCheck(startHour,startMinute,endHour,endMinute))
return 0;
return priceCalc(startHour,startMinute,endHour,endMinute);
}
bool errorCheck(int startHour, int startMinute, int endHour, int endMinute)
{
if (((startHour * 60) + startMinute) >= ((endHour * 60) + endMinute))
{
cout << "Time error ";
return true;
}
if (startHour>23||startHour<00||endHour>23||endHour<00||startMinute>59||startMinute<00||endMinute>59||endMinute<00)
{
cout << "Time error ";
return true;
}
else
return false;
}
double priceCalc(int startHour, int startMinute, int endHour, int endMinute)
{
int startinMiuntes = startHour * 60 + startMinute;
int endinMinutes = endHour * 60 + endMinute;
int totMin = 0;
int const pricePerMin=4;
double price;
const double VAT=1.25;
const double disconut=0.35;
const double discount30min=0.85;
totMin=endinMinutes-startinMiuntes;
if((startHour>=8 && startMinute>=0) && (endHour<=18 && endMinute<=30))
price=totMin*pricePerMin*VAT;
else
price=(pricePerMin*disconut)*totMin*VAT;
if(totMin>30)
{
price=price*discount30min;
}
return price;
}
Re: Calculate price of a phone call
Code:
do
{
ch = toupper(ch);
}while( !(ch == 'Y' || ch == 'N'));
Don't you want to input ch again within this loop?
Code:
if(startPos != string::npos)
;
else
{
cout << "Only : is allowed" << endl;
return 0;
}
This can be simplified as
Code:
if (startPos == string::npos)
{
cout << "Only : is allowed" << endl;
return 0;
}
Code:
start.replace(start.find(":"), 1, " ");
As you already have the return value of .find() from previous code, why not just
Code:
start.replace(startPos, 1, " ");
Code:
if((startHour>=8 && startMinute>=0) && (endHour<=18 && endMinute<=30))
What happens if a phone call starts prior to 8 but finishes after 8? Doesn't the part of the call prior to 8 get a discount but the part after 8 but before 18:30 charged at full price?
Re: Calculate price of a phone call
With the first loop I want to ask the user if she/he want to run the program again. But it seems like I have done something wrong there as well because if I input something else than y or n.
Yes that's true! If the call starts before 8 and finish after 8 you get a discount on the part that's after 8.