Why doesn't my program touch the functions I made? - c++

I am having issues with running the following code, it runs but doesn't display anything
#include <iostream>
using namespace std;
class Calorie {
public:
string name;
double height;
double weight;
char gender;
string fname;
string fgroup;
int cal;
double recommendcal;
double temp;
double RecCal();
Calorie(string foodname, string foodgroup, int calories);
Calorie(string n, double h, double w, char g);
};
class Foodgroup : public Calorie {
int rec;
void Grains();
void Proteins();
void Vege();
void fruit();
void Dairy();
};
int main() {
string foodname;
string foodgroup;
int calories;
string name;
double height;
double weight;
char gender;
cout << "Please enter your name:";
cin >> name;
cout << "Please enter your height in inches:";
cin >> height;
cout << "Please enter your weight in pounds:";
cin >> weight;
cout << "Please enter your gender F/M:";
cin >> gender;
Calorie human(name, height, weight, gender);
Calorie RecCal();
cout << "Enter Food name:";
cin >> foodname;
cout << "Enter Food type:";
cin >> foodgroup;
cout << "Enter number of calories:";
cin >> calories;
Calorie food(foodname, foodgroup, calories);
if (foodgroup == "grains") {
Foodgroup Grains();
}
else if (foodgroup == "proteins" || foodgroup == "Proteins") {
Foodgroup Proteins();
}
else if (foodgroup == "fruits" || foodgroup == "Fruits") {
Foodgroup fruits();
}
else if (foodgroup == "vegetables" || foodgroup == "Vegetables") {
Foodgroup Vege();
}
else if (foodgroup == "dairy" || foodgroup == "Dairy") {
Foodgroup Dairy();
}
return 0;
}
Calorie::Calorie(string n, double h, double w, char g) {
name = n;
height = h;
weight = w;
gender = g;
}
Calorie::Calorie(string foodname, string foodgroup, int calories) {
fname = foodname;
fgroup = foodgroup;
cal = calories;
}
double Calorie::RecCal() {
if (gender =='M' || gender == 'm') {
recommendcal = 2500;
if (height <= 70) {
temp = 70 - height;
temp = 25 * temp;
recommendcal = recommendcal - temp;
}else if (height >= 70) {
temp = height - 70;
temp = 25 * temp;
recommendcal = recommendcal + temp;
}
if (weight <= 165) {
temp = 165 - weight;
temp = 10 * temp;
recommendcal = recommendcal - temp;
}
else if (weight >= 165) {
temp = weight - 165;
temp = 10 * temp;
recommendcal = recommendcal + temp;
}
}
else if (gender == 'F' || gender == 'f') {
recommendcal = 2300;
if (height <= 70) {
temp = 70 - height;
temp = 25 * temp;
recommendcal = recommendcal - temp;
}
else if (height >= 70) {
temp = height - 70;
temp = 25 * temp;
recommendcal = recommendcal + temp;
}
if (weight <= 165) {
temp = 165 - weight;
temp = 5 * temp;
recommendcal = recommendcal - temp;
}
else if (weight >= 165) {
temp = weight - 165;
temp = 5 * temp;
recommendcal = recommendcal + temp;
}
}
cout << "You're recommended calorie intake per day is: " << recommendcal << " calories." << endl;
return recommendcal;
}
void Foodgroup::Grains() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}
void Foodgroup::Proteins() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}
void Foodgroup::Vege() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}void Foodgroup::fruit() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}void Foodgroup::Dairy() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}
I am unsure why the this occur, if you could help me resolve the issue it would be greatly appreciated. I have no idea on how to resolve this issue, it all looks like it should work but something isn't clicking. The codes purpose is to calculate the amount of calories on takes and see if they have met their recommended calorie intake. It is also supposed to ask three times, three different foods/drinks since we don't just eat/drink one thing a day.

Replace:
void Human(Calorie name, Calorie height, Calorie weight, Calorie gender)
with:
void Human(std::string name, double height, double weight, char gender)
You have the wrong types in a function signature.

Ron, that's right, it solves this problem but there is another problem :
void foodgroup::Grains() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}
[Error] 'recommendcal' was not declared in this scope
There is no declaration of 'recommendcal'

Related

FOR LOOP fails to iterate through the object vector where purchase transactions are stored

