lining up a table using precision rules c++ - c++

I am simply trying to make my console out look like the attached picture. trying to left justify, or something similar to make time and speed more in line with the table. Please help me figure out exactly what I need to do, to make my output look exactly like the attached image.
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
char RacerName [20];
int checkPointOneTime;
int checkPointTwoTime;
int checkPointThreeTime;
int checkPointFourTime;
int finishTime;
cout << "Enter the racer's first name: ";
cin >> RacerName;
cout << "Enter the time (in minutes) at Check Point 1: ";
cin >> checkPointOneTime;
cout << "Enter the time (in minutes) at Check Point 2: ";
cin >> checkPointTwoTime;
cout << "Enter the time (in minutes) at Check Point 3: ";
cin >> checkPointThreeTime;
cout << "Enter the time (in minutes) at Check Point 4: ";
cin >> checkPointFourTime;
cout << "Enter the time (in minutes) at the Finish Line: ";
cin >> finishTime;
cout << "\nLOTOJA After Action Review for John" << endl << endl;
cout << setw(24) << "Interval Details" << endl << endl;
int checkPointOneDistance = 44;
int checkPointTwoDistance = 87;
int checkPointThreeDistance = 128;
int checkPointFourDistance = 165;
int checkPointFinalDistance = 207;
int checkPointOneInterval = checkPointOneDistance;
int checkPointTwoInterval = checkPointTwoDistance - checkPointOneDistance;
int checkPointThreeInterval = checkPointThreeDistance - checkPointTwoDistance;
int checkPointFourInterval = checkPointFourDistance - checkPointThreeDistance;
int checkPointFiveInterval = checkPointFinalDistance - checkPointFourDistance;
int checkPointTimeOneInterval = checkPointOneTime;
int checkPointTimeTwoInterval = checkPointTwoTime - checkPointOneTime;
int checkPointTimeThreeInterval = checkPointThreeTime - checkPointTwoTime;
int checkPointTimeFourInterval = checkPointFourTime - checkPointThreeTime;
int checkPointTimeFiveInterval = finishTime - checkPointFourTime;
float minutesInHours = 60.0;
float speedIntervalOne = checkPointOneDistance / (checkPointTimeOneInterval / minutesInHours);
float speedIntervalTwo = checkPointTwoInterval / (checkPointTimeTwoInterval / minutesInHours);
float speedIntervalThree = checkPointThreeInterval / (checkPointTimeThreeInterval / minutesInHours);
float speedIntervalFour = checkPointFourInterval / (checkPointTimeFourInterval / minutesInHours);
float speedIntervalFive = checkPointFiveInterval / (checkPointTimeFiveInterval / minutesInHours);
cout << setw(11) << "Location" << setw(17.5) << " Distance" << setw(16.5) << " Time" << setw(18) << " Speed" << endl;
cout << "Interval 1" << setfill('.') << setw(16) << checkPointOneInterval << setfill (' ') << setw(16.5) << checkPointTimeOneInterval << setw(20) << setprecision(3) << fixed << speedIntervalOne << endl;
cout << "Interval 2" << setfill('.') << setw(16) << checkPointTwoInterval << setfill(' ') << setw(16.5) << checkPointTimeTwoInterval << setw(20) << setprecision(3) << speedIntervalTwo << endl;
cout << "Interval 3" << setfill('.') << setw(16) << checkPointThreeInterval << setfill(' ') << setw(16.5) << checkPointTimeThreeInterval << setw(20) << setprecision(3) << speedIntervalThree << endl;
cout << "Interval 4" << setfill('.') << setw(16) << checkPointFourInterval << setfill(' ') << setw(16.5) << checkPointTimeFourInterval << setw(20) << setprecision(3) << speedIntervalFour << endl;
cout << "Interval 5" << setfill('.') << setw(16) << checkPointFiveInterval << setfill(' ') << setw(16.5) << checkPointTimeFiveInterval << setw(20) << setprecision(3) << speedIntervalFive << endl;
int totalRaceCheckPoints = 5;
float totalAverageSpeed = checkPointFinalDistance / static_cast<float>(finishTime / minutesInHours);
cout << "\nThe average speed for the entire course was " << totalAverageSpeed << " mph. " << endl << endl;
return 0;
}

