Program builds but displays nothing - c++

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

Related

How to store multiple string inputs and displaying it after

I'm just starting in studying C++, and I am doing a simple challenge which is GWA Calculator, but I am having a problem finding out how to store the multiple strings input (which is the Subjects/Course) and displaying it after together with the Units and Grades. I am really sorry, but I tried finding out how and I couldn't find an answer. Hope you can help me out.
#include <stdlib.h>
using namespace std;
void calculateGWA();
int main()
{
system("cls");
int input;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
cout << "\t\t| GWA Calculator |" << endl;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
cout << "\t\t| MENU:\t\t\t\t\t\t\t " << "|" << endl;
cout << "\t\t| 1. Calculate GWA (General Weighted Average)\t\t " << "|" << endl;
cout << "\t\t| 2. Calculate CGWA (Cummulative Weighted Average) " << "|" << endl;
cout << "\t\t| 4. Exit Application\t\t\t\t\t " << "|" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
sub:
cout << "\t\tEnter your choice: ";
cin >> input;
switch(input)
{
case 1:
calculateGWA();
break;
case 2:
//calculateCGPA();
break;
case 3:
main();
break;
case 4:
exit(EXIT_SUCCESS);
break;
default:
cout << "You have entered wrong input.Try again!\n" << endl;
goto sub;
break;
}
}
void calculateGWA()
{
int q;
system("cls");
cout << "-------------- GWA Calculator -----------------"<<endl;
cout << " How many course(s)?: ";
cin >> q;
char c_name[50];
float unit [q];
float grade [q];
cout << endl;
for(int i = 0; i < q; i++)
{
cout << "Enter the Course Name " << i+1 << ": ";
cin >> c_name;
cout << "Enter the Unit " << c_name << ": ";
cin >> unit[i];
cout << "Enter the Grade " << c_name << ": ";
cin >> grade[i];
cout << "-----------------------------------\n\n" << endl;
}
float sum = 0;
float tot;
for(int j = 0; j < q; j++)
{
tot = unit[j] * grade[j];
sum = sum + tot;
}
float totCr = 0;
for(int k = 0; k < q; k++)
{
totCr = totCr + unit[k];
}
system("cls");
// PRINTS OUT THE COURSES - UNITS - GRADES AND GWA //
cout << "\t\t =============================================================== " << endl;
cout << "\t\t| COURSE | UNIT | GRADE |" << endl;
cout << "\t\t =============================================================== " << endl;
cout << "Total Points: " << sum << " \n Total Credits: " << totCr << " \nTotal GPA: " << sum/totCr << " ." << endl;
cout << c_name << "\n" << endl;
cout << "===================================" << endl;
sub:
int inmenu;
cout << "\n\n\n1. Calculate Again" << endl;
cout << "2. Go Back to Main Menu" << endl;
cout << "3. Exit This App \n\n" << endl;
cout << "Your Input: " << endl;
cin >> inmenu;
switch(inmenu)
{
case 1:
calculateGPA();
break;
case 2:
main();
break;
case 3:
exit(EXIT_SUCCESS);
default:
cout << "\n\nYou have Entered Wrong Input!Please Choose Again!" << endl;
goto sub;
}
}

different option throw an unexpected error