I'm unsure why when more than one transaction has been made the FOR LOOP won't iterate through the vector of "buyers". Am I missing something simple? I've spent the last few hours trying to fix this but to no avail. If any one could possibly help with my problem I'd be eternally grateful.
#include <iostream>
#include <iomanip>
#include <vector>
#include <cctype>
using namespace std;
//This class is for the car details that the user can purchase.
class cCar {
private:
string _sName;
double _dPrice;
public:
cCar(string s, double d)
{
_sName = s;
_dPrice = d;
}
string getName() { return _sName; }
double getPrice() { return _dPrice; }
};
//This is where all the car detail purchases are created and stored in the object 'carlist'
vector<cCar> CarDatabase(vector<cCar>& car_list)
{
car_list.push_back(cCar("Blue Nissan Skyline", 1000));
car_list.push_back(cCar("Red Mini", 3000));
car_list.push_back(cCar("Black Land Rover", 4000));
car_list.push_back(cCar("Beatle", 9000));
car_list.push_back(cCar("Ferrari", 300000));
return car_list;
}
//This class stores the user's transactions
class Finance {
private:
string _sUserName;
double _dCostOfCar;
string _sChosenCar;
int _iFinancePlan;
double _dDepositedAmount;
double _dMonthlyPayments;
double _dTotalLeftToPay;
public:
Finance(string sName, double dCostOfCar, string sChosenCar, int iFinancePlan, double dDepositedAmount, double dDMonthlyPayments, double dTotalLeftToPay)
{
_sUserName = sName;
_dCostOfCar = dCostOfCar;
_sChosenCar = sChosenCar;
_iFinancePlan = iFinancePlan;
_dDepositedAmount = dDepositedAmount;
_dMonthlyPayments = dDMonthlyPayments;
_dTotalLeftToPay = dTotalLeftToPay;
}
string getUserName() { return _sUserName; }
double getdCostOfCar() { return _dCostOfCar; }
string getChosenCar() { return _sChosenCar; }
int getFinancePlan() { return _iFinancePlan; }
double getDepositAmount() { return _dDepositedAmount; }
double getMonthlyAmount() { return _dMonthlyPayments; }
double getTotalLeftToPay() { return _dTotalLeftToPay; }
};
//1. This displays the car menu items.
void display_menu(vector<cCar>& car_list)
{
cout << "\nMENU";
for (int iCount = 0; iCount != car_list.size(); iCount++) {
cout << "\n" << iCount + 1 << ". " << car_list[iCount].getName();
cout << "\n\tPrice: £" << car_list[iCount].getPrice();
cout << "\n";
}
}
//This procedure proccesses the user's selection and all information regarding price and name of car are then transferred to transaction variables.
void selectedCar(vector<cCar>& car_list, string& sNameOfChosenCar, double& dCostOfChosenCar)
{
int iSelectionFromMenu = -1;
do {
cout << "\nChoose a car that you'd wish to buy from the menu (1 - " << car_list.size() << "): ";
cin >> iSelectionFromMenu;
if (iSelectionFromMenu > 0 && iSelectionFromMenu <= car_list.size()) {
sNameOfChosenCar = car_list[iSelectionFromMenu - 1].getName();
dCostOfChosenCar = car_list[iSelectionFromMenu - 1].getPrice();
}
else {
cout << "\nPlease enter valid number!";
iSelectionFromMenu = -1;
}
} while (iSelectionFromMenu == -1);
}
//This procedure gets from the user their preferred finance plan through their input.
void FinanceLength(int& iFinanceLength)
{
do {
cout << "\nHow long do you wish for your finance plan to last? (1 - 4 years): ";
cin >> iFinanceLength;
if (iFinanceLength < 0 || iFinanceLength > 4) {
cout << "\nOops, try again! Please enter between 1 - 4!";
}
} while (iFinanceLength < 0 || iFinanceLength > 4);
}
//This procedure gets the user's deposit.
void DepositMoney(double& dDepositAmount)
{
do {
cout << "\nEnter deposit amount (minimum £500 accepted): £";
cin >> dDepositAmount;
if (dDepositAmount < 500) {
cout << "\nTry again! Deposit an amount greater than or equal to £500.";
}
} while (dDepositAmount < 500);
}
//This function calculates the amount of money the user has to pay after deposit, added tax and charge percentage of 10%
double TotalLeftToPay(double iFinanceLength, double dDepositAmount, double dCostOfChosenCar)
{
double dChargePercentage = 0.10;
double dTotalLeftToPay = dCostOfChosenCar + (dCostOfChosenCar * dChargePercentage) - dDepositAmount + 135;
return dTotalLeftToPay;
}
//This calculates monthly payments.
double MonthlyPayments(double dTotalLeftToPay, int iFinanceLength)
{
double dMonthlyPayments = (dTotalLeftToPay / iFinanceLength) / 12;
return dMonthlyPayments;
}
//This asks the user whether they'd like to restart the application.
void RestartOptions(char& cOption, bool& bExit)
{
do {
cout << "\nDo you wish to make another purchase? (y/n): ";
cin >> cOption;
cOption = toupper(cOption);
switch (cOption) {
case 'Y':
bExit = false;
break;
case 'N':
bExit = true;
break;
default:
cout << "Sorry, that's an invalid input, please try again!";
continue;
}
} while (cOption != 'y' && cOption != 'Y' && cOption != 'n' && cOption != 'N');
}
//This string function returns either year or years (plural)
string YearOrYears(int iFinanceLength)
{
return (iFinanceLength > 1) ? "years" : "year";
}
//This displays receipt of the user's transaction.
//HERE IS WHRERE I "M STRUGGLING TO ITERATE
void Receipt(const string& sUserName, const int& iFinanceLength, const double& dDepositAmount, char cOption, bool& bExit, const string& sNameOfChosenCar, const double& dCostOfChosenCar, vector<Finance>& buyers)
{
double dTotalLeftToPay = TotalLeftToPay(iFinanceLength, dDepositAmount, dCostOfChosenCar);
double dMonthlyPayments = MonthlyPayments(dTotalLeftToPay, iFinanceLength);
buyers.push_back(Finance(sUserName, dCostOfChosenCar, sNameOfChosenCar, iFinanceLength, dDepositAmount, dMonthlyPayments, dTotalLeftToPay));
for (int iCount = 0; iCount != buyers.size(); iCount++) {
cout << "\nReceipt for: " << buyers[iCount].getUserName() << ". ";
cout << "\nYou have chosen " << buyers[iCount].getChosenCar() << ".";
cout << "\nYour finance plan timescale is " << buyers[iCount].getFinancePlan() << " " << YearOrYears(iFinanceLength) << ".";
cout << "\nYou've deposited £" << buyers[iCount].getDepositAmount() << ".";
cout << "\nTotal left to pay: £" << buyers[iCount].getTotalLeftToPay();
cout << "\nMonthly Payments: £" << buyers[iCount].getMonthlyAmount();
cout << "\n";
}
RestartOptions(cOption, bExit);
}
//This asks the user whether they're happy with the options of they've chosen.
void AcceptDeclineOptions(string& sUserName, int& iFinanceLength, double& dDepositAmount, bool& bExit, string& sNameOfChosenCar, double& dCostOfChosenCar, vector<Finance> buyers)
{
char cOption = 0;
do {
cout << "\nConfirm finance plan (y/n): ";
cin >> cOption;
cOption = toupper(cOption);
if (cOption == 'Y' || cOption == 'N') {
if (cOption == 'Y') {
Receipt(sUserName, iFinanceLength, dDepositAmount, cOption, bExit, sNameOfChosenCar, dCostOfChosenCar, buyers);
}
else {
RestartOptions(cOption, bExit);
}
}
else {
cout << "\nSorry, that's not a valid command.";
}
} while (cOption != 'Y' && cOption != 'N');
}
int main()
{
bool bExit = false;
int iFinanceLength = 0;
double dDepositAmount = 0;
string sNameOfChosenCar = "";
double dCostOfChosenCar = 0;
vector<cCar> car_list;
CarDatabase(car_list);
vector<cCar> car_purchases;
vector<Finance> buyers;
do {
cout << "Welcome!";
string sUserName = "";
cout << "\nEnter your name: ";
cin >> sUserName;
display_menu(car_list);
selectedCar(car_list, sNameOfChosenCar, dCostOfChosenCar);
FinanceLength(iFinanceLength);
DepositMoney(dDepositAmount);
AcceptDeclineOptions(sUserName, iFinanceLength, dDepositAmount, bExit, sNameOfChosenCar, dCostOfChosenCar, buyers);
} while (bExit == false);
}

Function/Syntax Errors

The goal of these functions in the end is to show someone's total fee's for being consulted, which is based off whether or not they have a low income. The functions are supposed flow downwards to get each needed value in order to calculate the total. I do not understand how to fix the various errors in my code, such as some variables not being declared in a specific scope, even though I thought i declared them correctly.
#include <iostream>
using namespace std;
const double HighIncomeRate = .7;
const double LowIncomeRate = .4;
bool isLowIncome();
int main() {
double Fee;
double ConsultTime;
cout << "Your fee is: " << getcalcFee(consultTime, Income) << endl;
return 0;
}
bool isLowIncome() {
double Income;
cout << "What is your income: " << endl;
cin >> Income;
cout << "How long have you been consulted" << endl;
cin >> ConsultTime;
if (Income <= 25000) {
return true;
} else {
return false;
}
}
double calcFee(int &ConsultTime, const double HighIncomeRate,
const double LowIncomeRate) {
if (isLowIncome == true) {
if (ConsultTime <= 30) {
calcFee = 0
} else {
calcFee = LowIncomeRate * 50((ConsultTime - 30) / 60)
}
}
if {
if (ConsultTime <= 20) {
calcFee = 0
} else {
calcFee = HighLowRate * 50((ConsultTime - 20) / 60)
}
}
return 0;
}