looks like you just need to enter in another space between the actual distance numbers and the time numbers
change your 16.5's to 17.5's like so
cout << setw(11) << "Location" << setw(17.5) << " Distance" << setw(16) << " Time" << setw(18) << " Speed" << endl;
cout << "Interval 1" << setfill('.') << setw(16) << checkPointOneInterval << setfill(' ') << setw(17.5) << checkPointTimeOneInterval << setw(20) << setprecision(3) << fixed << speedIntervalOne << endl;
cout << "Interval 2" << setfill('.') << setw(16) << checkPointTwoInterval << setfill(' ') << setw(17.5) << checkPointTimeTwoInterval << setw(20) << setprecision(3) << speedIntervalTwo << endl;
cout << "Interval 3" << setfill('.') << setw(16) << checkPointThreeInterval << setfill(' ') << setw(17.5) << checkPointTimeThreeInterval << setw(20) << setprecision(3) << speedIntervalThree << endl;
cout << "Interval 4" << setfill('.') << setw(16) << checkPointFourInterval << setfill(' ') << setw(17.5) << checkPointTimeFourInterval << setw(20) << setprecision(3) << speedIntervalFour << endl;
cout << "Interval 5" << setfill('.') << setw(16) << checkPointFiveInterval << setfill(' ') << setw(17.5) << checkPointTimeFiveInterval << setw(20) << setprecision(3) << speedIntervalFive << endl;

Related

Calling while loop function in main function, and won't move past after completing loop

So, I'm creating a program for class to take orders of cookies, and the guideline is to create separate functions. Unfortunately, when I try to get into the subsequent functions, it doesn't seem to be doing that, and I was wondering where my problem may be at? I've tried playing around with parameters and data types and seeing if that would do anything, unfortunately I have come up short.
I appreciate all your help guys.
For people looking at this in the future, I've completed this program to work, and this first bit of code is the original code I was asking the question about... carry on to next portion.
#include <iostream>
using namespace std;
int getThinMints();
int getOreos();
int main(){
getThinMints();
getOreos();
return 0;
}
int getThinMints(){
int numThinMints;
int min_order = 0;
int max_order = 10;
while ((numThinMints < 0 || numThinMints > 10))
{
cout << "Enter the number of Thin Mints (0-10): ";
cin >> numThinMints;
cin.ignore(100, '\n');
cin.clear();
}
}
int getOreos(){
int numOreos;
int min_order = 0;
int max_order = 10;
while ((numOreos < 0 || numOreos > 10))
{
cout << "Enter the number of Thin Mints (0-10): ";
cin >> numOreos;
cin.ignore(100, '\n');
cin.clear();
}
}
New code I implemented for the complete program:
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <iomanip>
using namespace std;
int getCookies(string cookietype, int max_order); // cookie input function prototype
int main()
{
int thin_mints;
int lemonUps;
int lemonades;
int samoas;
int tagalongs;
int dosidos;
int trefoils;
int thanksalot;
int toffee;
int caramel;
int max_order = 10; // 10 cookies max per type
int orderNumber = 0; // initial order number
double cPrice = 5.0; // $5 per order
string qtyStr = "(0-10): ";
string cookie1 = "Enter the number of Thin Mints " + qtyStr;
string cookie2 = "Enter the number of Lemon-Ups " + qtyStr;
string cookie3 = "Enter the number of Lemonades " + qtyStr;
string cookie4 = "Enter the number of Samoas " + qtyStr;
string cookie5 = "Enter the number of Tagalongs " + qtyStr;
string cookie6 = "Enter the number of Do-si-dos " + qtyStr;
string cookie7 = "Enter the number of Trefoils " + qtyStr;
string cookie8 = "Enter the number of Thanks-A-Lot " + qtyStr;
string cookie9 = "Enter the number of Toffee-tastic " + qtyStr;
string cookie10 = "Enter the number of Caramel Chocolate Chip " + qtyStr;
string fname, lname, address, cityStZip;
bool moreOrders = true;
ofstream outFile; // This will open a .txt file named 'na_orders' with an invoice for the cookie orders
// Open the output file
outFile.open ("na_orders.txt", ios::out);
while (moreOrders)
{
fname = lname = address = cityStZip = "";
cout << "Enter the customers first and last name (or q to quit): " <<endl;
cin >> fname;
if (fname == "q")
break;
orderNumber ++;
cin >> lname;
cin.ignore();
cin.clear();
while (address.length() == 0)
{
cout << "Enter the " << fname << " " << lname << "'s address: ";
getline(cin, address);
}
while (cityStZip.length() == 0)
{
cout << "Enter the " << fname << " " << lname << "'s city, state and zip code: ";
getline(cin, cityStZip);
}
/* The following section is going to be where the user will
input their specificed amounts for each cookie type */
thin_mints = getCookies(cookie1, max_order); // input for Thin Mints
cout << "Thin Mints amount: " << thin_mints << endl;
lemonUps = getCookies(cookie2, max_order); // input for Lemon-Ups
cout << "Lemon-Ups amount: " << lemonUps << endl;
lemonades = getCookies(cookie3, max_order); // input for Lemonades
cout << "Lemonades amount: " << lemonades << endl;
samoas = getCookies(cookie4, max_order); // input for Samoas
cout << "Samoas amount: " << samoas << endl;
tagalongs = getCookies(cookie5, max_order); // input for Tagalongs
cout << "Tagalongs amount: " << tagalongs << endl;
dosidos = getCookies(cookie6, max_order); // input for Do-si-dos
cout << "Do-si-dos amount: " << dosidos << endl;
trefoils = getCookies(cookie7, max_order); // input for Trefoils
cout << "Trefoils amount: " << trefoils << endl;
thanksalot = getCookies(cookie8, max_order); // input for Thanks-A-Lot
cout << "Thanks-A-Lot amount: " << thanksalot << endl;
toffee = getCookies(cookie9, max_order); // input for Toffee-tastic
cout << "Toffee-tastic amount: " << toffee << endl;
caramel = getCookies(cookie10, max_order); // input for Caramel Chocolate Chip
cout << "Caramel Chocolate Chip amount: " << caramel << endl;
if (outFile.is_open())
{
outFile << "================================" << endl;
outFile << "=== Girl Scout Cookie Invoice ===" << endl;
outFile << "================================\n" << endl;
string dateStr;
time_t result = time(NULL);
dateStr = ctime(&result);
outFile << dateStr << endl;
outFile << fname << " " << lname << endl;
outFile << address << endl;
outFile << cityStZip << endl;
outFile << endl;
outFile << "Order Number: " << orderNumber << endl;
outFile << endl;
outFile << "Dear " << fname << " " << lname << ":" << endl;
outFile << endl;
outFile << "Your Girl Scout Cookie order has arrived." << endl;
outFile << "Your order consists of the following:" << endl;
outFile << "-------------------------------------------------" << endl;
outFile << "|" << setw(14) << "Cookie" << setw(11) << "|" << setw(9) << "Quantity" << setw(2) << "|" << setw(7) << "Cost" << setw(5) << "|" << endl;
outFile << "-------------------------------------------------" << endl;
outFile << "|" << setw(11) << "Thin Mints" << setw(14) << "|" << setw(9) << thin_mints << setw(2) << "|" << setw(2) << "$" << setw(8) << fixed << setprecision(2) << thin_mints * cPrice << setw(2) << "|" << endl;
outFile << "|" << setw(9) << "Lemon-up" << setw(16) << "|" << setw(9) << lemonUps << setw(2) << "|" << setw(2) << "$" << setw(8) << fixed << setprecision(2) << lemonUps * cPrice << setw(2) << "|" << endl;
outFile << "|" << setw(10) << "Lemonades" << setw(15) << "|" << setw(9) << lemonades << setw(2) << "|" << setw(2) << "$" << setw(8) << fixed << setprecision(2) << lemonades * cPrice << setw(2) << "|" << endl;
outFile << "|" << setw(7) << "Samoas" << setw(18) << "|" << setw(9) << samoas << setw(2) << "|" << setw(2) << "$" << setw(8) << fixed << setprecision(2) << samoas * cPrice << setw(2) << "|" << endl;
outFile << "|" << setw(10) << "Tagalongs" << setw(15) << "|" << setw(9) << tagalongs << setw(2) << "|" << setw(2) << "$" << setw(8) << fixed << setprecision(2) << tagalongs * cPrice << setw(2) << "|" << endl;
outFile << "|" << setw(10) << "Do-si-dog" << setw(15) << "|" << setw(9) << dosidos << setw(2) << "|" << setw(2) << "$" << setw(8) << fixed << setprecision(2) << dosidos * cPrice << setw(2) << "|" << endl;
outFile << "|" << setw(9) << "Trefoils" << setw(16) << "|" << setw(9) << trefoils << setw(2) <<"|" << setw(2) << "$" << setw(8) << fixed << setprecision(2) << trefoils * cPrice << setw(2) << "|" << endl;
outFile << "|" << setw(13) << "Thanks-A-Lot" << setw(12) << "|" << setw(9) << thanksalot << setw(2) << "|" << setw(2) << "$" << setw(8) << fixed << setprecision(2) << thanksalot * cPrice << setw(2) << "|" << endl;
outFile << "|" << setw(14) << "Toffee-Tastic" << setw(11) << "|" << setw(9) << toffee << setw(2) << "|" << setw(2) << "$" << setw(8) << fixed << setprecision(2) << toffee * cPrice << setw(2) << "|" << endl;
outFile << "|" << setw(23) << "Caramel Chocolate Chip" << setw(2) << "|" << setw(9) << caramel << setw(2) << "|" << setw(2) << "$" << setw(8) << fixed << setprecision(2) << caramel * cPrice << setw(2) << "|" << endl;
outFile << "-------------------------------------------------" << endl;
outFile << "|" << setw(33) << "Total Due:" << setw(3) << "|" << setw(2) << "$" << setw(8) << fixed << setprecision(2) << cPrice * (thin_mints + lemonUps + lemonades + samoas + tagalongs + dosidos + trefoils + thanksalot + toffee + caramel) << setw(2) << "|" << endl;
outFile << "-------------------------------------------------" << endl;
}
}
}
int getCookies(string cookietype, int max_order)
{
int quantity = -1;
int min_order = 0;
while ((quantity < min_order) || (quantity > max_order))
{
cout << cookietype;
cin >> quantity;
cin.clear();
}
return quantity;
}
You need to enter the value of a variable before you use it, not afterwards. For example
for (;;) // loop until we break
{
// get the number of oreos
cout << "Enter the number of Oreos (0-10): ";
cin >> numOreos;
// check the number of oreos is OK
if (numOreos >= 0 && numOreos <= 10)
break; // quit the loop if it is
// ignore any extraneous input and try again
cin.ignore(100, '\n');
cin.clear();
}
See that the test on the number of Oreos happens after the user has input a value, not before.
In your code with the while loop you were checking the value of numOreos when it had not yet been given a value. C++ programs are sequences of instructions and the order things happen is important. They are not declarations of intent where you just say 'I want the variable to be within these values' and leave it up the computer to figure it out.

