issue with calculation function c++ - c++

my program is a simple payroll calculator.
if the employee type is 0 any hours worked over 40 are time and a half.
if type is 1 they don't get overtime just straight pay.
I am trying to hold all the data in struct arrays. This is a homework assignment, and my program meets the requirements, but the issue im having is after i input hours for employees and then choose menu option c, my output is just zero.
Im not sure why exactly there wasn't any data outputted except zero in the column. I have no errors in my compiler.
What did I do wrong?
C++
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//*********************************************************************************************
// Structure to hold employee ID, Name, Pay rate per hour, Employee type
//*********************************************************************************************
struct employeeRecord {
int IDnum; // holds employee id number
string name; // holds employess name
float payRate; // holds employee pay rate
int empType; // holds employee type
};
//*********************************************************************************************
// structure to hold time sheet
//*********************************************************************************************
struct timeSheet{
float hours; //hours worked
float grossPay; // gross pay
float netPay; // net pay
float taxAmount; // tax amount deduction
};
//*********************************************************************************************
// function prototype
//*********************************************************************************************
void addEmployee(employeeRecord *employee, int &index);
void addTime(timeSheet *time, employeeRecord *employee, int &index);
void calcTime(timeSheet *time, employeeRecord *employee, int &index, float taxRate);
void outTime(timeSheet *time, employeeRecord *employee, int &index);
//*********************************************************************************************
// main function
//*********************************************************************************************
int main(){
float taxRate = .15; // taxrate on pay
char choice; // holds user choice option
int index = 0; // index of array
// create struct arrays to hold data
employeeRecord employee[2];
timeSheet time[2];
// menu title
cout << "\nArmadillo Automotive Group Payroll\n" << endl;
//*********************************************************************************************
// do loop to cycle through menu options & validate input
//*********************************************************************************************
do{
//menu
cout << "\nMenu Options: \n";
cout << "A - Add Employee" << endl;
cout << "B - Add Employee's Hours" << endl;
cout << "C - Print Payroll Report" << endl;
//take user menu choice
cout << "\nEnter Menu Option: ";
cin >> choice;
choice = tolower(choice);
//validate user selection
while (choice != 'a' && choice != 'b' && choice!= 'c'){
cout << "Please enter 'A' 'B' or 'C' : ";
cin >> choice;
}
// Process menu selection choice
if (choice != 'c'){
if (choice == 'a'){
//process new employee
if (index < 2){
addEmployee(employee, index);
}
else{
cout << "\nEmployee index full" << endl;
}
}
// if choice 'b' add employee work hours
else{
addTime(time, employee, index);
}
}
}while (choice != 'c');
// calculates and stores employees time sheet information in struct array
calcTime(time, employee, index, taxRate);
//display pay check outputs
outTime(time, employee, index);
return 0;
}
//*********************************************************************************************
// function to add employee to index
//*********************************************************************************************
void addEmployee(employeeRecord *employee, int &index){
//take employees ID #
cout << "\nEnter employee's ID number: ";
cin >> employee[index].IDnum;
//validate employee id # is greater than 0
while (employee[index].IDnum < 0){
cout << "\nPlease enter an ID # above 0 : ";
cin >> employee[index].IDnum;
}
//take employees name
cout << "\nEnter employee's Name: ";
cin.ignore();
getline(cin, employee[index].name);
//validate line has characters only
//**********************
//**********************
//**********************
//take employees payrate
cout << "\nEnter employee's pay rate: $";
cin >> employee[index].payRate;
//validate payrate is amount above 0
while(employee[index].payRate < 0){
cout << "\nPlease enter a value greater than 0: ";
cin >> employee[index].payRate;
}
// take employees emptype
cout << "\nEnter Employee type \n";
cout << "0 for union, 1 for management: ";
cin >> employee[index].empType;
//validate 0 or 1 was entered
while(employee[index].empType != 0 && employee[index].empType != 1){
cout << "Enter 1 or 0: ";
cin >> employee[index].empType;
}
// increment index for future entry
index++;
}
//*********************************************************************************************
// function to add time to an employee
//*********************************************************************************************
void addTime(timeSheet *time, employeeRecord *employee, int &index){
//set index to zero to start with first employee on list
index = 0;
//main instruction
cout << "Enter timecard information for each employee: " << endl;
//enter hours for each employee for loop
for(int i=0; i < 2; i++){
cout << "Hours worked for " << employee[index].name << ": ";
cin >> time[index].hours;
index++;}
}
//*********************************************************************************************
// function to to calculate timesheet
//*********************************************************************************************
void calcTime(timeSheet *time, employeeRecord *employee, int &index, float taxRate){
index = 0; // set index back to zero to start at first array index
float tempHours; //temp hour hold
float overTime; // overTime hours
float hours = time[index].hours; //hours worked
float grossPay = time[index].grossPay; //employes's gross pay
float netPay = time[index].netPay; //employes's net pay
float taxAmount = time[index].taxAmount; ////employes's tax deduction
int empType = employee[index].empType; // employee type
float payRate = employee[index].payRate; // employes pay rate
for (int i=0; i < 2; i++){
if (empType == 0){
if(hours > 40){
tempHours = 40;
overTime = hours - 40;
grossPay = (overTime * (payRate * 1.5)) + (tempHours * payRate);
taxAmount = grossPay * taxRate;
netPay = grossPay - taxAmount;
}
else{
grossPay = hours * payRate;
taxAmount = grossPay * taxRate;
netPay = grossPay - taxAmount;
}
}
else{
grossPay = hours * payRate;
taxAmount = grossPay * taxRate;
netPay = grossPay - taxAmount;
}
// increase index number
index++;
}
}
//*********************************************************************************************
// function to to print out timesheet
//*********************************************************************************************
void outTime(timeSheet *time, employeeRecord *employee, int &index){
index = 0; // set index back to zero to start at first array index
int empID = employee[index].IDnum; //employes id number
string name = employee[index].name; // employees name
float grossPay = time[index].grossPay; //employes's gross pay
float taxAmount = time[index].taxAmount; ////employes's tax deduction
float netPay = time[index].netPay; //employes's net pay
cout << "Pay Roll Report\n" << endl;
cout << setw(10) << left << "ID" << setw(30) << left << "Name";
cout << setw(10) << right << "Gross Pay" << setw(10) << right << "Tax";
cout << setw(10) << right << "Net Pay" << endl;
// for loop to display employee information.'
for (int i=0; i < 2; i++){
cout << setw(10) << left << empID << setw(30) << left << name;
cout << setw(10) << right << grossPay << setw(10) << right << taxAmount;
cout << setw(10) << right << netPay << endl;
index++;}
}

Related

How to call integer from one method to another method in c++

I am trying to make a program that can take student's information(name, Fee) from the user, then ask the user to enter raise percentage value and then calculate the new fee of all students.
For this, I need the Fee details entered in setValues() method to be called in calculateFee() method. But I can't figure it out that how can I do this.
Kindly help me with this.
class student
{
public:
int fee;
char name[20];
//member function to read details
void setValues()
{
cout << "Enter Name:";
cin >> name;
cout << "Enter Fee:";
cin >> fee;
}
// member function to calculate the new fee
void calculateFee()
{
float rFee;
cout << "Enter the Percent value for Fee Increment";
cin >> rFee;
if (rFee <= 0)
{
cout << "Please enter value greater than 0" << endl;
}
else
{
float temp;
temp = fee * rFee / 100;
rFee = fee + temp;
cout << "New Fee is" << endl;
cout << "Name" << name;
cout << "Fee" << rFee << endl;
}
}
};
int main()
{
student std[3]; //array of students object
int n, i;
n = 3;
for (i = 0; i<n; i++){
cout << "Enter details of student " << i + 1 << ":\n";
std[i].setValues();
}
for (i = 0; i < n; i++){
cout << "\nDetails of student " << i + 1 << ":\n";
std[3].calculateFee();
}
return 0;
}

Total of an array using class object

How can I get the total of the grosspay of all five employees? I've tried everything including creating objects but none seem to work, also I must store all the data in one array called EmpData so I cannot change that. I require assistance. This is the code I've created and it runs and works properly so far.
#include<iostream>
using namespace std;
class Employee {
private:
double hourswrk;
double payrate;
double grosspay;
int empno;
char empname[20];
double netpay;
double tax;
double overt;
double overtime;
double taxdeduct;
public:
void getdetails();
void calculatepay();
void showdetails();
};
void Employee::getdetails()
{
cout << "\nEnter employee name:\n";
cin >> empname;
cout << "\nEnter employee number:\n";
cin >> empno;
cout << "Enter hours worked:";
cin >> hourswrk;
cout << "Enter rate of pay";
cin >> payrate;
}
void Employee::calculatepay()
{
tax = 0.25;
overt = 1.5;
if(hourswrk >= 60)
{
grosspay = 0;
netpay = 0;
taxdeduct = 0;
cout << "You have exceeded the amount of hours!";
}
else if(hourswrk <= 40)
{
grosspay = hourswrk * payrate;
}
else if(hourswrk > 40 && hourswrk < 60)
{
overtime = hourswrk - 40;
grosspay = overt * payrate*overtime + hourswrk * payrate;
}
taxdeduct = tax * grosspay;
netpay = grosspay - taxdeduct;
}
void Employee::showdetails()
{
cout << "Employee Payslip\n";
cout << "Name: " << empname;
cout << "Employee number:" << empno;
cout << "Basic Salary" << payrate;
cout << "Hours work" << hourswrk;
cout << "Grosspay" << grosspay;
cout << "Tax: " << taxdeduct;
cout << "Net Salary" << netpay;
cout << endl;
}
int main()
{
Employee EmpData[5];
int i;
double hourswrk;
double payrate;
double grosspay;
int empno;
char empname[20];
double netpay;
double tax = 0.25;
double taxdeduct;
double overt = 1.5;
double overtime;
for(int i = 0; i < 5; i++)
{
EmpData[i].getdetails();
EmpData[i].calculatepay();
EmpData[i].showdetails();
}
system("pause");
return 0;
}
i just added a global variable which has totgrosspay every time you enter grosspay grosspay is added to totgrosspay
#include<iostream>
long totgrosspay=0;
using namespace std;
class Employee {
private:
long grosspay=0;
double hourswrk;
double payrate;
int empno;
char empname[20];
double netpay;
double tax;
double overt;
double overtime;
double taxdeduct;
public:
void getdetails();
void calculatepay();
void showdetails();
};
void Employee::getdetails()
{
cout << "\nEnter employee name:\n";
cin >> empname;
cout << "\nEnter employee number:\n";
cin >> empno;
cout << "Enter hours worked:";
cin >> hourswrk;
cout << "Enter rate of pay";
cin >> payrate;
}
void Employee::calculatepay()
{
tax = 0.25;
overt = 1.5;
if(hourswrk >= 60)
{
grosspay = 0;
netpay = 0;
taxdeduct = 0;
cout << "You have exceeded the amount of hours!";
}
else if(hourswrk <= 40)
{
grosspay = hourswrk * payrate;
}
else if(hourswrk > 40 && hourswrk < 60)
{
overtime = hourswrk - 40;
grosspay = overt * payrate*overtime + hourswrk * payrate;
}
taxdeduct = tax * grosspay;
netpay = grosspay - taxdeduct;
totgrosspay= totgrosspay+grosspay;
}
void Employee::showdetails()
{
cout << "Employee Payslip\n";
cout << "Name: " << empname;
cout << "Employee number:" << empno;
cout << "Basic Salary" << payrate;
cout << "Hours work" << hourswrk;
cout << "Grosspay" << grosspay;
cout << "Tax: " << taxdeduct;
cout << "Net Salary" << netpay;
cout << endl;
}
int main()
{
Employee EmpData[5];
int i;
for(int i = 0; i < 5; i++)
{
EmpData[i].getdetails();
EmpData[i].calculatepay();
EmpData[i].showdetails();
}
cout<<totgrosspay;// it prints gross pay value
system("pause");
return 0;
}

Mortgage Calc outputs -e value

I have compiled and run the my equation and it seems to work completely fine from a an user interface and from the compile side, but the equation is getting lost in the inheritance somewhere. Any thoughts?
#include <iostream>
#include <cmath>
using namespace std;
class MortgageCalc
{
private:
float loan, interest, term;
int years;
float monthlyDue, totalDue, totalInt;
public:
MortgageCalc() = default;
void setData(float, float, float);
float setLoan(void); //mutator
float setIntrest(void); //mutator
float setYears(void);
float setTerm (); //mutator
float getMonthlyDue();
float getTotalDue();
float getTotalInt();
};
void MortgageCalc::setData(float l, float i, float y)
{
loan = l;
interest = i;
years = y;
}
float MortgageCalc::setLoan(void)
{
return loan;
}
float MortgageCalc::setIntrest(void)
{
return interest;
}
float MortgageCalc::setYears(void)
{
return years;
}
float MortgageCalc::setTerm()
{
term = pow((1 + ((interest/100) / 12)), (12 * years)); //simple interest calculation with power calc to establish whole number translation
return term;
}
float MortgageCalc::getMonthlyDue()
{
monthlyDue = (loan * ((interest/100) / 12) * term) / (term - 1);
return monthlyDue;
}
float MortgageCalc::getTotalDue()
{
totalDue = (getMonthlyDue() * (years * 12));
return totalDue;
}
float MortgageCalc::getTotalInt()
{
totalInt = (getTotalDue() - loan);
return totalInt;
}
int main()
{
MortgageCalc mort1;
int choice = 0;
int years = 0;
double term(0) , loan(0), interest(0);
cout << "Enter the total loan amount on your mortgage loan: $"; //established loan variable
cin >> loan;
cout << "Enter the interest rate (in whole #'s only): "; //establishes interest rate variable
cin >> interest;
cout << "Enter the length of the loan in years: "; //establishes term of payments
cin >> years;
mort1.setData(loan, interest, years);
while (choice != 3) //loop for menu options and clean function exit
{
cout << endl;
cout << "Program to Calculate Mortgage Payments" << endl <<
"1. Monthly Payment" << endl <<
"2. Total Payment" << endl <<
"3. Exit" << endl << endl <<
"Enter an option above: ";
cin >> choice;
if (choice == 1)
cout << "Monthly payment due is " << mort1.getMonthlyDue() << "." << endl;
else if (choice == 2)
cout << "Total amount for entire loan payments plus interest is $" << mort1.getTotalDue() << "." << endl <<
"Total Interest Paid for this loan amount $" << mort1.getTotalInt() << "." << endl;
}
return 0;
}
Here is the text output:
Enter the total Loan Amount: $100,000
Enter the interest Rate: 5
Enter the length of the loan: 10
Program to calculate mort payments
1. Monthly Payments
2. Total Payments
3. Exit Enter and Option Above: 1
Monthly Payment Due is: -1.56868e-036
Thanks for the support on this one, I found out that the term call was being placed outside of the child reference and therefore was able to update the field and return the correct input.
#include <iostream>
#include <cmath>
using namespace std;
class MortgageCalc
{
private:
float loan, interest, term;
int years;
float monthlyDue, totalDue, totalInt;
public:
MortgageCalc() = default;
void setData(float, float, float);
float setLoan(void); //mutator
float setIntrest(void); //mutator
float setYears(void);
float setTerm (); //mutator
float getMonthlyDue();
float getTotalDue();
float getTotalInt();
};
void MortgageCalc::setData(float l, float i, float y)
{ //copies arguments from input to private members
loan = l;
interest = i;
years = y;
setTerm();
}
float MortgageCalc::setTerm()
{ //simple interest calculation with power calc to establish whole number translation
term = pow((1 + ((interest/100) / 12)), (12 * years));
return term;
}
float MortgageCalc::setLoan(void)
{ //returns loan amt to private member
return loan;
}
float MortgageCalc::setIntrest(void)
{ //returns interest amt to private member
return interest;
}
float MortgageCalc::setYears(void)
{ //returns years to private member
return years;
}
float MortgageCalc::getMonthlyDue()
{ //simple cal to output monthly
monthlyDue = (loan * ((interest/100) / 12) * term) / (term - 1);
return monthlyDue;
}
float MortgageCalc::getTotalDue()
{
totalDue = (getMonthlyDue() * (years * 12));
return totalDue;
}
float MortgageCalc::getTotalInt()
{
totalInt = (getTotalDue() - loan);
return totalInt;
}
int main()
{
MortgageCalc mort1;
int choice = 0;
int years = 0;
double term(0) , loan(0), interest(0);
cout << "Enter the total loan amount on your mortgage loan: $"; //established loan variable
cin >> loan;
cout << "Enter the interest rate (in whole #'s only): "; //establishes interest rate variable
cin >> interest;
cout << "Enter the length of the loan in years: "; //establishes term of payments
cin >> years;
mort1.setData(loan, interest, years);
while (choice != 3) //loop for menu options and clean function exit
{
cout << endl;
cout << "Program to Calculate Mortgage Payments" << endl <<
"1. Monthly Payment" << endl <<
"2. Total Payment" << endl <<
"3. Exit" << endl << endl <<
"Enter an option above: ";
cin >> choice;
if (choice == 1)
cout << "Monthly payment due is " << mort1.getMonthlyDue() << "." << endl;
else if (choice == 2)
cout << "Total amount for entire loan payments plus interest is $" << mort1.getTotalDue() << "." << endl <<
"Total Interest Paid for this loan amount $" << mort1.getTotalInt() << "." << endl;
}
return 0;
}

How to add a set of integers in C++

I'm fairly new to programming with C++ and I'm trying to understand where I'm missing the flow of the algorithm in this code. The code is supposed to take the cost of a set of tickets ($25, $30, $15). When I run the code it will add the total number of tickets bought, but not the total cost of all the tickets. I'm believe I've assigned the variables correctly but I don't know why it isn't adding together the total cost unless I need to assign those variables separately.
Any help would be appreciated, thanks!
using namespace std;
int main()
{
//declare variables
double orchestra = 25;
double mainFloor = 30;
double balcony = 15;
double totalSales = 0.0;
//enter input items
cout << "Enter orchestra tickets ";
cin >> orchestra;
cout << "Enter main floor tickets ";
cin >> mainFloor;
cout << "Enter balcony tickets ";
cin >> balcony;
//add the input items and print the total
totalSales = orchestra + mainFloor + balcony;
//display the total sales
cout << "Total Sales $" << totalSales << endl;
system("pause");
return 0;
} //end of main function
As pointed it out in another comment, there was no explanation.
You are assigning the cost to the same variable that you are using for input on the number of tickets (and consequently overwriting the cost). Instead, put the costs in separate variables (or constants if you prefer) and then do the math after getting the user input.
Try this:
using namespace std;
int main()
{
//declare variables
double cost_per_orchestra = 25;
double cost_per_mainFloor = 30;
double cost_per_balcony = 15;
double orchestra = 0;
double mainFloor = 0;
double balcony = 0;
double totalSales = 0.0;
//enter input items
cout << "Enter orchestra tickets ";
cin >> orchestra;
cout << "Enter main floor tickets ";
cin >> mainFloor;
cout << "Enter balcony tickets ";
cin >> balcony;
//add the input items and print the total
totalSales = cost_per_orchestra * orchestra + cost_per_mainFloor * mainFloor + cost_per_balcony * balcony;
//display the total sales
cout << "Total Sales $" << totalSales << endl;
system("pause");
return 0;
} //end of main function
You're overwriting your price values with your cin statements: Better create separate variables for the price and multiply them with your input.
#include <iostream>
using namespace std;
int main(){
//declare variables
const double orchestraPrice = 25;
const double mainFloorPrice = 30;
const double balconyPrice = 15;
double orchestra = 0;
double mainFloor = 0;
double balcony = 0;
double totalSales = 0.0;
//enter input items
cout << "Enter orchestra tickets ";
cin >> orchestra;
cout << "Enter main floor tickets ";
cin >> mainFloor;
cout << "Enter balcony tickets ";
cin >> balcony;
//add the input items and print the total
totalSales = orchestra * orchestraPrice + mainFloor * mainFloorPrice + balcony * balconyPrice;
//display the total sales
cout << "Total Sales $" << totalSales << endl;
system("pause");
return 0;
} //end of main function
Instead of static declarations per ticket value you can take it at runtime. Yes, as previous answerer mentioned new input value is overwriting to variable.
#include <iostream>
#define MAX 3
using namespace std;
typedef struct
{
double ticket_type_per_cost;
double total_no_of_tickets;
}ticket;
int main()
{
double totalSales=0;
ticket t;
int i;
for(i=0; i<MAX; i++)
{
cout<< "\nenter cost per ticket of type "<<i+1 <<": ";
cin>>t.ticket_type_per_cost;
cout<<"\nenter number of tickets: ";
cin>>t.total_no_of_tickets;
totalSales= totalSales + (t.ticket_type_per_cost * t.total_no_of_tickets);
}
//display the total sales
cout << "Total Sales $" << totalSales << endl;
system("pause");
return 0;
} //end of main function

How to call void function from Main

