简体   繁体   中英

Function and selection issue in C++

I revised this post. I am new to programming and do not know if the functions are set up correctly or even how to allow the selection of packages. Below is my objective.

I have a class assignment to write a C++ program that will calculate a customers monthly internet bill. It would input a customer name, which package the customer purchased, and how many hours were used.

The Requirements are:

Input validation: to be sure the user only selects package AB or C. Display and error message if the wrong package is entered.

Calculation:

PackA for 9.95 a month with 10 hours of acess. 2 bucks per additional hour. PackB for 14.95 a month with 20 hours of acess. 1 bucks per additional hour. PackC for 19.95 a month with unlimited acess.

I then have to create the output as a bill.

The code I wrote so far is:

/*
 James Hayek
 CIS 165
 Passaic County Community College
 Program 04
*/


#include <iostream>
#include <iomanip>

using namespace std;

double calcBill(double packageChoosen, double hours, double basePack, char name); // This is the function prototype for calcBill
void dispBill(char packageChoosen, double hours, double basePack, char name, char bill); // This is the function prototype for dispBill


int main()
{
 char packageChoosen;
 double bill;
 char name;
 double hours;
 double basePack;

 cout << "Welcome To Your Personal Bill Caclculator!\n\n";
 cout << "Please enter your name: \n";
 cin  >> name;
 cout << "Please choose your package by entering A, B, or C: \n";
 cin  >> packageChoosen;

 bill = calcBill(name, packageChoosen, hours, basePack); // call function calcBill
 dispBill(name, packageChoosen, hours, basePack, bill); // call function dispBill


 return 0;

} // end main




double calcBill(char packageChoosen, double hours, double basePack, char name) //This is the function for calcBill
{

 if (packageChoosen == 'A' || packageChoosen == 'a' && hours <= 10)
 {
  bill = 9.95;
 }
 else if (packageChoosen == 'A' || packageChoosen == 'a' && hours > 10)
 {
  bill = 9.95 + (2 * hours);
 } 

 else
 {
  cout << "Please choose an apropriate package";

 }


 return bill;

} // end main



void dispBill(double packA, double packB, double packC, char name, char bill) //This is the function for dispBill
{



 cout << bill;


 return;

} // end dispBill

I am not really sure where to begin, any advice would be greatly appreciated.

The && operator binds more tightly than the || operator in C++. Rather than

packageChoosen == 'A' || packageChoosen == 'a' && hours > 10

as a boolean expression, you most likely want

(packageChoosen == 'A' || packageChoosen == 'a') && hours > 10

The top expression looks either for a "package A" or a "package a with hours > 10". The bottom expression looks for a "package A or package a" and "hours > 10".

As Platinum Azure mentions in his answer, it is also possible to convert whatever character you have to lowercase, then do a single comparison against a lowercase letter.

#include <cctype>
// ...
std::tolower(packageChoosen) == 'a' && hours > 10

This makes your code a little more readable, and you don't have to worry about operator precedence.

Zeke's answer is solid; however, you could #include <cctype> and use std::tolower(packageChoosen == 'a' as part of your condition.

You need to think more in terms of OO. You're stuck in C world. I would suggest that Package would be a great candidate for an object.

As to the errors, you haven't said what they are so I can't really help you there. I'm too lazy to run a compile myself just to get what you should have already posted.

create an abstract class PackageAbstract create a class called user create a class called bill

create concretes class PackageA, packageB, packageC

in the user class add method : addPackage(packageAbstract* packageAbs){} add the member m_vector package choosen

for billing create a bill with user fedded with package...

each month you loop through users to create bills

for a real app you'll need a class custormers wich is feeded by users ...

Nice solution:

Create an abstract class Package, like so:

class Package{
public:
   virtual double calculate(double hours) = 0;
}

Then subclass concrete package types from it, like so:

class PackageA : public Package{
public:
   virtual double calculate(double hours){
        return hours < 10 ? 9.92 : 9.95 + hours*2;
   }
}

Then, you can use something like this code to calculate the bill:

Package* bill_calculators[3];
bill_calculators[0] = new PackageA();
bill_calculators[1] = new PackageB();
bill_calculators[2] = new PackageC();
char packageChosen;
cin >> packageChosen;
packageChosen = toupper(packageChosen) - 'A';
if(packageChosen >= 0 && packageChosen < 3) cout << bill_calculators[packageChosen]->calculate(hours);
else cout << "Invalid package\n";

I haven't actually compiled or tested the code, but you get the idea: create an abstract interface, implement algorithms for particular packages in subclasses, then create instances and decide which instance to use for calculation based on user input. Btw, you'll have to #include to use the toupper() function

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM