this is a project I'm working on which comes from the book I'm using to learn C++ - "Starting out with C++". I'm having a problem with the cashier portion of the project at the moment. It asks the user to enter the date, quantity, isbn, title, and price of the book. Then, it asks the user if they wish to enter another book. Regardless of whether they type "y" or "n" it continues to the next part of the program. I don't really know why the for loop doesn't repeat after I type "y" to enter another book. Also, the date is coming out with garbage at the end when it is displayed, that's another thing I need to fix. Any help would be appreciated. There is definitely more problems but the main problem is in the cashier function in the first for loop. I didn't include the whole program because it's very long.
/*
* mainmenu.cpp
* Serendipity Booksellers software
*
* Created by Abraham Quilca on 9/5/12.
* Copyright 2012 __MyCompanyName__. All rights reserved.
*
*/
#include<iostream>
#include<iomanip>
#include<cstring>
#include"mainmenu.h"
using namespace std;
char bookTitle[20][51],
isbn[20][14],
author[20][31],
publisher[20][31],
dateAdded[20][11];
int qtyOnHand[20];
double wholesale[20];
double retail[20];;
int main()
{
int choice;
do
{
cout << "\t\t Serendipity Booksellers"<< endl;
cout << "\t\t\t Main Menu" << endl << endl;
cout << "\t\t1. Cashier Module" << endl;
cout << "\t\t2. Inventory Database Module" << endl;
cout << "\t\t3. Report Module" << endl;
cout << "\t\t4. Exit" << endl << endl;
cout << "\t\tEnter your choice: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
cashier();
break;
case 2:
invmenu();
break;
case 3:
reports();
break;
case 4:
continue;
break;
default:
cout << "\t\tPlease enter a number in the range 1-4." << endl << endl;
}
}
while(choice != 4);
cout << "\t\tYou selected item 4." << endl;
return 0;
}
// Cashier function
void cashier()
{
char again;
char date[8];
int quantity[20] = {0};
char ISBN[20][20] = {0};
char title[20][40] = {0};
float price[20] = {0}, bookTotal[20] = {0}, subtotal, total, tax;
const float tax_rate = .06;
cout << "Serendipity Booksellers" << endl;
cout << " Cashier Module" << endl << endl;
for(int count = 0; count < 20; count++)
{
cout << "Date: ";
cin >> date;
cout << "Quantity of Book: ";
cin >> quantity[count];
cout << "ISBN: ";
cin >> ISBN[count];
cout << "Title: ";
cin.ignore();
cin.getline(title[count], 40);
cout << "Price: ";
cin >> price[count];
bookTotal[count] = quantity[count] * price[count];
subtotal += price[count];
cout << "Would you like to enter another book? (Y/N) ";
cin >> again;
if(again == 'N' || 'n')
count = 21; // This line will end the for loop
}
// Calculating tax and total
tax = subtotal * tax_rate;
total = subtotal + tax;
cout << "\n\nSerendipity Booksellers" << endl << endl;
cout << "Date:" << date << endl << endl;
cout << "Qty\t ISBN\t\t "
<< left << setw(40) << "Title" << "Price\t Total" << endl
<< "-------------------------------------------------------------------------------"
<< endl << endl;
for(int count = 0; count < 20; count++)
{
cout << quantity[count] << "\t " << ISBN[count] << " " << left << setw(40) << title[count]
<< setprecision(2) << fixed << "$" << setw(6) << price[count] << " $" << setw(6) << bookTotal[count]
<< endl << endl;
}
cout << "\t\t\t Subtotal" << "\t\t\t\t $" << setw(6) << subtotal << endl;
cout << "\t\t\t Tax" << "\t\t\t\t $" << setw(6) << tax<< endl;
cout << "\t\t\t Total" "\t\t\t\t $" << setw(6) << total << endl << endl;
cout << "Thank You for Shopping at Serendipity!" << endl << endl;
}
if(again == 'N' || 'n')
This doesn't do what you think it does. Look at it like this:
if((again == 'N') || ('n'))
Is again == N true OR is n true? Well n will always be true (it is a char with non-zero value) so your loop will always end immediately. What you want is:
if(again == 'N' || again == 'n')
Also, you can break out of a loop using the aptly named break keyword:
if (again == 'N' || again == 'n') {
break;
}
The problem with the loop is this line:
if(again == 'N' || 'n')
C++ doesn't know that you mean it to check again against both characters. Instead, it tries again == 'N', which fails, and then tries 'n', which - not being zero - evaluates as true.
Instead, try:
if (again == 'N' || again == 'n')
break;
Related
It is me again. I want to add the prices in the purchase if I want to buy more items. But I do not know how to do that. For example if, I confirm my purchase and want to buy more items, I want it to add the prices that is confirmed to purchase, and if I finally do not want to buy more items and not to look for more items, that total price would be computed.
int main()
{
int choice;
int purchase;
int quantity;
double totalChoice1;
double totalChoice2;
char view;
char confirm;
char buyMore;
char look;
double alloy, apex, kraken, aorus;
double oppo, alpha, rog, huawei;
double ps4, nintendo, xbox, wii;
alloy = 69.99;
apex = 199;
kraken = 90;
aorus = 60;
do {
cout << "What type of items would you like to view?" << endl;
cout << " [1] Peripherals" << endl;
cout << " [2] Mobile Phones" << endl;
cout << " [3] Consoles" << endl;
cout << " [4] Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
cout << "--------------------" << endl;
cout << "What peripherals would you like to purchase?" << endl;
cout << "[1] HyperX Alloy FPS PRO - $69.99" << endl;
cout << "[2] SteelSeries APEX PRO - $199" << endl;
cout << "[3] Razer Kraken X - $90" << endl;
cout << "[4] AORUS K7 - $60" << endl;
cout << "[5] BACK TO MENU" << endl;
cout << "Enter your choice: ";
cin >> purchase;
cout << "--------------------" << endl;
if (purchase == 1) {
cout << "How many would you like to purchase? ";
cin >> quantity;
totalChoice1 = quantity * alloy;
cout << "The total price for that is " << totalChoice1 << endl;
cout << "Confirm the Purchase? [Y]/[N]: ";
cin >> confirm;
if (confirm == 'Y') {
totalChoice1; // This is just a trial code.
cout << "Would you like to buy more items? [Y]/[N]: ";
cin >> buyMore;
}
else if (confirm == 'N') {
cout << "Do you still want to look for items? [Y]/[N]: ";
cin >> look;
if (look == 'N') {
break;
}
}
else {
break;
}
}
}
while (purchase == 5 || buyMore == 'Y' || look == 'Y');
cout << "The total price for your items is: " << totalChoice1; // This is also a trial code (totalChoice1)
}
You are simply missing a variable to keep track of that total. Don't forget to give it an initial value of 0!
There are plenty of minor other issues with your code, so you'll have some more learning ahead. For instance, we find it easier to do bookkeeping in cents, because you can treat them as integers.
I need to create arrays that save the quantity of the item the user selects and also prints out a receipt with the product, quantity and the total price. Please help me understand how to do this. I've got a basic understanding of what an array is. I just couldn't figure out how to save the users input.
#include <iostream>
#include <Windows.h>
#include <cstdlib>
#include <string>
#include "customerclass.h"
using namespace std;
//***** Functions to calculate the price of multiple items *****
void finalPrice1(int itemQuantity) {
float price;
price = itemQuantity * 3.00;
cout << "Your total is $" << price << endl;
cout << "Thank you for using my shop" << endl;
exit(0);
}
void finalPrice2(int itemQuantity) {
float price;
price = itemQuantity * 2.50;
cout << "Your total is $" << price << endl;
cout << "Thank you for using my shop" << endl;
exit(0);
}
void finalPrice3(int itemQuantity) {
float price;
price = itemQuantity * 1.25;
cout << "Your total is $" << price << endl;
cout << "Thank you for using my shop" << endl;
exit(0);
} //***** End of functions that calculate price of multiple items *****
int main(void)
{
char selection = ' ';
string lname = "";
string luserAddress;
int itemQuantity;
string orderFinalized;
CustomerInfo myCustomerInfo;
do
{ // Displaying menu
cout << "Hello, welcome to my online shop! What is your name? " << endl;
cin >> lname;
cout << " And what is your shipping address? " << endl;
cin >> luserAddress;
myCustomerInfo.setName(lname);
myCustomerInfo.setAddress(luserAddress);
cout << lname + ", nice to meet you. Here are the items in my shop followed by the price, please enter the number that corresponds to the item you want. \n " << endl;
cout << "Products \n";
cout << "1 - Chocolate candy bar - $3.00" << endl;
cout << "2 - Sour hard candy - $2.50" << endl;
cout << "3 - Mints - $1.25" << endl;
cout << "4 - Exit" << endl << endl;
cout << "Enter selection ";
// Reading User Selection
cin >> selection;
switch (selection)
{
case '1':
cout << "You've chosen a Chocolate candy bar. How many would you like? ";
cin >> itemQuantity;
cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl;
cin >> orderFinalized;
if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES") {
cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl;
cout << "Printing your receipt now..." << endl;
finalPrice1(itemQuantity);
}
break;
case '2':
cout << "You've chosen Sour hard candy. How many would you like? ";
cin >> itemQuantity;
cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl;
cin >> orderFinalized;
if (orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES") {
cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl;
cout << "Printing your receipt now..." << endl;
finalPrice2(itemQuantity);
}
break;
case '3':
cout << "You've chosen Mints. How many would you like? ";
cin >> itemQuantity;
cout << "Ok, will this finalize your order? Type and enter either 'Yes' or 'No' " << endl;
cin >> orderFinalized;
if (orderFinalized == "Yes" || "yes" || "YES") {
cout << myCustomerInfo.getName() + " your items will be shipped to " << myCustomerInfo.getAddress() << endl;
cout << "Printing your receipt now..." << endl;
finalPrice3(itemQuantity);
}
break;
case '4':
cout << "Thank you for using my shop. <exiting now...>" << endl;
break;
default: cout << "Invalid selection. Please try again";
}
cout << endl << endl;
} while (selection != '4');
return 0;
}
You need dynamic array. For example:
cin >> itemQuantity;
// create a array during runtime
// and the size is itemQuantity
// you can access the ith array element by items[i]
Item *items= new Item[itemQuantity];
Or you can use the vector,
vector<Item> items;//you can also access the ith element by items[i]
items.push_back(hard_candy);//items = {hard_candy}
items.push_back(soft_candy);//items = {hard_candy, soft_candy}
items.pop_back();//items = {hard_candy}
BTW, the case 3 in your code has some error:
orderFinalized == "Yes" || "yes" || "YES"//wrong
orderFinalized == "Yes" || orderFinalized == "yes" || orderFinalized == "YES"//right
Just needed some help with my program.
I keep getting an error with my if statement.
Could you help me out please.
This is the code I have written.
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <string>
char carChoice;
int random_integer = rand();
int pricePerDay;
int daysOfHire;
int totalCost;
int age;
int telephoneNumber[30];
char name[30];
char address[30];
char response [4];
using namespace std;
int main()
{
cout << "Royal Rentals" << endl;
cout << "---------------------------------------------"<<"\n" <<endl;
cout << "Current Stock: " <<endl;
cout << "A - Lamborghini Aventador" <<endl;
cout << "B - Lamborghini Huracan" <<endl;
cout << "C - Mercedes Benz AMG GT S" <<endl;
cout << "D - Audi R8 V10" <<endl;
cout << "Which car would you like to rent (A, B, C or D)" << "\n" << endl;
cin >> carChoice;
cout << "How many days would you like to hire the car for" << "\n" << endl;
cin >> daysOfHire;
cout << "How old are you?" << "\n" << endl;
cin >> age;
if (age < 21)
{
cout << "Sorry but you have to over the age of 21 to hire our super cars" << "\n" << endl;
cout << "Please close the program to hire a car suitable for your age" << "\n" << endl;
exit (0);
}
cout << "What is your name?" << "\n" << endl;
cin >> name;
cout << "Have you got a full drivers license?" "\n" << endl;
cin >> response;
if (response == 'Y' //not "Y" but only one 'Y')
{
cout << "blah"<< "\n" <<endl;
}
else if (response == 'N')
{
cout << "blah"<< "\n" << endl;
}
cout << "what is your address?" << "\n" << endl;
cin >> address;
cout << "what is your telephone number?"<< "\n" << endl;
cin >> telephoneNumber;
if (carChoice == 'a')
{
totalCost = daysOfHire * 685;
cout << "You have chosen the following car for hire: Lamborghini Aventador" << endl;
cout << "The price per day is 685.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'b')
{
totalCost = daysOfHire * 585;
cout << "You have chosen the following car for hire: Lamborghini Huracan" << endl;
cout << "The price per day is �585.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'c')
{
totalCost = daysOfHire * 485;
cout << "You have chosen the following car for hire: Mercedes Benz AMG GT S" << endl;
cout << "The price per day is �485.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'd')
{
totalCost = daysOfHire * 445;
cout << "You have chosen the following car for hire: Audi R8 V10" << endl;
cout << "The price per day is �445.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else
{
cout << "You have entered an invalid response" << endl;
}
}
The statement is to verify if you have a valid driving license, if you don't then the program should display a message and close. If they do then it should continue so they can enter the rest of their details.
What am I missing from it and could some correct my if statement.
Many thanks,
Irbaaz
if (response == 'Y')
Response is an array, you cannot compare an array with an integer. If you want to check that the whole string consists of one 'Y' symbol, you should check
Strlen(response) == 1 && response[0] == 'Y'
Ok, so for my school project we are basically making a menu with 20 max people to enter information and change if need be. Everything was working fine. However, our assignment has us check input for zip code and Account Balance for integer values. I used a do-while loop for ZipCode validation until a positive number and a digit was entered. However, I get an infinite loop that I can't seem to fix. Here is my code. The error lies on lines 57-68 if you put into a compiler. Every time I enter a letter instead of a integer, I get an infinite loop. But I can't figure out why. Thanks!
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
struct Account //Structure to be used throughout
{
string CustomerName;
string CustomerAddress;
string City;
string State;
int ZIPCode;
string Telephone;
int AccountBalance;
string DateOfLastPayment;
};
//function prototypes
void valueChangeFunc(string, Account[], int);//This function will be used in option 2 for editing a certain customer information
int main()
{
Account array[20]; //Array to hold up to 20 customers
int choice; //this will hold which option the user decides to make 1-4
bool flagZip = false; //This will be used later on as a flag for a do-while loop
bool flag = false; //This will be used as a flag for the do-while loop right now
int index = 0; //Index for our customers. This tells how many customers have been entered
do //This do while loop will continue to ask the user what option to do until array fills up with 20 people and 4 is not entered
{
cout << "1. Enter new account information \n" <<endl
<< "2. Change account information \n" << endl
<< "3. Display all account information\n" <<endl
<< "4. Exit the program \n" <<endl;
cin >> choice;
if (choice > 4 || choice <= 0)//If user enters a number bigger than 4 or less then or equal to 0. Error!
cout << "Please enter a number between 1 and 4" << endl;
else if(choice == 1)
{
cout << "CustomerName: ";
cin.ignore();
getline(cin, array[index].CustomerName);
cout << "CustomerAddress ";
getline(cin, array[index].CustomerAddress);
cout << "City: ";
getline(cin, array[index].City);
cout << "State: ";
getline(cin, array[index].State);
do
{
cout << "Zip Code: ";
cin >> array[index].ZIPCode;
cin.ignore();
if (!isdigit(array[index].ZIPCode) && array[index].ZIPCode <= 0)
cout << "Please enter a valid entry " << endl;
else
flagZip = true;
}while(flagZip == false);
cout << "Telephone: ";
getline(cin, array[index].Telephone);
flagZip = false;
do
{
cout << "AccountBalance: ";
cin >> array[index].AccountBalance;
cin.ignore();
if (array[index].AccountBalance <= 0)
cout << "Please enter a valid entry " << endl;
else
flagZip = true;
}while(flagZip == false);
cout << "DateOfLastPayment: ";
getline(cin, array[index].DateOfLastPayment);
cout << "\n\nCustomerName: " << array[index].CustomerName << endl;
cout << "CustomerAddress " << array[index].CustomerAddress <<endl;
cout << "City: " << array[index].City << endl;
cout << "State: " << array[index].State << endl;
cout << "Zip Code: " << array[index].ZIPCode << endl;
cout << "Telephone: " << array[index].Telephone <<endl;
cout << "AccountBalance: " << array[index].AccountBalance << endl;
cout << "DateOfLastPayment: " << array[index].DateOfLastPayment << endl;
cout << "You have entered information for customer number " << index << endl << endl;
index++;
}
else if(choice == 2 && index != 0)
{
int num;
string valueChange;
do
{
cout << " Customer number: ";
cin >> num;
if (num > (index-1) || num < 0)
cout << " There is no customer with that number " << endl;
}while (num > (index-1));
cout << "\n\nCustomer Name: " << array[num].CustomerName << endl;
cout << "Customer Address " << array[num].CustomerAddress <<endl;
cout << "City: " << array[num].City << endl;
cout << "State: " << array[num].State << endl;
cout << "ZIPCode: " << array[num].ZIPCode << endl;
cout << "Telephone: " << array[num].Telephone <<endl;
cout << "Account Balance: " << array[num].AccountBalance << endl;
cout << "Date of last payment: " << array[num].DateOfLastPayment << endl;
cout << "You have requested information for customer number " << num << endl << endl;
cout << "What value do you want to change? (press 4 to change 'Date of last payment') \n";
cin.ignore();
getline(cin,valueChange);
valueChangeFunc(valueChange, array, num);
cout << "\nHere is the new value you entered for " << valueChange << endl;
cout << "\n\nCustomer Name: " << array[num].CustomerName << endl;
cout << "Customer Address " << array[num].CustomerAddress <<endl;
cout << "City: " << array[num].City << endl;
cout << "State: " << array[num].State << endl;
cout << "ZIPCode: " << array[num].ZIPCode << endl;
cout << "Telephone: " << array[num].Telephone <<endl;
cout << "Account Balance: " << array[num].AccountBalance << endl;
cout << "Date of last payment: " << array[num].DateOfLastPayment << endl << endl;
}
else if(choice == 3 && index != 0)
{
int num2;
do
{
cout << "Enter the Customer Number to display information regarding that customer" << endl;
cin >> num2;
if (num2 > (index-1) || num2 < 0)
cout << "That Customer does not exist " <<endl;
}
while(num2 > (index-1));
cout << "\n\nCustomerName: " << array[num2].CustomerName << endl;
cout << "CustomerAddress " << array[num2].CustomerAddress <<endl;
cout << "City: " << array[num2].City << endl;
cout << "State: " << array[num2].State << endl;
cout << "Zip Code: " << array[num2].ZIPCode << endl;
cout << "Telephone: " << array[num2].Telephone <<endl;
cout << "AccountBalance: " << array[num2].AccountBalance << endl;
cout << "DateOfLastPayment: " << array[num2].DateOfLastPayment << endl;
cout << "You have entered information for customer number " << index << endl << endl;
}
else
flag = true;
}while (flag == false);
return 0;
}
void valueChangeFunc(string valueChange2, Account array[], int num)
{
if (valueChange2 == "Customer Name" || valueChange2 == "Customer name" || valueChange2 == "customer Name" || valueChange2 == "customer name")
{
cout << "\nEnter new value for Customer Name: " <<endl;
getline(cin, array[num].CustomerName);
}
if (valueChange2 == "Customer Address" || valueChange2 == "Customer address" || valueChange2 == "customer Address" || valueChange2 == "customer address")
{
cout << "\nEnter new value for Customer Address: " <<endl;
getline(cin, array[num].CustomerAddress);
}
else if(valueChange2 == "city" || valueChange2 == "City")
{
cout << "\nEnter new value for City: " << endl;
getline(cin, array[num].City);
}
else if(valueChange2 == "state" || valueChange2 == "State")
{
cout << "Enter a value for State: " << endl;
getline(cin,array[num].State);
}
else if(valueChange2 == "Zip Code" || valueChange2 == "zip Code" || valueChange2 == "Zip code" || valueChange2 == "zip code")
{
cout << "\nEnter a value for Zip Code: " << endl;
cin >> array[num].ZIPCode;
}
else if(valueChange2 == "telephone" || valueChange2 == "Telephone")
{
cout << "\nEnter a value for Telephone: " << endl;
getline(cin, array[num].Telephone);
}
else if(valueChange2 == "Account Balance" || valueChange2 == "Account balance" || valueChange2 == "account Balance" || valueChange2 == "account balance")
{
cout << "\nEnter a value for account balance: " << endl;
cin >> array[num].AccountBalance;
}
else if(valueChange2 == "4")
{
cout << "\nEnter the value for Date of last payment: " << endl;
getline(cin, array[num].DateOfLastPayment);
}
else
cout << "Not entered correctly. Please enter a valid entry to edit " << endl;
}
Again everything worked until I started to use a loop to check for digit value of ZipCode. The loop only goes infinite when I enter a letter. It works for a negative number and positive number.
Short answer can be found in cplusplus.com (read the third paragraph)
Long answer:
This error isn't about ZipCode only, you can generate the same error when you input a letter instead of a number at the very first cin call.
cin is not type-safe so using a wrong type as an input results in an undefined behaviour (like the infinite loop you were experiencing) and that is why cin is a bit prone to errors. Also, cin gets the input value, however it doesn't remove newline, reading to a bit dirtier input from what you'd get with other methods.
Speaking of other methods, as explained in the link, try to use getline() whenever it is possible. You can use that function to get what cin buffer contains as a string and do additional type-check if necessary. Bug-free programs are more important than shorter programs.
As a personal note: try to use while loops instead of do..while ones when you can. That is not a general rule of programming but it looks more readable and is easier to understand in general (IMHO).
Also, try to use functions to split your program into parts. For example, the cout blocks where you are just printing the information stored to the screen can be turned into a function. You're using the same cout structure (it would shorten your code by 4 * 9 lines).
Finally, if you're using an integer as a key value to separate algorithms, try to use switch...case instead of if-else blocks. (Again, just my opinion).
I am working on an assignment. The problem I am having is every time I try to run my program to see what it displays, nothing shows up on the command prompt. However, if I press any key and then enter, the program starts looping uncontrollably. The program doesn't even display the initial cout message, just a blinking "_". Thanks
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void PizzaMenu();
void SizePrices();
int main()
{
double personal = 10.00;
double medium = 14.50;
double large = 19.00;
double xlarge = 23.50;
double FlavorChoice=0;
int SizeChoice;
int PizzaCountP=(cin >> PizzaCountP, PizzaCountP);
int PizzaCountM = (cin >> PizzaCountM, PizzaCountM);
int PizzaCountL = (cin >> PizzaCountL, PizzaCountL);
int PizzaCountXL = (cin >> PizzaCountXL, PizzaCountXL);
double orderTotal = (personal * PizzaCountP) + (medium * PizzaCountM) + (large * PizzaCountL) + (xlarge * PizzaCountXL);
cout << "Welcome to Joes pizza place!" << endl;
do{
PizzaMenu();
cout << "\nPlease chose a pizza from the menu(1-6): ";
cin >> FlavorChoice;
SizePrices();
cin >> SizeChoice;
if (SizeChoice > 0 && SizeChoice < 5)
{
switch (SizeChoice)
{
case 1:
cout << "How many personal pizzas? "; cin >> PizzaCountP;
break;
case 2:
cout << "How many medium pizzas?"; cin >> PizzaCountM;
break;
case 3:
cout << "How many large pizzas?"; cin >> PizzaCountL;
break;
case 4: cout << "How many extra large pizzas?"; cin >> PizzaCountXL;
break;
default: cout << "please enter a choice (1-4)"; cin >> SizeChoice;
break;
}
}
if (PizzaCountP > 0 || PizzaCountM > 0 || PizzaCountXL > 0 || PizzaCountL > 0)
{
printf("Your total is: %a", orderTotal);
}
} while (FlavorChoice != 6);
cout << "Thank you for visiting Joes place pizza! "<<endl;
}
void PizzaMenu()
{
cout << "\nSpecialty Pizza Menu" << endl;
cout << "\n1)Pizza 1" << endl << "\n2)Pizza 2" << endl << "\n3)Pizza 3" <<endl << "\n4)Pizza 4" << endl << "\n5)Pizza 5" << endl << "\n6)Pizza 6" << endl;
}
void SizePrices()
{
cout << "1) 10'' Personal" << "\t" << "- $10.00" << endl;
cout << "2) 14'' Medium" << "\t" << "- $14.50" << endl;
cout << "3) 16'' Large" << "\t" << "- $19.00" << endl;
cout << "4) 18'' Extra Large" << "\t" << "- $23.50" << endl;
cout << "Your choice (1-4)? ";
}
There were a few logical errors in the program. Right now, it should work fine...
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void PizzaMenu()
{
cout << "Specialty Pizza Menu:" << endl;
cout << "1) Pizza 1" << endl << "2) Pizza 2" << endl << "3) Pizza 3" << endl << "4) Pizza 4" << endl << "5) Pizza 5" << endl << "6) Exit" << endl;
}
void SizePrices()
{
cout << "Size Prices:" << endl;
cout << "1) 10'' Personal" << "\t" << "- $10.00" << endl;
cout << "2) 14'' Medium" << "\t" << "- $14.50" << endl;
cout << "3) 16'' Large" << "\t" << "- $19.00" << endl;
cout << "4) 18'' Extra Large" << "\t" << "- $23.50" << endl;
cout << "Your choice (1-4)? ";
}
int main()
{
double personal = 10.00;
double medium = 14.50;
double large = 19.00;
double xlarge = 23.50;
int FlavorChoice = 0;
int SizeChoice = 0;
int PizzaCountP = 0;
int PizzaCountM = 0;
int PizzaCountL = 0;
int PizzaCountXL = 0;
double orderTotal = 0.0;
cout << "Welcome to Joes pizza place!" << endl;
cout << "Please choose from the main menu(1-6): " << endl;
PizzaMenu();
cin >> FlavorChoice;
while(FlavorChoice != 6) {
SizePrices();
cin >> SizeChoice;
if (SizeChoice > 0 && SizeChoice < 5)
{
switch (SizeChoice)
{
case 1:
cout << "How many personal pizzas? ";
cin >> PizzaCountP;
orderTotal += personal * PizzaCountP;
break;
case 2:
cout << "How many medium pizzas?";
cin >> PizzaCountM;
orderTotal += medium * PizzaCountM;
break;
case 3:
cout << "How many large pizzas?";
cin >> PizzaCountL;
orderTotal += large * PizzaCountL;
break;
case 4: cout << "How many extra large pizzas?";
cin >> PizzaCountXL;
orderTotal += xlarge * PizzaCountXL;
break;
default: cout << "please enter a choice (1-4)";
cin >> SizeChoice;
break;
}
}
// orderTotal = (personal * PizzaCountP) + (medium * PizzaCountM) + (large * PizzaCountL) + (xlarge * PizzaCountXL);
if (PizzaCountP > 0 || PizzaCountM > 0 || PizzaCountXL > 0 || PizzaCountL > 0)
{
// printf("Your total is: %a", orderTotal);
cout << "Your total is: $" << orderTotal << endl;
}
cout << "Please choose from the main menu(1-6): " << endl;
PizzaMenu();
cin >> FlavorChoice;
}
cout << "Thank you for visiting Joes place pizza! " << endl;
// system("pause");
return 0;
}