How to Calculate How Many Times Inputs Entered in Sentinel Loop c++ - c++

Here is the link to the programming question for detail. I have done my source code but I don't know how to calculate and display how many times inputs are entered in sentinel loop. Here is my source code:
#include <iostream>
using namespace std;
int main() {
int i, hours;
int tot_clients = 0;
float charges;
float tot_collection = 0;
const int sentinel = -1;
cout << "Welcome to ABC Parking Services Sdn Bhd" << endl;
cout << "=======================================" << endl;
while (hours != sentinel) {
cout << "Please enter number of parking hours (-1 to stop)" << endl;
cin >> hours;
if (hours <= 2 && hours > 0) {
charges = 1.00 * hours;
cout << "Charges: " << charges << endl;
}
else if (hours > 2 && hours <= 12) {
charges = (1.00 * 2) + ((hours - 2) * 0.50);
cout << "Charges: " << charges << endl;
}
else if (hours > 12) {
charges = 10.00 * hours;
cout << "Charges: " << charges << endl;
}
else if (hours == sentinel) {
break;
}
tot_collection = tot_collection + charges;
}
cout << "SUMMARY OF REPORT" << endl;
cout << "=======================================" << endl;
cout << "Total clients:" << tot_clients << endl;
cout << "Total colletion:" << tot_collection << endl;
return 0;
}
Hope someone can help me solving this. I am studying for my programming final exam.