In my program I am trying to call the void function from Main but I can't figure out the correct way.
Main is at the very bottom and void GetTicketType(char &Choice) is the function I need to call to cout the ticket type.
//---------------------------------------------------------------------------
// Purpose: This program simulates a ticket office for sporting events
// Author: TBA
// Date: TBA
//---------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;
const char CASH = 'C';
const char CREDIT = 'D';
const char NOSEBLEED = 'N';
const char BOX_SEAT = 'B';
const char FIFTY_YARD_LINE = 'F';
const char STUDENT_SECTION = 'S';
const float NOSEBLEED_PRICE = 43.42;
const float BOX_SEAT_PRICE = 353.85;
const float FIFTY_YARD_LINE_PRICE = 94.05;
const float STUDENT_SECTION_PRICE = 19.99;
//---------------------------------------------------------------
// Function: ConfirmChoice
// Purpose: Confirms the users ticket purchase before processing payment
// Parameters: TicketType - The type of ticket selected
// Returns: true if the user confirms the selection, false otherwise
//--------------------------------------------------------------
bool ConfirmChoice(const char TicketType)
{
char Choice;
bool Confirmed;
// Print out their selection
cout << "\nYou have chosen to purchase ";
cout << fixed << setprecision(2);
switch (TicketType)
{
case NOSEBLEED:
cout << "Nosebleed ticket(s) at a price of $";
cout << NOSEBLEED_PRICE << ".\n";
break;
case BOX_SEAT:
cout << "Box Seat ticket(s) at a price of $";
cout << BOX_SEAT_PRICE << ".\n";
break;
case FIFTY_YARD_LINE:
cout << "Ticket(s) on the 50 yard line at a price of $";
cout << FIFTY_YARD_LINE_PRICE << ".\n";
break;
case STUDENT_SECTION:
cout << "Ticket(s) in the Student Section at a price of $";
cout << STUDENT_SECTION_PRICE << ".\n";
break;
}
// Confirm the selection
cout << "Do you wish to confirm your purchase? Enter Y or N: ";
cin >> Choice;
Choice = toupper(Choice);
while (Choice != 'Y' && Choice != 'N')
{
cout << "Invalid selection. Please enter either Y or N: ";
cin >> Choice;
Choice = toupper(Choice);
}
Confirmed = (Choice == 'Y');
// Check confirmation
if (Confirmed)
cout << "You have confirmed your choice.\n" << endl;
else
cout << "You not confirmed your choice.\n" << endl;
return (Confirmed);
}
//-------------------------------------------
// Function: CalculateChange
// Purpose: To output the change due
// Parameters: ChangeDue - The amount of change needed
// Returns: Nothing
//-------------------------------------------
void CalculateChange(const float ChangeDue)
{
int Change = 0;
int Dollars = 0;
int Quarters = 0;
int Dimes = 0;
int Nickels = 0;
int Pennies = 0;
// Compute change
Change = ChangeDue * 100;
Dollars = Change / 100;
Change = Change % 100;
Quarters = Change / 25;
Change = Change % 25;
Dimes = Change / 10;
Change = Change % 10;
Nickels = Change / 5;
Pennies = Change % 5;
// Print out change
cout << "Your change is \n\t";
cout << Dollars << " Dollars\n\t";
cout << Quarters << " Quarters\n\t";
cout << Dimes << " Dimes\n\t";
cout << Nickels << " Nickels\n\t";
cout << Pennies << " Pennies\n";
}
//---------------------------------------------------------------------------
// Function: CalculateCost
// Purpose: Calculate the cost of the ticket purchase(s) (num_tickets * price_per_ticket)
// Parameters: PricePerTicket - Ticket price based on the type of ticket
// Returns: The cost of purchasing the chosen number of tickets
//---------------------------------------------------------------------------
float CalculateCost(const float PricePerTicket)
{
int TicketCount;
float Cost;
cout << "How many tickets would you like? Please enter a positive integer value: ";
cin >> TicketCount;
while (TicketCount < 0)
{
cout << "Invalid entry. Please re-enter: ";
cin >> TicketCount;
}
Cost = PricePerTicket * TicketCount;
cout << "Your bill is: $" << fixed << setprecision(2) << Cost << endl;
return Cost;
}
//---------------------------------------------------------------------------
// Function: GetPaymentType
// Purpose: Ask the user how they want to pay, cash or credit
// Parameters: None
// Returns: Value is CREDIT or CASH (global character constants)
//---------------------------------------------------------------------------
char GetPaymentType()
{
char Choice;
// Print the main menu describing the ticket payment types
cout << "+-------------------------------------------------------+\n";
cout << "+ Welcome to our Ticket Office +\n";
cout << "+-------------------------------------------------------+\n";
cout << endl << endl;
// Cash or credit card (in upper case)
cout << "How would you like to pay?\n";
cout << "Enter C for cash or D for credit card: ";
cin >> Choice;
Choice = toupper(Choice);
while (Choice != CASH && Choice != CREDIT)
{
cout << "Invalid choice. Please enter C for cash or D for credit card: ";
cin >> Choice;
Choice = toupper(Choice);
}
return Choice;
}
//---------------------------------------------------------------------------
// Function: GetTicketType
// Purpose: Get the customer's choice between 4 types of tickets
// Parameters: Choice - Set to the user's choice
// Returns: Nothing
//---------------------------------------------------------------------------
void GetTicketType(char &Choice)
{
// Ask the customer what type of ticket s/he prefers to buy
cout << "\nWhat type of ticket would you like?\n";
cout << "\t" << NOSEBLEED << " for Nosebleed Section, Price = $";
cout << NOSEBLEED_PRICE << endl;
cout << "\t" << BOX_SEAT << " for Box Seats, Price = $";
cout << BOX_SEAT_PRICE << endl;
cout << "\t" << FIFTY_YARD_LINE << " for Seats on the Fifty Yard Line, Price = $";
cout << FIFTY_YARD_LINE_PRICE << endl;
cout << "\t" << STUDENT_SECTION << " for Student Section, Price = $";
cout << STUDENT_SECTION_PRICE << endl;
// Get ticket choice (in upper case)
cout << "Enter choice: ";
cin >> Choice;
Choice = toupper(Choice);
while (Choice != NOSEBLEED && Choice != BOX_SEAT &&
Choice != FIFTY_YARD_LINE && Choice != STUDENT_SECTION)
{
cout << "Invalid choice. Please re-enter: ";
cin >> Choice;
Choice = toupper(Choice);
}
}
//---------------------------------------------------------------------------
// Function: PayWithCash
// Purpose: Handles payment by cash. Asks the user for the money until
// they enter enough, then updates the ChangeDue parameter
// Parameters: Cost - The amount due for the purchase
// ChangeDue - The amount of change due to customer
// Returns: Nothing
//---------------------------------------------------------------------------
void PayWithCash(const float Cost, float &ChangeDue)
{
float CashOffered;
// Pay in cash
cout << "Please enter enough cash. Your bill is $" << Cost << ": $ ";
cin >> CashOffered;
// Check sufficiency
while (CashOffered < Cost)
{
cout << "That is not enough to pay for your purchase!\n"
<< " Please enter at least $" << Cost << ": ";
cin >> CashOffered;
}
// Calculate change
ChangeDue = CashOffered - Cost;
}
//---------------------------------------------------------------------------
// Function: PayWithCredit
// Purpose: Handles payment by credit. Basically, just prints a statement
// telling them that their card will be charged.
// Parameters: const float Cost - the amount due for the purchase
// Returns: Nothing
//---------------------------------------------------------------------------
void PayWithCredit(const float Cost)
{
cout << "Your credit card will be billed for $" << Cost << ".\n";
}
//---------------------------------------------------------------------------
// Function: main
// Purpose: This is the main program that calls functions above.
// Parameters: None
// Returns: Nothing
//---------------------------------------------------------------------------
int main()
{
// Declarations
char TChoice ; // Ticket type: Nosebleed, box seats etc..
TChoice << GetTicketType( &TChoice);
char PChoice = GetPaymentType() ; // Payment choice: cash or credit card
bool Confirmed; // Did the user confirm the selection
float Cost; // The cost of the ticket puchased
float ChangeDue; // The amount of change owed (for cash purchases)
// Print your name and UAID
// Get the choice of payment type
// Get the choice of ticket type
GetTicketType(TChoice );
{
cout << "You have chosen the " << TChoice << "tickets. " <<".\n";
}
// Confirm the selection
// If they confirm the purchase
// Call functions to figure out the price of ticket purchase(s)
// Be sure to use the named constants
// Handle the payment
// Say goodbye
// Else
// Cancel the purchase
return 0;
}
Change
TChoice << GetTicketType( &TChoice);
To Simple
GetTicketType( &TChoice);
Since TChoice << GetTicketType( &TChoice); is doing bitwise left shift operation it will expect an integer type after <<.Your function is returning nothing(void) and thus causes an error.
It looks like you're getting an error on this line:
TChoice << GetTicketType( &TChoice);
You're calling GetTicketType and expecting to use the result. Is that really what you want to do, since it's a void function?