when I'm executing this program, it works fine just for one option, but if I try to choose an other option when executing, it will throw an error in this line:
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
here's my program:
void avail_art(void)
{
char c,con;
int quantity,disp=MAX;
bool b = true;
map<string, unordered_set<int>>m{
{"Mouse",{120,disp}},
{"Keyboard",{75,disp}},
{"Monitor",{750,disp}},
{"GPU",{1200,disp}},
{"CPU",{1000,disp}}
};
auto it = m.find("CPU");
cout << "M - for mouse." << endl;
cout << "K - for keyboard." << endl;
cout << "R - for monitor." << endl;
cout << "G - for GPU." << endl;
cout << "C - for CPU." << endl;
cout << "Q - for quit." << endl;
cout << "======================" << endl;
while (b)
{
cout << "Enter your choice: ";
cin >> c;
switch (c)
{
case 'M':
it = m.find("Mouse");
cout << "You've choosen the Mouse!" << endl;
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
l:
cout << "Enter the quantity: ";
cin >> quantity;
if (quantity > * (it->second.find(disp)))
{
cout << "Out of Stock! the Stock has only :" << *(it->second.find(disp)) <<" Piece."<< endl;
goto l;
}
cout << "Confirm? y/n: ";
cin >> con;
if (con == 'y')
{
auto its=it->second.find(disp);
it->second.insert(disp=(*its - quantity));
it->second.erase(its);
}
break;
case 'K':
it = m.find("Keyboard");
cout << "You've choosen the Keyboard!" << endl;
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
cout << "Enter the quantity: ";
cin >> quantity;
cout << "Confirm? y/n :";
cin >> con;
if (con == 'y')
{
auto its = it->second.find(disp);
it->second.insert(disp = (*its - quantity));
it->second.erase(its);
}
break;
}
}
}
can someone help me please

Looping for calculators

The program will take in floating-point numbers and operators from the user and perform the required calculations, printing out the results. The result of each calculation will serve as an operand for the next calculation.
So, I am not sure if I am doing this correctly by using switch statements ? Is there a better way of doing it ? Maybe, by using a do - while loop ? I am really confused.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float firstOperand;
cout << "Enter a number: ";
cin >> firstOperand;
cout << endl;
cout << "Choose an instruction code: " << endl << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;
int choice;
cin >> choice;
cout << endl;
float secondOperand;
cout << "Enter the second number: ";
cin >> secondOperand;
cout << endl;
switch (choice)
{
case 1:
{
float resultOne = firstOperand + secondOperand;
cout << "The result of the calculation is " << resultOne << endl;
}
case 2:
{
float thirdOperand;
cout << "Enter another number ";
cin >> thirdOperand;
cout << endl;
cout << "Choose an instruction code: " << endl << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;
float resultTwo = resultOne + thirdOperand;
cout << "The result of the calculation is " << resultTwo << endl;
}
break;
}
system("pause");
return 0;
}
Try this code:
#include <iostream>
#include <math.h>
using namespace std;
int main(){
float firstOperand, secondOperand, result;
int choice, quit = 0;
cout << "Enter a number: ";
cin >> firstOperand;
cout << endl;
while (1){
cout << "The first operand is " << firstOperand << endl;
cout << "\nChoose an instruction code: " << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;
cin >> choice;
cout << endl;
if (choice == 8){
cout << "Quitting the program..." << endl;
break;
}
cout << "Enter the second number: ";
cin >> secondOperand;
cout << endl;
switch(choice){
case 1: result = firstOperand + secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 2: result = firstOperand * secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 3: result = pow(firstOperand, secondOperand);
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 4: result = 0;
cout << "The result has been cleared to " << result << endl;
cout << "Enter the first operand: ";
cin >> firstOperand;
cout << endl;
break;
case 5: result = firstOperand - secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 6: if(secondOperand){
result = firstOperand / secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
}
else{
cout << "Second operand is " << secondOperand << "Choose again!" << endl;
}
case 7: result = sqrt(secondOperand);
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
default:cout << "Invalid input. Enter again!" << endl;
break;
}
}
}
The main code is wrapped inside an infinite while loop with a quit condition. The break statement is used to exit the while loop when the user wishes to.
Note: The input for the operator is the number and not the symbol. You will have to change choice variable to char in case you want to use the symbol as the input.
Update: The following code is for character inputs.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
float firstOperand, secondOperand, result;
int quit = 0;
char choice;
cout << "Enter a number: ";
cin >> firstOperand;
cout << endl;
while (1){
cout << "The first operand is " << firstOperand << endl;
cout << "\nChoose an instruction code: " << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;
cin >> choice;
cout << endl;
if (choice == 'q'){
cout << "Quitting the program..." << endl;
break;
}
cout << "Enter the second number: ";
cin >> secondOperand;
cout << endl;
switch(choice){
case '+': result = firstOperand + secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case '*': result = firstOperand * secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 'p': result = pow(firstOperand, secondOperand);
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case 'c': result = 0;
cout << "The result has been cleared to " << result << endl;
cout << "Enter the first operand: ";
cin >> firstOperand;
cout << endl;
break;
case '-': result = firstOperand - secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
case '/': if(secondOperand){
result = firstOperand / secondOperand;
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
}
else{
cout << "Second operand is " << secondOperand << "Choose again!" << endl;
}
case 's': result = sqrt(secondOperand);
cout << "The result of the calculation is " << result << endl;
firstOperand = result;
break;
default: cout << "Invalid input. Enter again!" << endl;
break;
}
}
}
Here is one possible version on the code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int select_operation()
{
int choice;
cout << "Choose an instruction code: " << endl << endl;
cout << "1) (+) for addition. " << endl;
cout << "2) (*) for multiplication. " << endl;
cout << "3) (p) for power. " << endl;
cout << "4) (c) to clear the current result. " << endl;
cout << "5) (-) for subtraction. " << endl;
cout << "6) (/) for divison. " << endl;
cout << "7) (s) for square root. " << endl;
cout << "8) (q) to quit the program. " << endl << endl;
cin >> choice;
cout << endl;
return choice;
}
int main()
{
float secondOperand;
float firstOperand;
float thirdOperand;
float resultOne;
int choice;
cout << "Enter a number: ";
cin >> firstOperand;
cout << endl;
choice = select_operation();
if (choice == 8)
return 0;
else if(choice == 7)
secondOperand = .5;
else{
cout << "Enter the second number: ";
cin >> secondOperand;
}
cout << endl;
while(1)
{
switch (choice) {
case 1:
resultOne = firstOperand + secondOperand;
cout << "The result of the calculation is " << resultOne << endl;
break;
case 2:
resultOne = firstOperand * secondOperand;
cout << "The result of the calculation is " << resultOne << endl;
break;
case 3:
resultOne = pow(firstOperand,secondOperand);
cout << "The result of the calculation is " << resultOne << endl;
break;
case 4:
resultOne = 0;
cout << "The result of the calculation is " << resultOne << endl;
break;
case 5:
resultOne = firstOperand - secondOperand;
cout << "The result of the calculation is " << resultOne << endl;
break;
case 6:
if(secondOperand){
resultOne = firstOperand / secondOperand;
cout << "The result of the calculation is " << resultOne << endl;
}
break;
case 7:
resultOne = pow(firstOperand,secondOperand);
cout << "The result of the calculation is " << resultOne << endl;
break;
}
choice = select_operation();
if (choice == 8)
return 0;
else if(choice == 7)
secondOperand = .5;
else{
cout << "Enter another number ";
cin >> thirdOperand;
secondOperand = thirdOperand;
}
}
cout << endl;
firstOperand = resultOne;
return 0;
}
The above code accepts two inputs at first immediately returns if it is option 8(quit the program). if the operation is square root as it is a unary operation not taking in second operand.
Reading the 3rd operand and operator is continued until it is requested to stop.

C++ functions and loops

