for some reason my subtotal variable doesn't store the prices of the items inputted by the user any suggestions? I don't know if I set up my loop wrong or if the subtotal has to be put outside the if else statements but then I don't know how I would know what to store from users input
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
const double tax_rate = 0.05;
struct menuItemType
{
string ItemName;
double ItemPrice;
};
void getData(ifstream &in, menuItemType menuList[]);
void showMenu(menuItemType menuList[]);
void printCheck(menuItemType menuList[]);
int main()
{
menuItemType menuList[10];
menuList[10].ItemName;
menuList[10].ItemPrice;
ifstream in;
in.open("Menu.txt");
cout << "Welcome to Johnny's Breakfast Diner!" << endl;
getData(in,menuList);
showMenu(menuList);
return 0;
}
void getData(ifstream &in, menuItemType menuList[])
{
int i = 0;
while (!in.eof())
{
in >> menuList[i].ItemName >> menuList[i].ItemPrice;
i++;
}
}
void showMenu(menuItemType menuList[])
{
int j = 0;
char answ;
cout << fixed << setprecision(2);
cout << "Would you like to take a look at our menu? (Y/N)";
cin >> answ;
if (answ == 'Y' || answ == 'y')
{
cout << left << setw(10) << "Item#" << left << setw(15) << "Item" << left << setw(18) << "Price" << endl;
do {
{
cout << left << setw(8) << j << " " << left << setw(15) << menuList[j].ItemName << " " << "$" << menuList[j].ItemPrice << endl;
j++;
}
} while (j < 8);
printCheck(menuList);
}
else if (answ == 'N' || answ == 'n')
{
system("cls");
cout << "Have a good day!" << endl;
}
}
void printCheck(menuItemType menuList[])
{
char answ;
int choice;
bool menu = true;
double subtotal = 0;
double tax = (subtotal * tax_rate);
double total = (tax + subtotal);
cout << "Would like to place your order (Y/N)";
cin >> answ;
if (answ == 'Y' || answ == 'y')
{
cout << "Please enter the number of the item, 8 to finish order:";
do {
cin >> choice;
if (choice == 0)
{
cout << menuList[0].ItemName << " " << "$" << menuList[0].ItemPrice << endl;
subtotal = subtotal + menuList[0].ItemPrice; \\ for some reason here it doesn't store the prices have no idea why
}
else if (choice == 1)
{
cout << menuList[1].ItemName << " " << "$" << menuList[1].ItemPrice << endl;
subtotal = subtotal + menuList[1].ItemPrice;
}
else if (choice == 2)
{
cout << menuList[2].ItemName << " " << "$" << menuList[2].ItemPrice << endl;
subtotal = subtotal + menuList[2].ItemPrice;
}
else if (choice == 3)
{
cout << menuList[3].ItemName << " " << "$" << menuList[3].ItemPrice << endl;
subtotal = subtotal + menuList[3].ItemPrice;
}
else if (choice == 4)
{
cout << menuList[4].ItemName << " " << "$" << menuList[4].ItemPrice << endl;
subtotal = subtotal + menuList[4].ItemPrice;
}
else if (choice == 5)
{
cout << menuList[5].ItemName << " " << "$" << menuList[5].ItemPrice << endl;
subtotal = subtotal + menuList[5].ItemPrice;
}
else if (choice == 6)
{
cout << menuList[6].ItemName << " " << "$" << menuList[6].ItemPrice << endl;
subtotal = subtotal + menuList[6].ItemPrice;
}
else if (choice == 7)
{
cout << menuList[7].ItemName << " " << "$" << menuList[7].ItemPrice << endl;
subtotal = subtotal + menuList[7].ItemPrice;
}
else if (choice == 8)
{
break;
}
}while(menu = true);
cout << "Taxes" << "$" << tax << endl;
cout << "Amount Due" << "$" << total << endl;
}
else if (answ == 'N' || answ == 'n')
{
system("cls");
cout << "Ok, maybe I can help you at a later time." << endl;
}
}
It looks like you're trying to use subtotal before you've actually put data into it.
The problem is these lines:
double tax = (subtotal * tax_rate);
double total = (tax + subtotal);
At that point in the program, subtotal still contains the initial value, which is 0, so the result of those calculations is also 0. You need to put those lines after the loop so that they work with the final value of subtotal.
What's the result. the subtotal will have
menuList[choice].ItemPrice value
if you want to change
subtotal += menuList[choice].ItemPrice;
Related
I'm currently in a class that wants me to make a craps game.
The problem is in int main on the second while statement comparing the point with the roll. It ignores the if statement and does the loop again, even though it hits the point or 7. Sometimes it works like it should and other times it repeats the loop a few times.
#include "pch.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int diceRoll() {
int x = 0, y = 0;
x = rand() % 6 + 1;
y = rand() % 6 + 1;
cout << "You rolled a " << x << " and a " << y << " which comes out to ------> " << x + y << " <-------" << endl;
return x + y;
}
bool playAgain() {
char ans;
cout << "Do you want to play again ?? Y to continue, N to quit." << endl;
cin >> ans;
while (ans != 'Y' || ans != 'y' || ans != 'n' || ans != 'N') {
if (ans == 'Y' || ans == 'y')
{
return true;
}
if (ans == 'N' || ans == 'n')
{
return false;
}
cout << "Do you want to play again ?? Y to continue, N to quit." << endl;
cin >> ans;
}
}
int main()
{
srand(time(NULL));
int dices, bid, point = 0;
int money = 50;
bool gameRunning = true;
bool didTheyWin;
while (gameRunning == true) {
if (money == 0) {
cout << "You have no money, ending game." << endl;
break;
}
cout << "Please enter a bid. You currently have $" << money << endl;
cout << "$";
cin >> bid;
while (bid > money) {
cout << "Please bet below your current balance: $" << money << endl;
cout << "$";
cin >> bid;
}
dices = diceRoll();
didTheyWin = false;
if ((dices == 7) || (dices == 11)) {
cout << "You won $" << bid << " !" << endl;
money = money + bid;
}
else if ((dices == 2) || (dices == 3) || (dices == 12)) {
cout << "You LOSE! You lost $" << bid << " !" << endl;
money = money - bid;
}
else {
point = dices;
cout << "The target number is > " << point << " <" << endl;
cout << "If you hit a 7 you lose, if you hit the target you win. \nYou automatically roll until one of these two things happen.\n";
while (didTheyWin == false) {
diceRoll();
dices = diceRoll();
if (dices == point) {
cout << "You won $" << bid << " !" << endl;
money = money + bid;
cout << "You now have $" << money << endl;
didTheyWin = true;
}
else if (dices == 7) {
cout << "You LOSE! You lost $" << bid << " !" << endl;
money = money - bid;
cout << "You now have $" << money << endl;
didTheyWin = true;
}
}
}
gameRunning = playAgain();
}
cout << "Thanks for playing. _END_" << endl;
return 0;
}
You call diceRoll twice, and ignore what you get back from the first call. You'll see the results of that first roll displayed, but they'll be ignored and you'll roll again.
So I am trying to write a program that takes the total cost of a set of books given a certain value and then loop back to the beginning in order to get the total of a new set of books until the user tells it to stop. At the end of the program a total of the total cost of all books will be displayed. I am stuck trying to write this, thanks for any help with this, and sorry for my messy code as I am still learning.
Code Listed Here:
#include <iostream>
using namespace std;
const float kReqNew = 0.90;
const float kReqOld = 0.65;
const float kOptNew = 0.40;
const float kOptOld = 0.20;
class Book {
private:
float singleCost_;
int NeworOld_;
int ReqorOpt_;
int TotalBooksNeeded_;
int BookNumber_;
int NumOnHand_;
int prospectiveEnrole;
double totalCost_;
public:
void buildData();
void determineBooks();
void totalCosts();
void layOut();
void totalCostAdd();
};
void Book::buildData() {
cout << "Please enter: " << endl;
cout << "Book Number:";
cin >> BookNumber_;
cout << "Book Price: ";
cin >> singleCost_;
cout << "How many are in stock: ";
cin >> NumOnHand_;
cout << "Prospective Enrolment: ";
cin >> prospectiveEnrole;
cout << "Required(1), Optional(0)";
cin >> ReqorOpt_;
cout << "New(1), Old(0): ";
cin >> NeworOld_;
}
void Book::determineBooks(){
if(NeworOld_ == 1 && ReqorOpt_ == 1){
TotalBooksNeeded_ = NumOnHand_ * kReqNew;
}
else if(NeworOld_ == 1 && ReqorOpt_ == 0){
TotalBooksNeeded_ = NumOnHand_ * kOptNew;
}
else if(NeworOld_ == 0 && ReqorOpt_ == 1){
TotalBooksNeeded_ = NumOnHand_ * kReqOld;
}
else if(NeworOld_ == 0 && ReqorOpt_ == 0){
TotalBooksNeeded_ = NumOnHand_ * kOptOld;
}
cout << "Total Number of Books Needed: " << "$" << TotalBooksNeeded_ << endl;
}
void Book::totalCosts(){
totalCost_ = TotalBooksNeeded_ * singleCost_;
cout << "The total cost of the books is: " << totalCost_ << endl;
}
void Book::layOut(){
cout << BookNumber_<<endl;
cout << "$" << singleCost_ << endl;
cout << NumOnHand_ << endl;
}
void Book::totalCostAdd(){
double* newTotalCost;
*newTotalCost += totalCost_;
cout << newTotalCost << endl;
}
int main() {
Book books;
int exitNum;
cout << "\t\t\t ****************\n" << "\t\t\t Book Calculator\n" << "\t\t\t ****************\n";
do{
cout << "*******************************************\n";
books.buildData();
cout << "*******************************************\n";
books.layOut();
cout << "*******************************************\n";
books.determineBooks();
books.totalCosts();
cout << "*******************************************\n";
cout << "Would you like to continue: Yes(1), No(0)" << endl;
cin >> exitNum;
cout << "*******************************************\n";
books.totalCostAdd();
} while(exitNum != 0);
}
Thanks again for any help.
New to C++. I have a homework program I am programming. It is all but done, but our teacher wants us to add error catching to the program.
The issue arises in the code for asking the user to run again. I use a while loop to monitor a variable until it changes.
This works:
#include <iostream>
using namespace std;
int main() {
char runAgainYN;
while ( toupper(runAgainYN != 'N' ) {
// Do some stuff here!
cout << "Would you like to run again?";
cin >> runAgainYN;
}
return 0;
}
It keeps looping the program until runAgain is equal to 'N', then stops. Now, I modified the program to utilize some error correction for the question about running the program again, to limit the user to only entering Y or N. Here is the updated code:
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char runAgainYN;
bool runAgain = true;
while ( runAgain ) {
// Do some stuff here!
bool validResponse = false;
while ( !validResponse ) {
cout << "Would you like to run the program again (Y/N): ";
cin >> runAgainYN;
if ( toupper(runAgainYN) == 'Y' ) {
validResponse = true;
cin.ignore();
}
else if ( toupper(runAgainYN) == 'N' ) {
runAgain = false;
validResponse = true;
cin.ignore();
}
else {
cout << "INVALID RESPONSE" << endl;
}
}
}
return 0;
}
Here is where the problem arises. If the user enters 'N', the program exits with code 0, and if the user enters anything but Y or N, the invalid response triggers and asks for the input again. But, if the user enters Y, the program exits with code -1. Huh? I tried a different approach, with the same result:
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char runAgainYN;
do {
// Do some stuff here!
bool validResponse = false;
while ( !validResponse ) {
cout << "Would you like to run the program again (Y/N): ";
cin >> runAgainYN;
if ( toupper(runAgainYN) == 'Y' ) {
validResponse = true;
cin.ignore();
}
else if ( toupper(runAgainYN) == 'N' ) {
runAgain = false;
validResponse = true;
cin.ignore();
}
else {
cout << "INVALID RESPONSE" << endl;
}
}
}
while ( runAgain );
return 0;
}
Any help guys? Thanks!!!
OK, so it seems to be something in the actual program causing it. Here's the source code:
#include <iostream>
#include <string>
#include <string.h>
#include <iomanip>
#include <cctype>
#include <limits>
#include <algorithm>
#include <fstream>
using namespace std;
void cls();
void printLine( int length );
void showCurrency( double dv, int width = 14 );
int main() {
string itemName[999][2];
double itemPrice[999][3];
double salesTotal = 0.0;
double salesTax = 0.0;
double totalTax = 0.0;
double taxRate = 0.0;
double grandTotal = 0.0;
double test = 0.0;
int numLines = 0;
string readLine;
string temp;
ifstream fileIn;
string menuHeader = "Sales Receipt from File";
char runAgainYN;
bool runAgain = true;
do { // Loop until runAgain false
// Open the file and count the number of lines, then close for next operation:
fileIn.open("cost.txt");
while (!fileIn.eof()) {
fileIn >> temp;
numLines++;
temp = "";
}
fileIn.close();
// Open the file and move the data into the arrays, then close:
fileIn.open("cost.txt");
for ( int i = 0; i < numLines; i++) {
fileIn >> itemName[i][1] >> itemPrice[i][1];
}
fileIn.close();
cls();
numLines = numLines / 2;
cout << "/";
printLine(80);
cout << "\\" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "|" << setw(41 + (menuHeader.length() / 2)) << menuHeader << setw(40 - (menuHeader.length() / 2)) << "|" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "\\";
printLine(80);
cout << "/" << endl << endl;
cout << "Enter the sales tax percentage (ie for 6% enter 6): ";
// Ask for taxRate and error check:
while (!(cin >> taxRate)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "INVALID RESPONSE" << endl << "Please enter a number: ";
}
cout << endl;
salesTax = taxRate / 100; // Convert sales tax to percentage
for (int i = 0; i < numLines; i++ ) { // Set the running tax amounts
itemPrice[i][2] = itemPrice[i][1] * salesTax;
salesTotal = salesTotal + itemPrice[i][1];
totalTax = totalTax + itemPrice[i][2];
}
//totalTax = salesTotal * salesTax; // Calculate tax
grandTotal = salesTotal + totalTax; // Calculate grand total
// Output:
cls();
cout << "/" << setfill('-') << setw(63) << "-" << "\\" << setfill(' ') << endl;
cout << "| SALES RECEIPT |" << endl;
cout << "|" << setfill('-') << setw(63) << "-" << "|" << setfill(' ') << endl;
cout << "| " << left << setw(32) << "Sales item" << setw(13) << right << "Price" << setw(18) << "Tax |" << endl;
cout << "|" << setfill('-') << setw(63) << "-" << "|" << setfill(' ') << endl;
for ( int i = 0; i <= numLines - 1; ++i ){
cout << "| " << left << setw(32) << itemName[i][1] << "$" << setw(12) << setprecision(2) << fixed << right << itemPrice[i][1] << setw(5) << "$" << setw(11) << itemPrice[i][2] << " |" << endl;
}
cout << "|" << setfill('-') << setw(63) << "-" << "|" << setfill(' ') << endl;
cout << "| Total Sales" << setw(36);
showCurrency(salesTotal);
cout << " |" << endl;
cout << "| Sales Tax (" << setprecision(0) << fixed << taxRate << "%)" << setw(33);
showCurrency(totalTax);
cout << " |" << endl;
cout << "|" << setfill('-') << setw(63) << "-" << "|" << setfill(' ') << endl;
cout << "| Grand Total" << setw(36);
showCurrency(grandTotal);
cout << " |" << endl;
cout << "\\" << setfill('-') << setw(63) << "-" << "/" << setfill(' ') << endl;
cout << endl;
// Clear vars and array for next run:
salesTax = 0.0;
totalTax = 0.0;
salesTotal = 0.0;
grandTotal = 0.0;
memset(itemPrice, 0, sizeof(itemPrice));
memset(itemName, 0, sizeof(itemName));
// Ask if program is to be run again:
bool validResponse = false;
while ( !validResponse ) {
cout << "Would you like to enter a new tax rate (Y/N): ";
cin >> runAgainYN;
if ( toupper(runAgainYN) == 'Y' ) {
validResponse = true;
cin.ignore();
}
else if ( toupper(runAgainYN) == 'N' ) {
runAgain = false;
validResponse = true;
cin.ignore();
}
else {
cout << "INVALID RESPONSE" << endl;
}
}
}
while ( runAgain == true );
return 0;
}
void printLine( int length ) {
for ( int i = 0; i < length; i++ ) {
cout << "=";
}
}
void cls() {
// check OS and run correct clear screen (I do some of my coding in Linux :)
#if (defined (_WIN32) || defined (_WIN64))
system("CLS");
#elif (defined (LINUX) || defined (__linux__))
system("clear");
#endif
}
void showCurrency(double dv, int width){
/* Credit where credit is due:
* The following code snippet was found at https://arachnoid.com/cpptutor/student3.html
* Copyright © 2000, P. Lutus. All rights reserved.
*/
const string radix = ".";
const string thousands = ",";
const string unit = "$";
unsigned long v = (unsigned long) ((dv * 100.0) + .5);
string fmt,digit;
int i = -2;
do {
if(i == 0) {
fmt = radix + fmt;
}
if((i > 0) && (!(i % 3))) {
fmt = thousands + fmt;
}
digit = (v % 10) + '0';
fmt = digit + fmt;
v /= 10;
i++;
}
while((v) || (i < 1));
cout << unit << setw(width) << fmt.c_str();
}
And here is the contents of 'cost.txt':
Books 45.01
Pens 21.03
Pencils 10.90
Hats 50.00
Caps 800.00
Food 1.00
OK looks like i got it. The file operations were within the do loop. I moved them outside of the loop, and it all works. Thanks!
I am stuck.
I have been given this problem: http://i.imgur.com/1U8PjY4.png?1
The code I've written so far is this:
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
char cIn;
char full;
int capacity = 750;
int numVans = 1;
float heaviestVan = 0;
float payload = 0;
float parcelWeight;
bool emptyBelt = false;
int main()
{
bool end = false;
while(!end)
{
start:
cout << string(25, '\n');
cout << endl << "Pauls Premier Parcels (PPP)" << endl << endl;
cout << "Van being loaded is number: " << numVans << endl << endl;
cout << "The payload of van " << numVans << " is currently " << payload << " / 750kg" << endl << endl;
cout << "Is the belt full? ('Y' or 'N'): ";
cin >> full;
if (full == 'Y' || 'y')
{
while (!emptyBelt)
{
cout << endl << endl << "Please enter the weight of the next parcel: ";
cin >> parcelWeight;
if (parcelWeight > 120)
{
cout << "The maximum parcel weight is 120kg, please weigh a different parcel: ";
cin >> parcelWeight;
}
if (payload + parcelWeight <= capacity)
{
payload = payload + parcelWeight;
cout << endl << "The parcel has been loaded onto the van" << endl << endl;
goto start;
}
else
{
cout << endl << "The current van has reached capacity and is being dispatched" << endl;
//numVans = numVans + 1;
if(payload > heaviestVan)
{
heaviestVan = payload;
}
payload = 0;
cout << endl << endl << endl << "Vans dispatched: " << numVans;
cout << endl << endl << "Weight of heaviest van: " << heaviestVan;
}
}
}
}
return 0;
}
I need to implement a statement asking the user to place parcels on the belt if the belt is empty, right now It just continues running the program.
Also the user could enter anything besides Y or y and the program would still run.
Try rewriting
if (full == 'Y' || 'y')
to
if ((full == 'Y') || (full == 'y'))
Some explanation:
if (full == 'Y' || 'y')
is the same as
if ((full == 'Y') || ('y'))
which is the same as
if ((full == 'Y') || true)
which is the same as
if (true)
regardsless of the value of the variable full.
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)