Searching for specific position of binary file to start reading or writing

Lets say you have a data file with multiple entries of struct type data
Based on the code number variable of one of the data entries,you are supposed to draw out the remaining variables of the structure.
Or maybe should I try to find the record number based on the scode?
then using the record number to find the size I need to skip from the beginning using seekg
char scode[MAX];
Subject M;
afile.open (fileName, ios::in | ios::out | ios::binary);
cout << "Enter Subject code: ";
cin >> scode;
cin.clear();
cin.ignore(100,'\n');
cout << endl << endl;
while (afile.read (reinterpret_cast <char *>(&M), sizeof (M)))
{
if (strcmp(scode,M.subCode) == 1)
{
cout << "Subject code not found" << endl;
cout << "----------------------------" << endl;
return;
}
else
{
afile.seekg(-strlen(M.subCode),ios::cur);
cout << "Subject Code: " << M.subCode << endl
<< "Subject Name: " << M.subTitle << endl;
cout << left << setw(8) << "Task"
<< left << setw(14) << "Title"
<< right << setw(7) << "Weight"
<< right << setw(7) << "Upon"
<< right << setw(7) << "Mark"
<< right << setw(12) << "Obtained"
<< endl << endl;
cout << setw (66) << setfill ('-') << "-" << endl;
cout << setfill (' ');
while (afile.read (reinterpret_cast <char *>(&M), sizeof (M)))
{
for (int i = 1; i <= M.noTask; i++)
{
cout << left << setw(8) << i
<< left << setw(14) << M.Task[i].title
<< right << setw(7) << M.Task[i].weight
<< right << setw(7) << M.Task[i].fullmark
<< right << setw(7) << M.Task[i].mark
<< right << setw(12) << M.Task[i].mark / M.Task[i].fullmark * M.Task[i].weight
<< endl;
}
}
}

Basic C++ Application has extra output then it should

So I'm writing a basic application and for some reason when I run the program a bunch of numbers pop up before my intended output. It was working fine until I added the "std::cout" lines to have the outputs only be 2 decimals. The general gist of the application is a program acts as a self-checkout register at a store and lets the user buy 2 items. And yes I know the code probably looks really bad, I'm still super new to C++.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float price1;
float number1;
float price2;
float number2;
float priceofitemplustax1;
float priceofitemplustax2;
float total;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << price1;
std::cout << price2;
std::cout << priceofitemplustax1;
std::cout << priceofitemplustax2;
std::cout << total;
cout << endl << "Please scan your first item." <<endl;
cin.get();
cout << endl << "How many of that item are you buying? "<<endl;
cin >> number1;
cout << endl << "How much is that item?"<<endl;
cin >> price1;
priceofitemplustax1 = (number1 * price1) * 1.0875;
cout << endl << "So you want " << number1 << " of this item? Adding tax that will be " << priceofitemplustax1 << "."<<endl;
cin.get();
cout << endl << "Please scan your second item."<<endl;
cin.get();
cout << endl << "How many of that item are you buying? "<<endl;
cin >> number2;
cout << endl << "How much is that item?"<<endl;
cin >> price2;
priceofitemplustax2 = (number2 * price2) * 1.0875;
cout << endl << "So you want " << number2 << " of this item? Adding tax that will be " << priceofitemplustax2 << "."<<endl;
cin.get();
total = priceofitemplustax1 + priceofitemplustax2;
cout << endl << "So your final total for this shopping trip including tax is " << total << "."<<endl;
cin.get();
cout << endl << "Your reciept will print below."<<endl;
cin.get();
cout << setw(14) << right << "Number of Item" << setw(10) << right << "Price" << setw(20) << "Price plus tax" << endl;
cout << setw(14) << right << number1 << setw(10) << right << price1 << setw(20) << priceofitemplustax1 << endl;
cout << setw(14) << right << number2 << setw(10) << right << price2 << setw(20) << priceofitemplustax2 << endl;
cout << endl;
cout << endl;
cout << setw(8) << right << "Total is" << setw(10) << total << price2 << endl;
cin.get();
}
std::cout << std::setprecision(2);
std::cout << price1;
std::cout << price2;
std::cout << priceofitemplustax1;
std::cout << priceofitemplustax2; std::cout << total;
here you write 5 floats
The lines
std::cout << std::fixed; // sets a format
std::cout << std::setprecision(2); // sets a format
set the streams output format.
The lines
std::cout << price1; // outputs a number
std::cout << price2; // outputs a number
std::cout << priceofitemplustax1; // outputs a number
std::cout << priceofitemplustax2; // outputs a number
std::cout << total; // outputs a number
print the variables to the stream.
Just remove the variable output lines. Do not accept this answer - Credit goes to manni66