My Very Basic C++ Program Broke and I Have No Idea Why

This is a program I am doing for programming class that that is supposed calculate people's pay depending on overtime and time and a half, but it broke and I have no idea why.
The thing will output "" "" has job 6942646 and worked 6923592 hours" or something like that.
#include <iostream>
#include <iomanip>
using namespace std;
string getName(string, string);
int getJob(int&);
int hoursWorked(int);
double jobCalc(int&, int&, double&);
void display(string, string, int, int, double);
int main()
{
int job;
int hours;
double pay;
string firstname, lastname;
getName(firstname, lastname);
getJob(job);
hoursWorked(hours);
jobCalc(job, hours, pay);
display(firstname, lastname, job, hours, pay);
return 0;
}
string getName(string firstname, string lastname)
{
cout << "Enter your name (First): ";
cin >> firstname;
cout << "Enter your name (Last): ";
cin >> lastname;
return firstname, lastname;
}
int getJob(int job&)
{
cout << "Yo What motha duckin job number is you? Ya' dig? (10, 20, or 30): ";
cin >> job;
bool jobNo = false;
while (jobNo = false)
{
if (job == 10)
{
jobNo = true;
}
else if (job == 20)
{
jobNo = true;
}
else if (job == 30)
{
jobNo = true;
}
else if (job < 9 || job > 11 || job < 19 || job > 21 || job < 29 || job > 31)
{
cout << "Yo! Please re-enter a correct value. What it is, mostly, Mama! Don't make me shank ya!" << endl;
cout << "Yo What motha' duckin' job number is you? Ya' dig? (10, 20, or 30): ";
cin >> job;
}
}
return job;
}
int hoursWorked(int hours)
{
cout << "Yo! How geezery hours dahd ya work?: ";
cin >> hours;
return hours;
}
double jobCalc(int& job, int& hours, double& pay)
{
if (job == 10)
{
if (hours < 40)
{
pay = hours * 8.75;
}
else if (hours < 60)
{
pay = 8.75 * 40;
hours = hours - 40;
pay = pay + (hours * 13.125);
}
else
{
pay = 8.75 * 40;
pay = pay + (13.125 * 20);
hours = hours - 60;
pay = pay + (17.5 * hours);
}
}
else if (job == 20)
{
if (hours < 40)
{
pay = hours * 12.25;
}
else if (hours < 60)
{
pay = 12.25 * 40;
hours = hours - 40;
pay = pay + (hours * 18.375);
}
else
{
pay = 12.25 * 40;
pay = pay + (18.375 * 20);
hours = hours - 60;
pay = pay + (24.5 * hours);
}
}
else if (job == 30)
{
if (hours < 40)
{
pay = hours * 13.75;
}
else if (hours < 60)
{
pay = 13.75 * 40;
hours = hours - 40;
pay = pay + (hours * 20.625);
}
else
{
pay = 13.75 * 40;
pay = pay + (20.625 * 20);
hours = hours - 60;
pay = pay + (27.5 * hours);
}
}
return pay;
}
void display(string firstname, string lastname, int job, int hours, double pay)
{
std::cout << std::fixed;
std::cout << std::setprecision(2);
cout << firstname << " " << lastname << " has job " << job << " and worked " << hours << " hours, thus earning them $" << setprecision(2) << pay << "." << endl;
}
all of your functions are returning values. where you are storing it?. multiple return is not possible as you are using. try the concept of pointers(references)

Regarding Structs and Using Them With Array's

I'm trying to write a program that will take every line from a text file and load the value into an array. For some reason however, when I try create a dynamic array and try to put information in any position beyond 0, the information from from position zero gets copied over and I can't seem to understand why. Specifically in this program its in the readInventory function I have written. Basically, why can't I copy one struct to the other?
Sample from file
A009 Strawberries_Case 0 12.50 8
4028 STRAWBERRIES_PINT 0 0.99 104
4383 MINNEOLAS 1 0.79 187.3
4261 Rice_1_LB_Bag 0 0.49 107
Code from program
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <fstream>
using namespace std;
struct Product
{
string PLU;
string name;
int salesType;
double unitPrice/*rice per pound*/;
double inventory;
};
struct ItemSold
{
string PLU;
string name;
double cost;
};
Product *inventoryLevels = new Product[100];
ItemSold *itemsSold = new ItemSold[100];
bool readInventory(string filename, int &numberOfItems);
double checkout(int inventoryLength);
double price(string PLU, double units);
int typeCheck(string PLU, int inventoryLength);
string nameCheck(string PLU, int inventoryLength);
int main()
{
int numberOfItems = 0;
string filename = "products.txt";
int total;
if (readInventory(filename, numberOfItems))
{
cout << "Inventory file has errors, please make changes before continuing" << endl << endl;
}
total = checkout(numberOfItems);
cout << total;
system("pause");
}
double checkout(int inventoryLength)
{ // Function that will be used to perform the checkout by the user
string PLU = "1";
double units/*pounds*/;
int salesType;
int counter = 0;
int temp;
double total = 0;
while (PLU != "0")
{
cout << "Enter a PLU: ";
cin >> PLU;
itemsSold[counter].PLU = PLU;
if (PLU == "0")
{
// do nothing
}
else
{
itemsSold[counter].name = nameCheck(PLU, inventoryLength);
if (typeCheck(PLU, inventoryLength) == 0)
{
cout << " Enter the number of units being bought: ";
cin >> units;
while (units > inventoryLevels[counter].inventory)
{
cout << "You have entered in more units than we have on hand \n Please reduce the number of units being bought\n";
cout << " Enter the number of units being bought: ";
cin >> units;
}
itemsSold[counter].cost = price(PLU, units);
inventoryLevels[counter].inventory -= units;
}
else
{
cout << "Enter the number of pounds of the item being bought: ";
cin >> units;
itemsSold[counter].cost = price(PLU, units);
while (units > inventoryLevels[counter].inventory)
{
cout << "You have entered in more pounds than we have on hand \n Please reduce the number of pounds being bought\n";
cout << "Enter the number of pounds of the item being bought: ";
cin >> units;
}
inventoryLevels[counter].inventory -= units;
}
counter++;
}
}
temp = counter;
while (temp >= 0)
{
total += itemsSold[temp].cost;
temp--;
}
return total;
}
string nameCheck(string PLU, int inventoryLength)
{
for (int k = 0; k < inventoryLength; k++)
{
if (inventoryLevels[k].PLU == PLU)
{
return inventoryLevels[k].name;
}
}
return "We are currently out of stock of this item.";
}
int typeCheck(string PLU, int inventoryLength)
{
for (int k = 0; k < inventoryLength ; k++)
{
if (inventoryLevels[k].PLU == PLU)
{
return inventoryLevels[k].salesType;
}
}
}
double price(string PLU, double units)
{ //
double price;
for (int k = 0; k < 100; k++)
{
if (inventoryLevels[k].PLU == PLU)
{
price = units * (inventoryLevels[k].unitPrice);
return price;
}
}
}
bool readInventory(string filename, int &numberOfItems)
{
// File object
fstream inventory;
// Some temp variable used to validate information is still in file while it is being transfered to array
//string temp;
// Open the inventory file
inventory.open(filename);
// Will temporarily hold the properties of an item until loaded onto the array
Product temp;
// Counter will allow for a new item to be stored onto the next available location in the array
int counter = 0;
// Will demonstrate whether or not there is an error
int error = 0;
// Store items and their properties in the global array
while (inventory >> temp.PLU >> temp.name >> temp.salesType >> temp.unitPrice >> temp.inventory)
{
// Checks to see if they
if ((temp.PLU.at(0) > 57) || (temp.PLU.at(1) > 57) || (temp.PLU.at(2) > 57) || (temp.PLU.at(3) > 57))
{
error++;
}
else
{
inventoryLevels[numberOfItems].PLU = temp.PLU;
inventoryLevels[numberOfItems].name = temp.name;
inventoryLevels[numberOfItems].salesType = temp.salesType;
inventoryLevels[numberOfItems].unitPrice = temp.unitPrice;
inventoryLevels[numberOfItems].inventory = temp.inventory;
numberOfItems++;
counter++;
}
}
// If there is no error return true
if (error == 0)
{
return false;
}
// If there is an error return false
else if (error > 0)
{
return true;
}
}
When you assign values here,
while (inventory >> temp.PLU >> temp.name >> temp.salesType >> temp.unitPrice >> temp.inventory)
Am I right to assume that the input file is in the format (since you're assigning each line to the variables?
line 1: Some string you want assigned to PLU
line 2: Some string you want assigned to name
line 3: Some Int you want assigned to salestype
..........
..........
line n:string PLU

C++ Calculation Problem, Always returns $0

I have to display and loop a menu, allowing the customer to make multiple orders for peanuts, movies, or books. The menu displays fine, but the quantity doesn't go down to the calculations part of my code. Everytime I enter a quantity for anything and checkout it returns $0. I have no idea why this is happening I don't see anything wrong with my code, but obviously there is. Do you guys have any suggestions on what to do based on looking at what I have?
#include <iostream>
#include <cstdlib>
using namespace std;
//function declarations
void displayMenu();
//constant statements
const double BOOK_PRICE = 9.00; //price per book
const double BOOK_SHIPPING = 1.06; //shipping per book
const double MOVIE_PRICE = 13.99; //price per movie
const double MOVIE_SHIPPING = .05; //shipping per movie subtotal
const double PEANUT_PRICE = 1.80; //price of peanuts per pound
const double SHIPPING_PRICE = .50; //shipping of peanuts per lb
int main()
{
//declaration statements
int numBooks = 0; //# of books purchased
int numMovies = 0; //# of movies purchased
double numPeanuts = 0.0; //# of peanuts per pound
double bookSubtotal = 0.0; //subtotal of books
double movieSubtotal = 0.0; //subtotal of movies
double peanutSubtotal = 0.0; //subtotal of peanuts
int totalBooks = 0; //running total of books
int totalMovies = 0; //running total of movies
double totalPeanuts = 0.0; //running total of peanuts
int userChoice = 0; //user input
double totalPrice = 0.0; //final price
while (userChoice != 4)
{
displayMenu();
cout << "Enter a menu choice: ";
cin >> userChoice;
if (userChoice == 1)
{
cout << "Please enter the number of books: ";
cin >> numBooks;
totalBooks = totalBooks + numBooks;
}
else if (userChoice == 2)
{
cout << "Please enter the number of movies: ";
cin >> numMovies;
totalMovies = totalMovies + numMovies;
}
else if (userChoice == 3)
{
cout << "Please enter the pounds of peanuts as a decimal: ";
cin >> numPeanuts;
totalPeanuts = totalPeanuts + numPeanuts;
}
else if (userChoice == 4)
{
break;
}
else
{
cout << "Invalid Input" << endl;
}
}
//computations
bookSubtotal = (totalBooks * BOOK_PRICE) + (totalBooks * BOOK_SHIPPING);
movieSubtotal = (totalMovies * MOVIE_PRICE * .05) + (totalMovies * MOVIE_PRICE);
peanutSubtotal = (PEANUT_PRICE * totalPeanuts) + (totalPeanuts * .5);
totalPrice = bookSubtotal + movieSubtotal + peanutSubtotal;
cout << "The total price is $" << totalPrice << endl;
system("PAUSE");
return 0;
}//end of main
void displayMenu()
{
cout << "1 Books" << endl;
cout << "2 Movies" << endl;
cout << "3 Peanuts" << endl;
cout << "4 Checkout" << endl;
}//end of displayMenu
The problem is in the cin >> - you found the answer yourself when you say that the book count is zero. I suggest you try to put << endl after each cout << .... Other solution is to use _flushall(); after each cout.