I am working on the "checkout" process of my vending machine code. I want to write it so that the program will keep asking for the amount needed from the user until all the money is entered. However, this code segment does not completely work.
"Checkout" Segment of Code:
while (money < total) {
float amountOwed = total - money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: $" << flush;
float payment;
cin >> payment;
}
if (money > total) {
float change = money - total;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == total) {
cout << "Thank you! Have a nice day!." << endl;
}
Full code below:
#include <iostream>
#include <iomanip>
using namespace std;
string menuItems[5] = { "Popcorn", "Coconut Clusters" , "Granola Bar" , "Trail Mix" , "Chocolate" };
float cost[5] = { 2, 3, 2.50, 1.50, 1 };
void vendingMachine() {
for (int i = 0; i < 5; i++)
cout << i + 1 << ". " << menuItems[i] << ": $" << cost[i] << endl;
}
int main() {
cout.precision(2);
cout << std::fixed;
cout << "Vending Machine" << endl;
cout << "----Items------" << endl;
vendingMachine();
cout << "Enter 0 to checkout" << endl;
float total;
total = 0;
int item;
do {
cout << "Enter your selection: " << flush;
cin >> item;
item = item - 1;
//here will be printed : $0 has been added to cart even if you pressed 0 and what to escape
//is it possible to fix this??
cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;
total = total + cost[item];
} while (item != -1);
cout << " " << endl;
cout << "Proceding to checkout..." << endl;
cout << "========================" << endl;
cout << "Amount due: $" << total << endl;
cout << "Insert money here: $" << flush;
float money;
cin >> money;
while (money < total) {
float amountOwed = total - money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: $" << flush;
float payment;
cin >> payment;
}
if (money > total) {
float change = money - total;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == total) {
cout << "Thank you! Have a nice day!." << endl;
}
return 0;
}
In this loop:
while (money < total) {
you are not modifying money or total so the loop will never exit.
You probably want to update money like this:
while (money < total) {
// ...
cin >> payment;
money += payment;
}
Related
I am currently writing a code where in my main function I am just calling other functions. I am attempting to reloop back to the begining so the user can run the program again. The main function is just to call functions so my question is I know it is not possible to go back to the main function, but is it possible to create a function that will loop all other functions again? I feel as though I tried everything and continue to get infinite loops. I attached my code.
To condense the code please understand that all variables/classes are declared
void instructions();
void full_outputs(string, double, double, double);
int main()
{
instructions();
employee_num = employee_ID();
//cout << employee_num << " This is the employee ID."<<endl;
base_salary = baseSalary();
//cout << base_salary << " This is the employee's base salary." <<endl;
per_commission = percentage_commission();
//cout << per_commission << " This is the employee's percentage commission." << endl;
base_commission = base_and_com(base_salary, per_commission);
cout<< base_commission << "This is the total base salary with comission" << endl;
gross_pay = grossPay(base_commission);
//cout << gross_pay << "This is the gross pay"<<endl;
state_tax_hold = stateTax_hold(gross_pay);
//cout<< state_tax_hold << "This is the state tax hold on the amount" <<endl;
fica_total = ficaTotal(gross_pay);
//cout << fica_total << " This is the fica hold on the amount" <<endl;
fed_tax = fedTax(gross_pay);
//cout << fed_tax << " THis is the federal tax hold on the amount" << endl;
total_tax_hold = withholding_total(state_tax_hold, fica_total, fed_tax);
//cout << total_tax_hold << " This is the total tax withholding" << endl;
net_pay = netPay(total_tax_hold, gross_pay);
//cout << net_pay << " This is the total net pay" << endl;
full_outputs(employee_num, gross_pay, total_tax_hold, net_pay);
return 0;
}
void instructions()
{
cout << " This program will process sales employee's base salary \n";
cout << " and their percentage commission. \n";
cout << " You will be prompted to enter the employee's ID, base salary \n";
cout << " and percentage commission. \n";
cout << " \n";
cout << " The program will terminate if unspecified characters are used. \n";
}
string employee_ID()
{
string employee_num;
cout << " Please enter the employees eight digit ID number" << endl;
cin >> employee_num;
return employee_num;
}
double baseSalary()
{
double base_salary;
cout << " Please enter the employees base salary " << endl;
cin >> base_salary;
return base_salary;
}
float percentage_commission()
{
float per_commission;
cout << " Please enter the employees percentage commission."<< endl;
cout << " Please do not enter the percent symbol." << endl;
cout << " Percentage commission is between 0.05% - 10%" << endl;
cin >> per_commission;
while ((per_commission < 0.05)||(per_commission > 10))
{
cout << "The commission rate is not between 0.05% and 10%" << endl;
cout << "Please try again " << endl;
cin >> per_commission;
}
per_commission = per_commission / PERCENT_TO_DECIMAL;
return per_commission;
}
double base_and_com(double base_salary, float per_commission)
{
double base_commission;
double total;
total = base_salary*per_commission;
base_commission = total + base_salary;
return base_commission;
}
double grossPay(double base_commission)
{
double gross_pay;
gross_pay = base_commission;
cout << fixed << showpoint << setprecision(2);
return gross_pay;
}
double stateTax_hold(double gross_pay)
{
double state_tax_hold;
state_tax_hold= gross_pay*STATE_TAX;
return state_tax_hold;
}
double ficaTotal (double gross_pay)
{
double fica_total;
fica_total = gross_pay* FICA;
return fica_total;
}
double fedTax (double gross_pay)
{
double fed_tax;
if (gross_pay <= 500)
{
fed_tax = gross_pay * FEDERAL_TAX_UNDER;
}
else
{
fed_tax = gross_pay * FEDERAL_TAX_OVER;
}
return fed_tax;
}
double withholding_total(double fed_tax, double fica_total, double state_tax_hold )
{
double tax_withholding_total;
tax_withholding_total = fed_tax + fica_total + state_tax_hold;
cout << fixed << showpoint << setprecision(2);
return tax_withholding_total;
}
double netPay(double total_tax_hold, double gross_pay)
{
double net_pay;
net_pay = (gross_pay - total_tax_hold);
cout << fixed << showpoint << setprecision(2);
return net_pay;
}
void full_outputs(string employee_num, double gross_pay, double total_tax_hold, double net_pay)
{
cout << " The employee ID : " << right << employee_num << endl;
cout << " The gross pay is: " << right << gross_pay << endl;
cout << " The total tax withholding amount is : " << right << total_tax_hold << endl;
cout << " The net pay is: " << right << net_pay << endl;
}
As you know, in main you can just have a while loop with the code you want to repeat inside it:
int main()
{
// this will loop forever, until you explicitly break or return
while (true) {
// your code here
}
}
... but since you are constrained by the artificial limitation of only having function calls in main...
void run()
{
// this will loop forever, until you explicitly break or return
while (true) {
// your code here
}
}
int main()
{
run();
}
so I have a school project where I'm trying to create a bank account. So I have an array of class customer containing the customer name etc, this customer can have multiple accounts, so I have created an array of class accounts inside of customer. My problem is, I can't call a getter function inside of the account array, I have tried using customerArray[curr_id]->accountArray[curr_acc_num]->get_balance() however I am getting an error telling my that it is unaccessable.
Here is the code, any input would be apprechiated as it is still a work in progress.
#include <iostream>.
#include <windows.h>
#include "account.h"
#include "customer.h"
using namespace std;
bool quit;
int amount;
double bal;
bool valid_pin;
customer* customerArray[200];
account* accountArray[5];
int curr_id;
int curr_acc_num;
//1st name, 2nd name, ID
customer cust1("Paul", "Smith", 2233);
//ID, Deposit, Pin
account Paul(112, 222, 6716);
//Welcome screen function
void welcome_screen() {
cout << endl << endl << endl;
cout << "\tAbertay Banking" << endl;
cout << "\t System " << endl << endl << endl;
Sleep(1800);
}
//Display account balance function
void account_balance() {
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << " " << customerArray[curr_id]->get_name() << endl;
cout << " ID: " << customerArray[curr_id]->get_custid() << endl << endl;
cout << " Balance: " << char(156) << customerArray[curr_id]->accountArray[curr_acc_num]->get_balance() << endl;
//Display any outstanding loans
if (Paul.get_loan() > 0) {
cout << endl << endl;
cout << " Outstanding loan: -" << char(156) << Paul.get_loan();
Sleep(3000);
}
//User has no loan on this account
else {
Sleep(3000);
}
}
//Withdrawl function
void withdrawl() {
bal = Paul.get_balance();
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter the amount you wish to withdraw: ";
cin >> amount;
//customer has sufficent funds, withdrawl accepted
if (amount <= bal + Paul.get_overdraft()) {
bal -= amount;
Paul.set_balance(bal);
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl; cout << "You have withdrawn: " << char(156) << amount << endl;
cout << "You now have a balance off: " << char(156) << bal << endl;
cout << "Do you wish to carry out another transaction?"; //do you wish to carry out another transaction.
Sleep(2000);
}
//customer doesn't have enough funds to carry out withdrawl
else {
char choice;
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "You have insufficient funds in your account" << endl;
}
}
//Deposit function
void deposit() {
//balance from user account
bal = Paul.get_balance();
char check;
double ln;
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter the amount you wish to deposit: ";
cin >> amount;
//Option to pay off loan, if applicable
if (Paul.get_loan() > 0) {
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Do you want to pay off your loan? (Y/N)" << endl;
cin >> check;
system("cls");
cout << "\tBank of Abertay" << endl << endl;
//User selected to pay off loan
if (check == 'y') {
//deposit paid into loan
if (amount < Paul.get_loan()) {
ln = Paul.get_loan() - amount;
Paul.set_loan(ln);
cout << "Your loan is now at: -" << char(156) << Paul.get_loan() << endl;
}
//loan is payed of, any extra money is deposited into balance
else {
ln = amount - Paul.get_loan() + Paul.get_balance();
Paul.set_loan(0);
Paul.set_balance(ln);
cout << "You have payed your loan off!" << endl;
}
}
}
//user has no loan or doesn't wish to pay off loan
else {
bal += amount;
Paul.set_balance(bal);
}
cout << "You have deposited: " << char(156) << amount << endl;
cout << "You now have a balance off: " << char(156) << bal << endl;
cout << "Would you like to select another option with this account?"; //do you wish to carry out another transaction.
Sleep(1500);
}
//Apply for overdraft
void overdraft() {
system("cls");
char choice;
cout << endl << endl;
cout << "\tTake out an overdraft of " << char(156) << "300? " << endl;
cout << "\t (Y/N)" << endl;
cin >> choice;
if (choice == 'y') {
Paul.set_overdraft(300);
}
}
//Calculating interest rates
double interest_rate(int loan) {
//Up to £3,000 rate of 11%
if (loan < 3000) {
return 1.11;
}
//From £3,000 to £7,000 rate of 9%
else if (loan > 3000 && loan < 7000) {
return 1.09;
}
//From £7,000 to £21,000 rate of 7%
else if (loan > 7000 && loan < 21000) {
return 1.07;
}
//From £21,000 over rate of 5%
else if (loan < 21) {
return 1.05;
}
}
//Loan eligibility funcition
void loan_elgibility() {
double max_loan;
double loan;
//Calculating loan eligibility
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Caculating loan eligibility";
max_loan = (2 * Paul.get_balance());
Sleep(800);
//Loan and rates
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << " You are eligible to borrow up to: " << char(156) << max_loan << endl << endl;
cout << " Interest rates are as follows" << endl << endl;
cout << " - " << char(156) << "3,000 at 11%" << endl;
cout << " " << char(156) << "3,000 - " << char(156) << "7,000 at 9%" << endl;
cout << " " << char(156) << "7,000 - " << char(156) << "21,000 at 7%" << endl;
cout << " " << char(156) << "21,000 - at 5%" << endl;
Sleep(6200);
//User input loan request
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " Enter loan request: " << char(156);
cin >> loan;
//Processing loan request
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " We are processing your request..." << endl;
Sleep(800);
system("cls");
//Loan has been approved, loan deposited into account
if (loan <= Paul.get_balance()) {
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "Your loan has been approved" << endl;
bal = Paul.get_balance() + loan;
loan = interest_rate(loan) * loan;
Paul.set_balance(bal);
Paul.set_loan(loan);
Sleep(800);
}
//Loan has not been approved
else {
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "You do not qualify for a loan of this amount";
Sleep(800);
}
}
//Quit function
void quit_screen() {
system("cls");
char choice;
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " Do you want to quit? (Y/N) " << endl;
cout << " ";
cin >> choice;
// customer wants to quit, end while loop and program
if (choice == 'y') {
system("cls"); //clear screen
cout << endl << endl << endl;
cout << " Thanks for using," << endl;
cout << " Bank of Abertay!"<< endl;
Sleep(2000);
quit = true;
}
else {
quit = false;
}
}
//Request user pin number
bool pin_request() {
double pin;
int counter = 0;
do {
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "\tEnter pin: ";
cin >> pin;
if (pin == Paul.get_pin()) {
return true;
}
else if (counter == 3) {
cout << "You have entered an invalid pin too many times!";
Sleep(800);
return false;
}
else {
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "You have entered an invalid pin" << endl;
Sleep(800);
valid_pin = false;
counter++;
}
} while (valid_pin == false && counter < 3);
}
//Adding a new customer
int counter = 0;
void add_customer(string first_nom, string second_nom, int cust_ID) {
customer* new_customer = new customer(first_nom, second_nom, cust_ID);
customerArray[counter] = new_customer;
counter++;
}
//creating new bank account
void create_account()
{
int input_custid;
double input_balance;
int input_pin;
string first_nom;
string second_nom;
int cust_ID;
char choice;
//Request new customer information
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter first name: ";
cin >> first_nom;
cout << endl;
cout << "Enter surname: ";
cin >> second_nom;
cout << endl;
cout << "Customer ID: ";
cin >> cust_ID;
//create new customer class array
add_customer(first_nom, second_nom, cust_ID);
//creating new account array
system("cls");
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "Enter The account No. :";
cin >> input_custid;
cout << "Initial deposit in new account: ";
cin >> input_balance;
cout << "Create pin: ";
cin >> input_pin;
system("cls");
cout << " We are processing your request..." << endl;
Sleep(800);
cust1.add_account(input_custid, input_balance, input_pin);
system("cls");
cout << "Account Created.." << endl;
cout << "Thank you for joining the Bank of Abertay";
Sleep(800);
}
//find user account
bool find_account() {
int custID;
int counter;
bool found_account;
system("cls");
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << " Enter your customer ID: ";
cin >> custID;
cout << " - ";
cin >> curr_acc_num;
//not happy about this!!
for (int i = 0; i < 200; i++) {
if (customerArray[i]->get_custid() == custID) {
curr_id = i;
return true;
}
else if (i == 199) {
return false;
}
}
}
//an option to send money to another account?
// need a function that takes user back to option menu if wrong pin enter!
int main() {
//welcome screen
welcome_screen();
//while loop until player decides to quit
do {
int option_choice;
system("cls"); //clear screen, updating board every time function called
cout << endl << endl;
cout << "\tBank of Abertay" << endl << endl;
cout << "\t Menu" << endl << endl;
cout << "\t1. View Balance" << endl;
cout << "\t2. Withdraw" << endl;
cout << "\t3. Deposit" << endl;
cout << "\t4. Loan eligibility" << endl;
cout << "\t5. New account" << endl;
cout << "\t6. Overdraft" << endl;
cout << "\t7. Quit" << endl << endl;
cout << "\t\tSelect: ";
cin >> option_choice;
//ensure option screen only runs through once unless an invalid option is chosen
int valid_number_check = 0;
// do {
switch (option_choice) {
//Display account balance
case 1:
if (find_account() == true && pin_request() == true) {
account_balance();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Account withdrawl
case 2:
if ( find_account() == true && pin_request() == true) {
withdrawl();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Account deposit
case 3:
if (find_account() == true && pin_request() == true) {
deposit();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Loan eligibility
case 4:
if (find_account() == true && pin_request() == true) {
loan_elgibility();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Create new account
case 5:
create_account();
break;
//Overdraft
case 6:
if (find_account() == true && pin_request() == true) {
overdraft();
break;
}
else {
cout << "Your request has been denied";
break;
}
//Quit
case 7:
quit_screen();
break;
//Invalid number entered
default:
cout << endl << endl;
cout << " Bank of Abertay" << endl << endl;
cout << "Please enter a valid number";
Sleep(800);
break;
}
} while (quit == false);
}
// main
Account .h
#pragma once
#include <string>
#include "account.h"
using namespace std;
class customer
{
private:
int custid;
string name;
account* accountArray[5];
int counter = 0;
public:
int get_custid();
customer(string, string, int);
string get_name();
void add_account(int, double, int);
~customer();
};
Account.cpp
#include "customer.h"
#include "account.h"
#include <string>
using namespace std;
customer::customer(string first_nom, string second_nom, int cust_ID) {
counter++;
string first_name = first_nom;
string sur_name = second_nom;
//counting the number of accounts
counter++;
//identification of customer array
//custid = counter + 1000;
custid = cust_ID;
}
customer::~customer()
{
}
int customer::get_custid() {
return custid;
}
void customer::add_account(int id, double dep, int pin) {
account* new_account = new account(id, dep, pin);
accountArray[counter] = new_account;
counter++;
}
string customer::get_name() {
return name;
}
customer.h
#pragma once
class account
{
private:
double balance;
int custid;
double loan_amount;
double overdraft;
int pin;
int account_number;
public:
account(int, double, int);
~account();
//getters
double get_balance();
double get_loan();
double get_pin();
double get_overdraft();
//setters
void set_overdraft(double over);
void set_balance(double bal);
void set_loan(double loan);
};
customer.cpp
#include "account.h"
#include <iostream>
#include <windows.h>
using namespace std;
account::account(int input_custid, double input_balance, int input_pin)
{
custid = input_custid;
balance = input_balance;
pin = input_pin;
loan_amount = 0;
overdraft = 0;
}
account::~account()
{
}
double account::get_balance()
{
return balance;
}
double account::get_loan()
{
return loan_amount;
}
double account::get_pin()
{
return pin;
}
double account::get_overdraft()
{
return overdraft;
}
void account::set_balance(double bal)
{
balance = bal;
}
void account::set_loan(double loan)
{
loan_amount = loan;
}
void account::set_overdraft(double over)
{
overdraft = over;
}
I created this simple program to practice working with classes. I'm certain there are some errors with the way I used the class, but I'm just beginning to learn about them, so I haven't learned all of the conventions and etiquette. My main question is, can Xcode have functions that are buffered and other functions that are non-buffered? I'd like my function void InputValuesAndDisplayTotals(); to be buffered and my function void ManuallyAddCoinsAndDisplayTotals(); to be non-buffered (basically, hit a key and the character is instantly processed without using the enter key). Is this possible? Thanks in advanced.
#include <iostream>
#include <iomanip>
using namespace std;
class Coin
{
private:
const float PENNYVALUE = 0.01;
const float NICKELVALUE = 0.05;
const float DIMEVALUE = 0.10;
const float QUARTERVALUE = 0.25;
int PennyInput=0;
int NickelInput=0;
int DimeInput=0;
int QuarterInput=0;
public:
void InputValuesAndDisplayTotals();
void ManuallyAddCoinsAndDisplayTotals();
void Total();
};
int main()
{
int Choice;
Coin Count;
cout << "1 to enter total coin counts, 2 to manually count coins: ";
cin >> Choice;
if (Choice==1)
{
Count.InputValuesAndDisplayTotals();
Count.Total();
}
else
{
Count.ManuallyAddCoinsAndDisplayTotals();
}
cout << endl;
}
void Coin::InputValuesAndDisplayTotals()
{
cout << fixed << setprecision(2) << endl;
cout << "Input penny count: ";
cin >> PennyInput;
cout << "Total penny value: $" << PENNYVALUE*PennyInput;
Total();
cout << endl;
cout << "Input nickel count: ";
cin >> NickelInput;
cout << "Total nickel value: $" << NICKELVALUE*NickelInput;
Total();
cout << endl;
cout << "Input dime count: ";
cin >> DimeInput;
cout << "Total dime value: $" << DIMEVALUE*DimeInput;
Total();
cout << endl;
cout << "Input quarter count: ";
cin >> QuarterInput;
cout << "Total quarter value: $" << QUARTERVALUE*QuarterInput;
Total();
}
void Coin::ManuallyAddCoinsAndDisplayTotals()
{
char Choice2;
cout << "\n'1' for penny,\n'2' for nickel,\n'3' for dime,\n'4' for quarter,\n'q' to quit\n";
do
{
cout << "\nInput: ";
cin >> Choice2;
if (Choice2=='1')
++PennyInput;
if (Choice2=='2')
++NickelInput;
if (Choice2=='3')
++DimeInput;
if (Choice2=='4')
++QuarterInput;
cout << endl;
cout << "Pennies: " << PennyInput << endl;
cout << "Nickels: " << NickelInput << endl;
cout << "Dimes: " << DimeInput << endl;
cout << "Quarters: " << QuarterInput << endl;
Total();
}
while (Choice2!='q' && Choice2!='Q');
}
void Coin::Total()
{
cout << "\nTotal amount: $";
cout << fixed << setprecision(2) << (PENNYVALUE*PennyInput)+(NICKELVALUE*NickelInput)+(DIMEVALUE*DimeInput)+(QUARTERVALUE*QuarterInput);
cout << "\n";
}
I'm new here. Sorry if I'm not posting correctly. My program is comparing cell phone plans based on minutes. I want to know how I can compare plans to determine the best plan for the money. I believe I should be using minimum functions for this, but I'm honestly stuck. My code is below. Is it possible to find the minimum value from 3 separate functions? I'm not looking for an answer, but maybe an example or an article how to do so. Thanks!
#include<iostream>
using namespace std;
void companyA();
void companyB();
void CompanyC();
int min(); // I want to use this to find my minimum
int numEmployees;
int avgMin;
double totalCost;
// double bestChoice; Not used yet
int main()
{
double newMin;
cout << "Please enter number of employees." << endl;
cin >> numEmployees;
cout << "Please enter average minutes used by each employee. " << endl;
cin >> avgMin;
if (numEmployees < 0)
{
cout << "Incorrect value. Please enter positive number of employees." << endl;
cin >> numEmployees;
}
else if (avgMin < 0)
{
cout << "Incorrect value. Please enter positive number of minutes. " << endl;
cin >> avgMin;
}
cout << "\nStandard Packages" << endl;
cout << "Company A: For $29.99 per month, 450 minutes are included. Additional minutes are $0.35 per minute." << endl;
cout << "Company B: For $49.99 per month, 900 minutes are provided. Additional minutes are $0.30 per minute." << endl;
cout << "Company C: For $59.99 per month, unlimited minutes are provided." << endl;
companyA();
companyB();
companyC();
cout << "\nBased on the number of employees and average minutes used, " << x << "is the best choice." << endl;
system("pause");
return 0;
}
void companyA()
{
if (avgMin <= 450)
{
totalCost = 29.99*numEmployees;
cout << "\nCompany A will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 450)
{
totalCost = (avgMin-450)*0.35+29.99;
cout << "\nCompany A will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
}
void companyB()
{
if (avgMin <= 900)
{
totalCost = 49.99*numEmployees;
cout << "Company B will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 900)
{
totalCost = (avgMin - 900)*0.30 + 49.99;
cout << "Company B will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
}
void companyC()
{
totalCost = 59.99*numEmployees;
cout << "Company C will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
I don't know if this is what you're looking for.
void minimum(double numEmployees, double avgMin);
double minimumCompany(double x, double y, double z);
void minimum(double numEmployees, double avgMin)
{
double totalCostA, totalCostB, totalCostC, minimumTotal;
string choice;
if (avgMin <= 450)
{
totalCostA = 29.99 * numEmployees;
cout << "\nCompany A will cost an $" << totalCostA << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 450)
{
totalCostA = (avgMin - 450) * 0.35 + 29.99;
cout << "\nCompany A will cost an $" << totalCostA << " a month for " << numEmployees << " employee(s)." << endl;
}
if (avgMin <= 900)
{
totalCostB = 49.99*numEmployees;
cout << "Company B will cost an $" << totalCostB << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 900)
{
totalCostB = (avgMin - 900)*0.30 + 49.99;
cout << "Company B will cost an $" << totalCostB << " a month for " << numEmployees << " employee(s)." << endl;
}
totalCostC = 59.99*numEmployees;
cout << "Company C will cost an $" << totalCostC << " a month for " << numEmployees << " employee(s)." << endl;
minimumTotal = minimumCompany(totalCostA, totalCostB, totalCostC);
if (minimumTotal == totalCostA)
{
choice = "Company A";
}
else if (minimumTotal == totalCostB)
{
choice = "Company B";
}
else
{
choice = "Company C";
}
cout << "\nBased on the number of employees and average minutes used, " << choice << " is the best choice." << endl;
}
double minimumCompany(double x, double y, double z)
{
double minimum = x;
if (y < minimum ) {
minimum = y;
}
if (z < minimum) {
minimum = z;
}
return minimum;
}
int main()
{
double numEmps, averageMin;
bool isValid = true;
while (isValid)
{
cout << "Please enter number of employees." << endl;
cin >> numEmps;
cout << "Please enter average minutes used by each employee. " << endl;
cin >> averageMin;
if ((numEmps < 0))
{
cout << "Please enter number of employees. " << endl;
cin >> numEmps;
isValid = true;
}
else
{
isValid = false;
}
if ((averageMin < 0))
{
cout << "Please enter average minutes used by each employee. " << endl;
cin >> numEmps;
isValid = true;
}
else
{
isValid = false;
}
}
cout << "\nStandard Packages" << endl;
cout << "Company A: For $29.99 per month, 450 minutes are included. Additional minutes are $0.35 per minute." << endl;
cout << "Company B: For $49.99 per month, 900 minutes are provided. Additional minutes are $0.30 per minute." << endl;
cout << "Company C: For $59.99 per month, unlimited minutes are provided." << endl;
minimum(numEmps, averageMin);
system("pause");
return 0;
}
Getting an unassigned variable error after the GetTransfer portion of the code. It is claiming account1 is the unassigned variable but from what i can see i have declared it. i have tried multiple times to correct it and i just can figure it out.
#include <iostream>
#include <conio.h>
using namespace std;
// Chance Pinkerton
// December 3, 2013
// CaseProject2Chapter3
struct bankInfo
{
int accountNum;
double startBal;
double endBal;
};
int main()
{
bankInfo account1;
bankInfo account2;
double transferAmt;
int GetAccount1();
int GetAccount2();
double GetBal1();
double GetBal2();
double GetTransfer();
account1.accountNum = GetAccount1();
while (account1.accountNum < 1000 || account1.accountNum > 9999)
{
cout << "Error. That account does not exsist." << endl;
account1.accountNum = GetAccount1();
}
GetBal1();
account2.accountNum = GetAccount2();
while (account2.accountNum < 1000 || account2.accountNum > 9999)
{
cout << "Error. That account number doesnt exsist." << endl;
account2.accountNum = GetAccount2();
}
if (account2.accountNum == account1.accountNum)
{
cout << "Error. Account numbers can not be the same " << endl;
account2.accountNum = GetAccount2();
}
GetBal2();
transferAmt = GetTransfer();
account1.accountNum = (account1.accountNum % 5) + (account1.accountNum * 10);
account2.accountNum = (account2.accountNum % 5) + (account2.accountNum * 10);
account1.endBal;
account2.endBal;
while (account1.endBal < 0)
{
cout << "Error. Account balance can not be negative." << endl;
GetTransfer();
if (account1.endBal < 10)
{
cout << "Warning. Account balance will be below $10.00 " << endl;
}
}
account1.endBal = account1.startBal - transferAmt;
account2.endBal = account2.startBal + transferAmt;
cout << "Account number 1: " << account1.accountNum << endl;
cout << "Starting balance: " << account1.startBal << endl;
cout << "Ending balance: " << account1.endBal << endl;
cout << "Account number 2: " << account2.accountNum << endl;
cout << "Starting balance: " << account2.startBal << endl;
cout << "Ending balance: " << account2.endBal << endl;
getch();
return 0;
}
int GetAccount1()
{
int accountNum;
cout << "Please enter your account number " << endl;
cin >> accountNum;
return accountNum;
}
int GetAccount2()
{
int accountNum;
cout << "Please enter the second account number " << endl;
cin >> accountNum;
return accountNum;
}
double GetBal1()
{
double startBal;
cout << "Enter your account balance " << endl;
cin >> startBal;
return startBal;
}
double GetBal2()
{
double startBal;
cout << "Enter the second account balance " << endl;
cin >> startBal;
return startBal;
}
double GetTransfer()
{
double transferAmt;
cout << "How much would you like to transfer to the second account " << endl;
cin >> transferAmt;
return transferAmt;
}
These two statements do exactly nothing, and are likely the source of the warning the compiler is giving you:
account1.endBal;
account2.endBal;
Furthermore, I think you need to actually capture the return value of GetTransfer() here, perhaps into transferAmt:
GetTransfer();
You probably need to rethink this entire while loop, especially taking into account how transferAmt relates to account1.endBal:
while (account1.endBal < 0)
{
cout << "Error. Account balance can not be negative." << endl;
GetTransfer();
if (account1.endBal < 10)
{
cout << "Warning. Account balance will be below $10.00 " << endl;
}
}
For example, you don't want to update endBal until you know the transfer will succeed. Furthermore, your low-balance warning is in completely the wrong place.