I have to write a program that simulates an ice cream cone vendor. The user inputs the number of cones, and for each cone, the user inputs the number of scoops, then the flavor(a single character) for each scoop. At the end, the total price is listed. For the pricing, 1 scoop costs 2.00, 2 scoops costs 3.00 and each scoop after 2 costs .75.
I'm having trouble with the pricing. The correct price is displayed if the user only wants one cone.
/*
* icecream.cpp
*
* Created on: Sep 14, 2014
* Author:
*/
#include <iostream>
#include <string>
using namespace std;
void welcome() {
cout << "Bob and Jackie's Ice Cream\n";
cout << "1 scoop - $1.50\n";
cout << "2 scoops - $2.50;\n";
cout << "Each scoop after 2 - $.50\n";
cout << "Ice Cream Flavors: Only one input character for each flavor.\n";
}
bool checkscoops(int scoops) {
int maxscoops = 5;
if ((scoops > maxscoops) || (scoops < 1))
return false;
else
return true;
}
bool checkcones(int cones) {
int maxcones = 10;
if ((cones > maxcones) || cones < 1)
return false;
else
return true;
}
int price(int cones, int numberofscoops) {
float cost = 0.00;
{
if (numberofscoops == 5) {
cost = cost + 5 + (.75 * 3);
}
if (numberofscoops == 4) {
cost = cost + 5 + (.75 * 2);
}
if (numberofscoops == 3) {
cost = cost + 5.75;
}
if (numberofscoops == 2) {
cost = cost + 5.00;
}
if (numberofscoops == 1) {
cost = cost + 2.00;
}
}
cout << "Total price is: " << cost << endl;
}
int buildcone(int numcones) {
char flav1, flav2, flav3, flav4, flav5;
int numberofscoops;
for (int i = 1; i <= numcones; i++) {
cout << "Enter the amount of scoops you wish to purchase. (5 max): ";
cin >> numberofscoops;
checkscoops(numberofscoops);
while (checkscoops(numberofscoops) == false) {
cout << "You are not allowed to buy more than 5 scoops and you "
"cannot buy less than one scoop. Please try again.\n";
cout << "How many scoops would you like?(5 max): ";
cin >> numberofscoops;
checkcones(numberofscoops);
}
cout << "You are buying " << numberofscoops
<< " scoops of ice cream.\n";
if (numberofscoops == 5) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << "Enter flavor 5: ";
cin >> flav5;
cout << " ( " << flav1 << " )/" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " ( " << flav5 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 4) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 3) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 2) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 1) {
cout << "Enter a flavor: ";
cin >> flav1;
cout << " ( " << flav1 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
}
price(numcones, numberofscoops);
}
int main() {
int numberofcones;
int numberofscoops;
welcome();
cout << "How many cones would you like?(10 max) ";
cin >> numberofcones;
checkcones(numberofcones);
while (checkcones(numberofcones) == false) {
cout << "You are not allowed to buy more than 10 cones and you cannot "
"buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
checkcones(numberofcones);
}
cout << "You are buying " << numberofcones << " ice cream cones.\n";
buildcone(numberofcones);
}
Start by changing the return value of price() to float, or the function won't be able to return the proper cost. Also, since cones is not used to compute the cost of the purchase, we don't it as a parameter:
float price(int numberofscoops)
{
float total_cost = 0.0f;
if (numberofscoops == 1) {
total_cost = 2.0f;
}
else if (numberofscoops == 2) {
total_cost = 3.0f;
}
else if (numberofscoops > 2) {
total_cost = 5.0f + ((numberofscoops-2) * 0.75f);
}
return total_cost;
}
You code could have other problems, but I think these changes will let you continue to debug and fix the code on your own.
Your while() loop is flawed. Comment your call to checkcones() as shown below. You're already calling checkcones() as the conditional in your while(), no need to evaluate again as this will sent you into a perma-loop. You've got two of these while() statements that I could see, you'll want to comment out both.
while ( checkcones( numberofcones ) == false )
{
cout << "You are not allowed to buy more than 10 cones and you cannot buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
// THIS LINE IS THE PROBLEM :)
// checkcones(numberofcones);
}
After this fix, your program begins to work but the pricing fails. You should be able to figure that out with the answer given above.
I would also see if you can figure out how to implement a c++ class with members and methods as your current approach is very "c" like. Happy coding! :)

Serendipity booksellers software program C++

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;