How do I create an accumulator in C++ using a user defined function?

I am trying to keep a running total of cups of coffee sold and I have to use a user defined function to do it. I have tried numerous variations of the attached code but nothing seems to work. What am I doing wrong? Also I am a newb to C++ so that is why it looks amateurish!
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int SM_OZ = 8;
const int MD_OZ = 12;
const int LG_OZ = 16;
const double SM_PRICE = 1.19;
const double MD_PRICE = 1.49;
const double LG_PRICE = 1.89;
const double TAX = .0825;
void amtSold(int &smtCup, int &mdtCup, int &lgtCup);
int main()
{
int selection;
int smCup;
int mdCup;
int lgCup;
int smtCup;
int mdtCup;
int lgtCup;
smCup = 0;
mdCup = 0;
lgCup = 0;
do
{
cout << "COFFEE SHOP" << endl;
cout << "1. Sell Coffee" << endl;
cout << "2. Total Number of Cups Sold" << endl;
cout << "3. Total Amount of Coffee Sold" << endl;
cout << "4. Total Amount of Money made" << endl;
cout << "0. Exit" << endl;
cout << "Type a number to continue: ";
cin >> selection;
cout << endl;
//loop through the solutions based on the user's selection
switch (selection)
{
case 1:
cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;
system("cls");
cout << fixed << setprecision(2) << endl;
//Sale Coffee Receipt Page
cout << "COFFEE SHOP" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE)+(mdCup*MD_PRICE)+(lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))+(((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 2:
//Total Number of Cups Sold
cout << "REPORT - NUMBER OF COFFEE CUPS SOLD" << endl;
amtSold(smtCup, mdtCup, lgtCup);
cout << "SIZE" << setw(21) << "Number" << endl;
cout << "Small: " << setw(18) << smCup << endl;
cout << "Medium: " << setw(17) << mdCup << endl;
cout << "Large: " << setw(18) << lgCup << endl;
cout << endl;
cout << endl;
break;
case 3:
//Total Amount of Coffee Sold
cout << "REPORT - AMOUNT OF COFFEE SOLD" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "OZ" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << smCup*SM_OZ << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << mdCup*MD_OZ << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << lgCup*LG_OZ << endl;
cout << "Total: " << setw(36) << (smCup*SM_OZ) + (mdCup*MD_OZ) + (lgCup*LG_OZ) << endl;
cout << endl;
cout << endl;
break;
case 4:
//Total Amount of Money made
cout << "COFFEE SHOP - REPORT MONEY MADE" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE)) + (((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 0:
system("cls");
break;
default:
//notify the user that an invalid selection has been inputted
cout << "You have made an invalid selection. Please choose a number from the list." << endl;
cout << endl;
}
} while (selection != 0);
system("pause");
return 0;
}
void amtSold(int &smtCup, int &mdtCup, int &lgtCup)
{
int smCup;
int mdCup;
int lgCup;
smCup = 0;
mdCup = 0;
lgCup = 0;
smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;
}
So as you probably know, you're not keeping track of the number of coffee cups of each size that you're selling (i.e. smtCup, mdtCup, and lgtCup).
I'm assuming that these variables mean the total number of cups for each size, you might want to put some comments during the variable declaration step. You'll want to initialise the variables to 0:
int smtCup = 0;
int mdtCup = 0;
int lgtCup = 0;
As this is a fairly simple program, you can perform accumulation without using your amtSold function, so you can delete that.
Then, in case 1 of your switch statement, you'll want to update smtCup, mdtCup, and lgtCup every time you update the values. Please be aware that smCup, mdCup, and lgCup are used only for input in this program.
cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;
smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;
From here on out, you can print out the total number of small, medium and large cups by calling smtCup, mdtCup, and lgtCup in the other cases! Change smCup, mdCup, and lgCup to smtCup, mdtCup, and lgtCup in cases 2-4. Hope this helps!
Edit: Can't comment, so I'll just say you're welcome here!
Thanks KTing! It is disappointing to know I was much closer to a correct answer with an earlier version of my code. I could not figure out why it would not initialize and so I started to get desperate and try things I was 95% sure would not work. I ended up going with the following solution.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
//Constant for size of cup of coffee
const int SM_OZ = 8;
const int MD_OZ = 12;
const int LG_OZ = 16;
//Constant for price of cup of coffee and Tax
const double SM_PRICE = 1.19;
const double MD_PRICE = 1.49;
const double LG_PRICE = 1.89;
const double TAX = .0825;
int main()
{
//declare and initialize the variables for the individual cups of coffee
int selection;
int smCup = 0;
int mdCup = 0;
int lgCup = 0;
//declare and initialize the variables for the total cups of coffee
int smtCup = 0;
int mdtCup = 0;
int lgtCup = 0;
do
{
//get input from user as to what they want to do
cout << "COFFEE SHOP" << endl;
cout << "1. Sell Coffee" << endl;
cout << "2. Total Number of Cups Sold" << endl;
cout << "3. Total Amount of Coffee Sold" << endl;
cout << "4. Total Amount of Money made" << endl;
cout << "0. Exit" << endl;
cout << "Type a number to continue: ";
cin >> selection;
cout << endl;
//loop through the solutions based on the user's selection
switch (selection)
{
case 1:
//get the number of cups of coffee from the user
cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;
//get the total cups of coffee and store it as a variable
smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;
system("cls");
cout << fixed << setprecision(2) << endl;
//Sale Coffee Receipt Page
cout << "COFFEE SHOP" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE)+(mdCup*MD_PRICE)+(lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))+(((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 2:
//Total Number of Cups Sold
cout << "REPORT - NUMBER OF COFFEE CUPS SOLD" << endl;
cout << "SIZE" << setw(21) << "Number" << endl;
cout << "Small: " << setw(18) << smtCup << endl;
cout << "Medium: " << setw(17) << mdtCup << endl;
cout << "Large: " << setw(18) << lgtCup << endl;
cout << endl;
cout << endl;
break;
case 3:
//Total Amount of Coffee Sold
cout << "REPORT - AMOUNT OF COFFEE SOLD" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "OZ" << endl;
cout << "Small: " << setw(18) << smtCup << setw(18) << smtCup*SM_OZ << endl;
cout << "Medium: " << setw(17) << mdtCup << setw(18) << mdtCup*MD_OZ << endl;
cout << "Large: " << setw(18) << lgtCup << setw(18) << lgtCup*LG_OZ << endl;
cout << "Total: " << setw(36) << (smtCup*SM_OZ) + (mdtCup*MD_OZ) + (lgtCup*LG_OZ) << endl;
cout << endl;
cout << endl;
break;
case 4:
//Total Amount of Money made
cout << "COFFEE SHOP - REPORT MONEY MADE" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smtCup << setw(18) << SM_PRICE << setw(18) << smtCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdtCup << setw(18) << MD_PRICE << setw(18) << mdtCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgtCup << setw(18) << LG_PRICE << setw(18) << lgtCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE)) + (((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 0:
system("cls");
break;
default:
//notify the user that an invalid selection has been inputted
cout << "You have made an invalid selection. Please choose a number from the list." << endl;
cout << endl;
}
//loop through if the user is still making a valid selection
} while (selection != 0);
system("pause");
return 0;
}

Help implementing a "store buying" program

My professor instructed us to make a Starbucks like menu where the user can continue to input orders until they are finished. I got the menu display down along with the loop, but I can't get it to add up the orders that were inputted and display a total.
#include <iostream>
using namespace std;
int main()
{
int choice = 1;
cout << endl << "Welcome to Hunterbucks!";
while (choice > 0)
{
cout << endl << "Input -1 when you're finished ordering!";
cout << endl << endl << "Coffee" << " " << "($)";
cout << endl << "1. Regular" << " " << "1.50";
cout << endl << "2. Decaf" << " " << "1.23";
cout << endl << "3. Americano" << " " << "2.25";
cout << endl << "4. Espresso" << " " << "2.25";
cout << endl << "5. Latte" << " " << "2.50";
cout << endl << "6. Cappuccino" << " " << "2.75";
cout << endl << "7. Frappuccino" << " " << "2.75";
cout << endl << "8. Macchiato" << " " << "2.50";
cout << endl << endl << "Snacks" << " " << "($)";
cout << endl << "9. Muffin" << " " << "1.00";
cout << endl << "10. Blueberry Muffin" << " " << "1.25";
cout << endl << "11. Raspberry Muffin" << " " << "1.25";
cout << endl << "12. Scone" << " " << "0.75";
cout << endl << "13. Blueberry Scone" << " " << "1.00";
cout << endl << "14. Croissant" << " " << "0.75";
cout << endl << endl << "What would you like to order? ";
cin >> choice;
if (choice <= 0)
cout << endl << "Thank you for your order.";
else
cout << endl << "What else would you like to order?";
}
cout << endl << "Thank you for choosing Hunterbucks! Come again soon.";
return 0;
}
Any info that can help me? I'm just a beginner and have been trying this for a few hours.
In pseudo-code you want something like this:
float total = 0.0;
while (choice > 0)
{
....
cin >> choice;
if (choice <= 0)
cout << endl << "Thank you for your order.";
else
{
total += costs[choice];
cout << endl << "What else would you like to order?";
}
}
You'll need to define an array names costs that contains the cost of each item. You'll also want to tackle validation of the user input so that you don't erroneously attempt to read outside the range of the costs array.
You're probably looking at something like this:
#include <iostream>
using namespace std;
int main()
{
int choice = 1;
float sum = 0.0;
float arr[] = {
0.00, 1.50, 1.23, 2.25, 2.25, 2.50, 2.75, 2.75, 2.50,
1.00, 1.25, 1.25, 0.75, 1.00, 0.75
};
cout << endl << "Welcome to Hunterbucks!";
while (choice > 0)
{
cout << endl << "Input -1 when you're finished ordering!";
cout << endl << endl << "Coffee" << " " << "($)";
cout << endl << "1. Regular" << " " << "1.50";
cout << endl << "2. Decaf" << " " << "1.23";
cout << endl << "3. Americano" << " " << "2.25";
cout << endl << "4. Espresso" << " " << "2.25";
cout << endl << "5. Latte" << " " << "2.50";
cout << endl << "6. Cappuccino" << " " << "2.75";
cout << endl << "7. Frappuccino" << " " << "2.75";
cout << endl << "8. Macchiato" << " " << "2.50";
cout << endl << endl << "Snacks" << " " << "($)";
cout << endl << "9. Muffin" << " " << "1.00";
cout << endl << "10. Blueberry Muffin" << " " << "1.25";
cout << endl << "11. Raspberry Muffin" << " " << "1.25";
cout << endl << "12. Scone" << " " << "0.75";
cout << endl << "13. Blueberry Scone" << " " << "1.00";
cout << endl << "14. Croissant" << " " << "0.75";
cout << endl << endl << "What would you like to order? ";
cin >> choice;
if (choice <= 0){
cout << endl << "Thank you for your order.";
} else {
cout << endl << "What else would you like to order?";
sum += arr[choice];
}
}
cout << "Total: " << sum << endl;
cout << endl << "Thank you for choosing Hunterbucks! Come again soon.";
return 0;
}
Do note the following:
1) Your menu choices being with '1' thus there is a need to offset your arr at index '0' with the '0.00' value there.
2) The cost added up follows that of your indexed array, thus you would probably want to format your output according to your array, so that next time, all you need to do is to update your array.
Hope it helped. Cheers!
The way you have your code set up warrants a switch statement, like the following:
double total = 0;
switch (choice)
{
case 1:
total += 1.50; // Regular.
break;
case 2:
total += 1.23; // Decaf.
break;
// Etc.
}
cout << endl << "Your total is " << total;
That being said, the easiest way to do this would be to have an array of prices:
double prices[] = {1.50, 1.23, 2.25};
// ...
total += prices[choice - 1]; // No switch statement needed.