I understand you want to count the number of clients (tot_clients). You have already intitalised the tot_clients = 0. This is good. You just have to increment the tot_clients variable inside the while loop (tot_clients++).
while (hours != sentinel)
{
cout << "Please enter number of parking hours (-1 to stop)" << endl;
cin >> hours;
tot_clients++; //this is the code to be added
/*rest of the code remains same*/

Add tot_clients++; just before or after tot_collection = tot_collection + charges;

1- initialize hours=0 otherwise hour will have some undetermined value during first-time condition check of while loop.
2-i am assuming that tot_clients stores total no. of customers than,
you just need to increment tot_clients on each iteration
#include <iostream>
using namespace std;
int main()
{
int i, hours=0;
int tot_clients=0;
float charges;
float tot_collection=0;
const int sentinel = -1;
cout << "Welcome to ABC Parking Services Sdn Bhd" << endl;
cout << "=======================================" << endl;
while (hours != sentinel)
{
cout << "Please enter number of parking hours (-1 to stop)" << endl;
cin >> hours;`
if (hours <= 2 && hours >0)
{
charges = 1.00 * hours;
cout << "Charges: " << charges << endl;
}
else if (hours>2 && hours <=12)
{
charges = (1.00*2) + ((hours - 2) * 0.50);
cout << "Charges: " << charges << endl;
}
else if (hours > 12)
{
charges = 10.00 * hours;
cout << "Charges: " << charges << endl;
}
else if (hours == sentinel)
{
break;
}
tot_collection = tot_collection + charges;
tot_clients=tot_clients+1; //increment's on each iteration except on input -1
}
cout << "SUMMARY OF REPORT" << endl;
cout << "=======================================" << endl;
cout << "Total clients:" << tot_clients << endl;
cout << "Total colletion:" << tot_collection << endl;
return 0;
}
`

Related

How to update a while loop with multiple if statements?

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;
}

Calculations no processing C++

I'm making a simple payroll calculator and for some reason my "taxes" aren't calculating. When I run the program they just show $0 in the output. Any idea whats causing this? I commented "// Not calculating. Shows $0" on the lines that aren't calculating.
#include <iostream>
using namespace std;
int main()
// Parameters: None
// Returns: Zero
// Calls: None
{
int const hourly_wage = 16.78, overtime_rate = 25.17, social_security = 0.06, federal_income = 0.14, state_income = 0.05, union_dues = 10;
int gross_pay, hours_worked, number_of_dependents, ss_withheld, federal_withheld, state_withheld, net_pay, insurance, overtime_pay, again;
cout << "This program will calculate payroll for an employee." << endl;
do
{
cout << "\n\nEnter the number of hours the employee worked: " << endl;
cin >> hours_worked;
if (hours_worked > 40) {
overtime_pay = (hours_worked - 40) * overtime_rate;
gross_pay = (40 * hourly_wage) + overtime_pay;
}
else {
gross_pay = hours_worked * hourly_wage;
}
cout << "\n\nEnter the number of dependents for employee: " << endl;
cin >> number_of_dependents;
if (number_of_dependents >= 3) {
insurance = 35;
}
else {
insurance = 0;
}
// Payroll Calculation
ss_withheld = gross_pay * social_security;
federal_withheld = gross_pay * federal_income;
state_withheld = gross_pay * state_income;
net_pay = gross_pay - (ss_withheld + federal_withheld + state_withheld + insurance + union_dues);
cout << "\n\nGross Pay: $" << gross_pay << endl;
cout << "Social Security Withhholding: $" << ss_withheld << endl; // Not calculating. Shows $0
cout << "Federal Withholding: $" << federal_withheld << endl; // Not calculating. Shows $0
cout << "State Withholding: $" << state_withheld << endl; // Not calculating. Shows $0
cout << "Union Dues: $" << union_dues << endl;
cout << "Insurance Deduction: $" << insurance << endl;
cout << "Net Pay: $" << net_pay << endl;
cout << "\nDo you want to do another employee(Y/N)? ";
cin >> again;
} while (again == 'y' || again == 'Y');
cout << "\nHope they enjoy the money!" << endl;
return 0;
}
It looks like you are creating int Const for double values. ie.
#include "stdafx.h";
#include <iostream>
using namespace std;
int main()
// Parameters: None
// Returns: Zero
// Calls: None
{
int const union_dues = 10;
double const hourly_wage = 16.78, overtime_rate = 25.17, social_security = 0.06, federal_income = 0.14, state_income = 0.05;
double gross_pay, hours_worked, number_of_dependents, ss_withheld, federal_withheld, state_withheld, net_pay, insurance, overtime_pay, again;
cout << "This program will calculate payroll for an employee." << endl;
do {
cout << "\n\nEnter the number of hours the employee worked: " << endl;
cin >> hours_worked;
if (hours_worked > 40) {
overtime_pay = (hours_worked - 40) * overtime_rate;
gross_pay = (40 * hourly_wage) + overtime_pay;
}
else {
gross_pay = hours_worked * hourly_wage;
}
cout << "\n\nEnter the number of dependents for employee: " << endl;
cin >> number_of_dependents;
if (number_of_dependents >= 3) {
insurance = 35;
}
else {
insurance = 0;
}
// Payroll Calculation
ss_withheld = gross_pay * social_security;
federal_withheld = gross_pay * federal_income;
state_withheld = gross_pay * state_income;
net_pay = gross_pay - (ss_withheld + federal_withheld + state_withheld + insurance + union_dues);
cout << "\n\nGross Pay: $" << gross_pay << endl;
cout << "Social Security Withhholding: $" << ss_withheld << endl; // Not calculating. Shows $0
cout << "Federal Withholding: $" << federal_withheld << endl; // Not calculating. Shows $0
cout << "State Withholding: $" << state_withheld << endl; // Not calculating. Shows $0
cout << "Union Dues: $" << union_dues << endl;
cout << "Insurance Deduction: $" << insurance << endl;
cout << "Net Pay: $" << net_pay << endl;
cout << "\nDo you want to do another employee(Y/N)? ";
cin >> again;
} while (again == 'y' || again == 'Y');
cout << "\nHope they enjoy the money!" << endl;
return 0;
}

Code WORKS Math DOESN'T

My code compiles nicely, but the math formulas that I am using aren't providing the right outcome. I need to calculate the balance, withdrawn, and interest for all 3 months. I am also required to validate user's input. For these purposes I am using nested loops. Please let me know if you spot my mistake. Thank you lovely people!
cout << "Please enter the starting balance: ";
cin >> startBalance;
cout << "Please enter the annual interest rate: ";
cin >> annualInterest;
for (int month = 1; month <= 3; month++) {
do {
cout << setprecision(5);
cout << "Please enter the total amount deposited on month " << month << ": ";
cin >> balance;
if (balance <0) {
goodChoice = false;
cout << "\n\t***ERROR " << balance << " ***";
cout << "*** Choice must be positive***\n" << endl;
}
else {
goodChoice = true;
}
startBalance += balance; //need to calculate the balance for all 3 months
} while (!goodChoice);
do {
cout << setprecision(5);
cout << "Please enter the total amount withdrawn on " << month << ": ";
cin >> withdrawn;
if ( (withdrawn <0) || (withdrawn > startBalance) ) {
goodChoice = false;
cout << "***ERROR " << withdrawn << " ***";
cout << "*** Choice must be positive or greater than the balance***" << endl;
}
else {
goodChoice = true;
}
finalWithdrawn += withdrawn; // the total amount of withdrawn
finalBalance = startBalance - withdrawn;
monthInterest = ((startBalance + finalBalance) / 2) * (annualInterest / 12);
totalInterest += monthInterest; //total interest for all 3 months
endBalance = monthInterest + finalBalance;
} while (!goodChoice);
}
cout << "Total Deposit: " << endBalance << endl;
cout << "Total Withdrawn: " << finalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << endBalance << endl;
You have a lot of extra variables defined which aren't needed. Also, your interest rate may have been in percentage instead of a decimal number, i.e. 10% = 0.1. Also, your monthInterest is taking an average then applying interest. I wrote this up and it seems to work.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float totalDeposit = 0.0f;
float totalWithdrawn = 0.0f;
float totalInterest = 0.0f;
float totalBalance = 0.0f;
float interestRate = 0.0f;
setprecision(5); //?
cout << "Enter starting balance: ";
cin >> totalBalance;
cout << "Enter annual interest rate (%): ";
cin >> interestRate;
// Convert to monthly and non-percent; i.e. 10% = 0.1 = 10 / 100
interestRate = interestRate / 100.0f / 12.0f;
for (int month = 1; month <= 3; month++)
{
float deposit = -1.0; // Default to an error state
while (deposit < 0.0)
{
cout << "Enter total deposited in month " << month << ": ";
cin >> deposit;
if (deposit < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
float withdrawn = -1.0f; // Default to an error state
while (withdrawn < 0.0f)
{
cout << "Enter total withdrawn in month " << month << ": ";
cin >> withdrawn;
if (withdrawn < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
totalDeposit += deposit;
totalWithdrawn += withdrawn;
totalBalance = totalBalance + deposit - withdrawn;
totalBalance += totalBalance * interestRate;
totalInterest += totalBalance * interestRate;
}
cout << "Total Deposit: " << totalDeposit << endl;
cout << "Total Withdrawn: " << totalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << totalBalance << endl;
int wait; // Pause so console window doesn't close. Delete this line.
cin >> wait;
return 0;
}

Comparing Cell Phone plans c++

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;
}

Program Running in a Loop

Im trying to figure out why the program runs in a loop. I just can't see how it starts the loop. would like to give you more info but just run the code and do everything right and the program just starts back over but not at the beginning. It will start from when I am selecting the payment option.
here is the code and the code is c++:
#include <iostream>
#include <functional>
using namespace std;
int option, pause, payment, selection, zipcode, error, pin, quantity_chips, quantity_milk, quantity_cola,
quantity_coffee, quantity_pennies, quantity_nickels, quantity_dimes, quantity_quarters, quantity_dollars,
quantity_fives, quantity_tens, quantity_twenties, quantity_total;
float chips, milk, cola, coffee, total, tax, final_cost, pennies, nickels, dimes, quarters,
dollars, fives, tens, twenties, cash_total, owed, change;
void checkout(), debitcard (), creditcard (), cash (), receipt ();
void menu ()
{
cout << "Welcome to Kenjin Xer0's Item Pooper.\n"
<< "10 or less items.\n";
do
{
quantity_total = (quantity_chips) + (quantity_milk) + (quantity_cola) + (quantity_coffee);
cout << "Select from our menu: Selected\n"
<< "\t1. Potato Chip.....$1.50......" << quantity_chips << "\n"
<< "\t2. 2% Milk.........$2.00......" << quantity_milk << "\n"
<< "\t3. Off Brand Cola..$1.00......" << quantity_cola << "\n"
<< "\t4. Dark Coffee.....$2.50......" << quantity_coffee << "\n"
<< "\t5. Check out Total:" << quantity_total << "\n";
cout << "Enter your option: ";
cin >> option;
if (option == 1)
{
cout << "Quantity: ";
cin >> quantity_chips;
chips = (1.5 * quantity_chips);
}
else if (option == 2)
{
cout << "Quantity: ";
cin >> quantity_milk;
milk = (2 * quantity_milk);
}
else if (option == 3)
{
cout << "Quantity: ";
cin >> quantity_cola;
cola = (1 * quantity_cola);
}
else if (option == 4)
{
cout << "Quantity: ";
cin >> quantity_coffee;
coffee = (2.5 * quantity_coffee);
}
else if (option == 5)
{
if (total > 10)
{
cout << "Problem! 10 or less line, Man!\n";
option = 0;
}
else
{
checkout();
}
}
else
{
cout << "Invalid option.\n";
}
}
while (option !=5);
}
void checkout ()
{
do
{
cout << "Select payment option (1:Debit 2:Credit 3:Cash): ";
cin >> payment;
if (payment == 1)
{
debitcard ();
}
else if (payment == 2)
{
creditcard ();
}
else if (payment == 3)
{
cash ();
}
else
{
cout << "Weird Choice, try again.\n";
}
}
while (payment != 1||2||3);
}
void debitcard ()
{
error = 3;
do
{
error--;
cout << "Enter PIN:\n";
cin >> pin;
if (error == 0)
{
cout << "We are sorry but does this card acually belong to you.\n"
<< "Now Leave.\n";
break;
}
else if (pin != 0000)
{
cout << "Wrong. Try Again\n"
<< "You now have " << error << " more tries\n";
}
else
{
receipt();
}
}
while (pin != 0000);
}
void creditcard ()
{
error = 3;
do
{
error--;
cout << "Enter Zip:\n";
cin >> zipcode;
if (error == 0)
{
cout << "We are sorry but does this card acually belong to you.\n"
<< "Now Leave.\n";
break;
}
else if (zipcode != 77523)
{
cout << "Wrong. Try Again\n"
<< "You now have " << error << " more tries\n";
}
else
{
receipt();
}
}
while (zipcode != 77523);
}
void cash ()
{
total = (chips) + (milk) + (cola) + (coffee);
tax = (total * .10);
final_cost = tax + total;
cout << "You owe $"<< final_cost <<".\n";
do
{
cash_total = (pennies) + (nickels) + (dimes) + (quarters) + (dollars) + (fives) + (tens) + (twenties);
owed = final_cost - cash_total;
cout << "Select the Amount:\n"
<< "\t1. Penny................$0.01......" << quantity_pennies << "\n"
<< "\t2. Nickel...............$0.05......" << quantity_nickels << "\n"
<< "\t3. Dime.................$0.10......" << quantity_dimes << "\n"
<< "\t4. Quarter..............$0.25......" << quantity_quarters << "\n"
<< "\t5. One Dollar Bill......$1.00......" << quantity_dollars << "\n"
<< "\t6. Five Dollars Bill....$5.00......" << quantity_fives << "\n"
<< "\t7. Ten Dollar Bill......$10.00....." << quantity_tens << "\n"
<< "\t8. Twenty Dollar Bill...$20.00....." << quantity_twenties << "\n"
<< "\t9. Cash Out\n"
<< "Cash you Have: $" << cash_total << " Still owe: $" << owed << "\n";
cout << "Enter your selection: ";
cin >> selection;
if (selection == 1)
{
cout << "Quantity: ";
cin >> quantity_pennies;
pennies = (0.01 * quantity_pennies);
}
else if (selection == 2)
{
cout << "Quantity: ";
cin >> quantity_nickels;
nickels = (0.05 * quantity_nickels);
}
else if (selection == 3)
{
cout << "Quantity: ";
cin >> quantity_dimes;
dimes = (0.10 * quantity_dimes);
}
else if (selection == 4)
{
cout << "Quantity: ";
cin >> quantity_quarters;
quarters = (0.25 * quantity_quarters);
}
else if (selection == 5)
{
cout << "Quantity: ";
cin >> quantity_dollars;
dollars = (1.00 * quantity_dollars);
}
else if (selection == 6)
{
cout << "Quantity: ";
cin >> quantity_fives;
fives = (5.00 * quantity_fives);
}
else if (selection == 7)
{
cout << "Quantity: ";
cin >> quantity_tens;
tens = (10.00 * quantity_tens);
}
else if (selection == 8)
{
cout << "Quantity: ";
cin >> quantity_twenties;
twenties = (20.00 * quantity_twenties);
}
else if (selection == 9)
{
receipt ();
}
else
{
cout << "Invalid option.\n";
}
}
while (selection != 9);
}
void receipt ()
{
total = (chips) + (milk) + (cola) + (coffee);
tax = (total * .10);
final_cost = tax + total;
change = owed * -1;
cout << "Receipt Take:\n";
if (quantity_chips > 0)
{
cout << "Potato Chips: $1.50 x " << quantity_chips << " = $" << chips << endl;
}
if (quantity_milk > 0)
{
cout << "2% Milk: $2.00 x " << quantity_milk << " = $" << milk << endl;
}
if (quantity_cola > 0)
{
cout << "Off Brand Cola: 1.00 x " << quantity_cola << " = $" << cola << endl;
}
if (quantity_coffee > 0)
{
cout << "Dark Coffee: $2.50 x " << quantity_coffee << " = $" << coffee << endl;
}
cout << "Tax (10.0%): $" << tax << endl;
cout << "Total: $" << final_cost << endl;
cout << "Change Returned: $" << change << endl;
}
int main ()
{
menu ();
return 0;
}
in ur checkout function
replace
while (payment != 1||2||3)
by
while (payment != 1 && payment != 2 && payment != 3)
The while loop that you have in checkout() is incorrect. You have:
while (payment != 1||2||3);
Which is not how you do mutiple or's. You would need to write it as
while (payment != 1 || payment != 2 || payment != 3);
To remove the confusing set of conditions that you've done to exit the do-while loop in the checkout function, this can be done instead:
void checkout ()
{
bool choice_made;
do
{
choice_made = true;
cout << "Select payment option (1:Debit 2:Credit 3:Cash): ";
cin >> payment;
if (payment == 1)
{
debitcard ();
}
else if (payment == 2)
{
creditcard ();
}
else if (payment == 3)
{
cash ();
}
else
{
cout << "Weird Choice, try again.\n";
choice_made = false;
}
}
while (!choice_made);
}
You only exit the loop until a valid choice is made. A simple boolean is all you need to control the logic.
As to your attempt, this line:
while (payment != 1||2||3)
applies the logical or to the values of 1, 2, and 3. The result is always true, which is 1. So what it all boils down to is
while (payment != 1)