I'm Using Visual studio (C++) in a class that I am taking, I've had to teach myself functions, and I've hit a small snag in the road that I'd appreciate some advice on.
What I'm having trouble with, is the part of the assignment that states i must
"Utilize a function that prints (not find) the largest/average/smallest commissions"
The way I read this, I'm assuming she only wants it to print, and not do calculations in the function.
A friend suggested I try void print(etc) However I'm unsure how to grab the calculation in main and give it to the function I'm trying to print, or am i going at it all wrong?
I've commented out the portion of code I was trying to function. You can also find it at the very bottom of the code.
Any suggestions/help is greatly appreciated, as most of what I've looked up haven't really dealt with this problem (that I've found)
#include <iostream>
#include <iomanip>
using namespace std;
#define TITLE "Alva's"
#define STANDARD 0.05
#define HYBRID 0.10
#define ELECTRIC 0.15
#define HOLD 50
void dashLine();
int getSalesId();
int runProgram();
char vehicleType();
double sellingPrice();
void print(double largeSmallAverage);
int main()
{
int tot_count,
id_num[HOLD], //* ID
r_ay = 0, //* array
s_count, //*standard
h_count, //*Hybrid
e_count, //*electric
hold_id, //*array Id
compare, //*Pass
change, //*change made when change has value
yesno;
double tot_standard,
tot_hybrid,
tot_electric,
tot_price,
price[HOLD],
hold_price, //*Array hold
comm_l, //* Large
comm_s, //* Small
hold_comm, //*Array hold
avg_comm,
tot_commission,
commission[HOLD];
char car[HOLD],
temp_car;
tot_count = 0;
s_count = 0;
h_count = 0;
e_count = 0;
tot_price = 0;
tot_standard = 0;
tot_hybrid = 0;
tot_electric = 0;
cout << "\n" << TITLE << " Commission Calculator";
yesno = runProgram();
while (yesno == 1)
{
dashLine();
id_num[r_ay] = getSalesId();
car[r_ay] = vehicleType();
price[r_ay] = sellingPrice();
tot_price += price[r_ay];
switch (car[r_ay])
{
case 'S':
case 's':
commission[r_ay] = (price[r_ay] * STANDARD);
tot_standard += commission[r_ay];
s_count++;
break;
case 'H':
case 'h':
commission[r_ay] = (price[r_ay] * HYBRID);
tot_hybrid += commission[r_ay];
h_count++;
break;
case 'E':
case 'e':
commission[r_ay] = (price[r_ay] * ELECTRIC);
tot_electric += commission[r_ay];
e_count++;
break;
}
cout << "\n The commission for this sale, for Employee ID: " << fixed << setprecision(0) << id_num[r_ay] << " is:$ " << fixed << setw(5) << setprecision(2) << commission[r_ay];
cout << "\n";
yesno = runProgram();
r_ay++;
if (r_ay >= HOLD)
yesno = 0;
}
tot_count = (s_count + h_count + e_count);
tot_commission = (tot_standard + tot_hybrid + tot_electric);
{
cout << "\n Number of standard vehicle commissions calculated = " << fixed << setw(8) << setprecision(0) << s_count;
cout << "\n Number of hybrid vehicle commissions calculated = " << fixed << setw(8) << h_count;
cout << "\n Number of electric vehicle commissions calculated = " << fixed << setw(8) << e_count;
cout << "\n Number of vehicle commissions calculated = " << fixed << setw(8) << tot_count;
cout << "\n Total Overall price calculated =$ " << fixed << setw(8) << setprecision(2) << tot_price;
cout << "\n Total amount of standard vehicle commissions =$ " << fixed << setw(8) << tot_standard;
cout << "\n Total amount of hybrid vehicle commissions =$ " << fixed << setw(8) << tot_hybrid;
cout << "\n Total amount of electric vehicle commissions =$ " << fixed << setw(8) << tot_electric;
cout << "\n Total amount of all commissions paid out =$ " << fixed << setw(8) << tot_commission;
cout << "\n";
cout << "\n " << "Sales ID " << "Car type " << "Selling price " << "Commission ";
for (r_ay = 0; r_ay < tot_count; r_ay++)
{
cout << "\n " << fixed << id_num[r_ay] << " " << setprecision(2) << car[r_ay] << " " << setw(10) << price[r_ay] << " " << setw(10) << commission[r_ay];
}
if (tot_count > 0)
{
avg_comm = (tot_commission / tot_count);
comm_s = commission[0];
comm_l = commission[0];
for (r_ay = 1; r_ay < tot_count; r_ay++)
{
if (commission[r_ay] < comm_s)
comm_s = commission[r_ay];
if (commission[r_ay] > comm_l)
comm_l = commission[r_ay];
}
void print(double largeSmallAverage);
//{
// cout << "\n ";
// cout << "\n The smallest commission computed totals =$ " << fixed << setw(10) << comm_s;
// cout << "\n The largest commission computed totals =$ " << fixed << setw(10) << comm_l;
// cout << "\n Total average of commissions computed =$ " << fixed << setw(10) << avg_comm;
//}
}
cout << "\n";
change = 1;
compare = tot_count - 1;
do
{
change = 0;
for (r_ay = 0; r_ay < compare; r_ay++)
{
if (commission[r_ay] > commission[r_ay + 1])
{
temp_car = car[r_ay];
hold_id = id_num[r_ay];
hold_price = price[r_ay];
hold_comm = commission[r_ay];
commission[r_ay] = commission[r_ay + 1];
commission[r_ay + 1] = hold_comm;
id_num[r_ay] = id_num[r_ay + 1];
id_num[r_ay + 1] = hold_id;
car[r_ay] = car[r_ay + 1];
car[r_ay + 1] = temp_car;
price[r_ay] = price[r_ay + 1];
price[r_ay + 1] = hold_price;
change = 1;
}
}
compare--;
} while ((compare > 0) && (change == 1));
cout << "\n";
cout << "\n " << "Sales ID " << "Car type " << "Selling price " << "Commission ";
for (r_ay = 0; r_ay < tot_count; r_ay++)
{
cout << "\n " << fixed << id_num[r_ay] << " " << setprecision(2) << car[r_ay] << " " << setw(10) << price[r_ay] << " " << setw(10) << commission[r_ay];
cout << "\n ";
}
system("pause");
return 0;
}
}
void dashLine()
{
cout << "\n -----------------------------------";
}
int getSalesId()
{
int id_num;
cout << "\n Please enter Employee ID: ";
cin >> id_num;
while (id_num < 10000 || id_num > 99999)
{
cout << "\n Invalid Employee ID, Please enter a 5 digit ID ";
cin >> id_num;
}
return id_num;
}
int runProgram()
{
int rp_yesno;
cout << "\n Is there a customer? 1 = yes, 0 = no ";
cin >> rp_yesno;
while ((rp_yesno != 1) && (rp_yesno != 0))
{
cout << "\n Invalid Entry Please enter 1/0 ";
cout << "\n Is there a customer? 1 = yes 0 = no ";
cin >> rp_yesno;
}
return rp_yesno;
}
char vehicleType()
{
char car;
cout << "\n Please enter type of vehicle sold";
cout << "\n (S=standard, H=hybrid, E=electric): ";
cin >> car;
while (!((car == 'S') || (car == 's') || (car == 'h') || (car == 'H') || (car == 'E') || (car == 'e')))
{
cout << "\n Invalid input Please enter S/E/H ";
cin >> car;
}
return car;
}
double sellingPrice()
{
double price;
cout << "\n Please enter the selling price of the car:$ " << fixed << setprecision(2);
cin >> price;
while (price < 1)
{
cout << "\n Invalid entry, Please enter an amount greater than 0 ";
cin >> price;
}
return price;
}
void print(double largeSmallAverage)
{
double comm_s,
comm_l,
avg_comm;
{
cout << "\n ";
cout << "\n The smallest commission computed totals =$ " << fixed << setw(10) << comm_s;
cout << "\n The largest commission computed totals =$ " << fixed << setw(10) << comm_l;
cout << "\n Total average of commissions computed =$ " << fixed << setw(10) << avg_comm;
}
}
You would have a function called print that takes has three double parameters: void print(double small, double large, double average);.
Then later on you would call it with print(comm_s, comm_l, avg_comm);.
You need to change the definition of print:
void print(double small, double large, double average)
{
cout << "\n ";
cout << "\n The smallest commission computed totals =$ " << fixed << setw(10) << small;
cout << "\n The largest commission computed totals =$ " << fixed << setw(10) << large;
cout << "\n Total average of commissions computed =$ " << fixed << setw(10) << average;
}
Related
I am trying to create a C++ program that calculates sales tax for a customer and displays a receipt. For example, if you entered 10 as the first sale amount and the tax rate is 0.0825 it should display the total tax as $0.83. Why does my subtotal and total due at the end of the receipt display $10.82 when it should be $10.83?
//Customer Receipt
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
struct Item_Receipt
{
double item;
double cost;
double tax;
double subtotal;
};
int main()
{
vector <Item_Receipt> Store_Receipt;
Item_Receipt Purchase;
//Variables
const double item_tax = .0825;
double Item_Total = 0.0;
double Tax_Total = 0.0;
double Total_Sales = 0.0;
int numSales = 0;
cout << "First sales amount (Enter a 0 to stop): ";
cin >> Purchase.item;
Purchase.tax = Purchase.item * item_tax;
Purchase.subtotal = Purchase.item + Purchase.tax;
Store_Receipt.push_back(Purchase);
Item_Total += Purchase.item;
Tax_Total += Purchase.tax;
Total_Sales += Purchase.subtotal;
numSales++;
while (Purchase.item > 0.0)
{
cout << "Next sales amount (Enter a 0 to stop): ";
cin >> Purchase.item;
if(Purchase.item > 0.0)
{
Purchase.tax = Purchase.item * item_tax;
Purchase.subtotal = Purchase.item + Purchase.tax;
Store_Receipt.push_back(Purchase);
Item_Total += Purchase.item;
Tax_Total += Purchase.tax;
Total_Sales += Purchase.subtotal;
numSales++;
}
else
cout << endl << "That was the last item being puchased.\nHere is your itemized receipt." << endl << endl;
}
//end while
//Output
cout << "----------------------------------------- " << endl;
cout << "\tReceipt of Purchase" << endl;
cout << "----------------------------------------- " << endl << endl;
cout << fixed << setprecision(2);
cout << setw(10) << "Item Cost" <<
setw(15) << "Item Tax" <<
setw(15) << "Subtotal" << '\n';
cout << "----------------------------------------- " << endl;
for(int x=0;x<numSales;x++)
cout << setw(8) << Store_Receipt[x].item << setw(15) << Store_Receipt[x].tax <<
setw(15) << Store_Receipt[x].subtotal << endl;
cout << "----------------------------------------- " << endl;
cout << setw(10) << "Item Total" <<
setw(15) << "Tax Total" <<
setw(15) << "Total Due" << endl;
cout << setw(8) << Item_Total << setw(15) << Tax_Total <<
setw(15) << Total_Sales << endl;
cout << "----------------------------------------- " << endl;
cout << "\tYou purchased " << numSales << " items." << endl;
cout << "----------------------------------------- " << endl;
cout << "\tThank you! Have a nice day!" << endl;
cout << "----------------------------------------- " << endl;
cin >> numSales;
return 0;
}
setprecision(2) doesn't mean "round to 2 decimal digits," it means "display 2 decimal digits." The actual value is 10.825 but you're only displaying the first two decimal digits.
If you want to round away from the midpoint, you need to use one of the rounding functions on the result.
Since you want to round to the second decimal place, you have to first multiply the number by 100, then round it, then divide by 100. You could do this with the help of a function:
double round_to_cents(double v) {
return std::round(v * 100) / 100;
}
Then round the tax calculation:
Purchase.tax = round_to_cents(Purchase.item * item_tax);
(Demo)
I'm new here. Sorry if I'm not posting correctly. My program is comparing cell phone plans based on minutes. I want to know how I can compare plans to determine the best plan for the money. I believe I should be using minimum functions for this, but I'm honestly stuck. My code is below. Is it possible to find the minimum value from 3 separate functions? I'm not looking for an answer, but maybe an example or an article how to do so. Thanks!
#include<iostream>
using namespace std;
void companyA();
void companyB();
void CompanyC();
int min(); // I want to use this to find my minimum
int numEmployees;
int avgMin;
double totalCost;
// double bestChoice; Not used yet
int main()
{
double newMin;
cout << "Please enter number of employees." << endl;
cin >> numEmployees;
cout << "Please enter average minutes used by each employee. " << endl;
cin >> avgMin;
if (numEmployees < 0)
{
cout << "Incorrect value. Please enter positive number of employees." << endl;
cin >> numEmployees;
}
else if (avgMin < 0)
{
cout << "Incorrect value. Please enter positive number of minutes. " << endl;
cin >> avgMin;
}
cout << "\nStandard Packages" << endl;
cout << "Company A: For $29.99 per month, 450 minutes are included. Additional minutes are $0.35 per minute." << endl;
cout << "Company B: For $49.99 per month, 900 minutes are provided. Additional minutes are $0.30 per minute." << endl;
cout << "Company C: For $59.99 per month, unlimited minutes are provided." << endl;
companyA();
companyB();
companyC();
cout << "\nBased on the number of employees and average minutes used, " << x << "is the best choice." << endl;
system("pause");
return 0;
}
void companyA()
{
if (avgMin <= 450)
{
totalCost = 29.99*numEmployees;
cout << "\nCompany A will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 450)
{
totalCost = (avgMin-450)*0.35+29.99;
cout << "\nCompany A will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
}
void companyB()
{
if (avgMin <= 900)
{
totalCost = 49.99*numEmployees;
cout << "Company B will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 900)
{
totalCost = (avgMin - 900)*0.30 + 49.99;
cout << "Company B will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
}
void companyC()
{
totalCost = 59.99*numEmployees;
cout << "Company C will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
I don't know if this is what you're looking for.
void minimum(double numEmployees, double avgMin);
double minimumCompany(double x, double y, double z);
void minimum(double numEmployees, double avgMin)
{
double totalCostA, totalCostB, totalCostC, minimumTotal;
string choice;
if (avgMin <= 450)
{
totalCostA = 29.99 * numEmployees;
cout << "\nCompany A will cost an $" << totalCostA << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 450)
{
totalCostA = (avgMin - 450) * 0.35 + 29.99;
cout << "\nCompany A will cost an $" << totalCostA << " a month for " << numEmployees << " employee(s)." << endl;
}
if (avgMin <= 900)
{
totalCostB = 49.99*numEmployees;
cout << "Company B will cost an $" << totalCostB << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 900)
{
totalCostB = (avgMin - 900)*0.30 + 49.99;
cout << "Company B will cost an $" << totalCostB << " a month for " << numEmployees << " employee(s)." << endl;
}
totalCostC = 59.99*numEmployees;
cout << "Company C will cost an $" << totalCostC << " a month for " << numEmployees << " employee(s)." << endl;
minimumTotal = minimumCompany(totalCostA, totalCostB, totalCostC);
if (minimumTotal == totalCostA)
{
choice = "Company A";
}
else if (minimumTotal == totalCostB)
{
choice = "Company B";
}
else
{
choice = "Company C";
}
cout << "\nBased on the number of employees and average minutes used, " << choice << " is the best choice." << endl;
}
double minimumCompany(double x, double y, double z)
{
double minimum = x;
if (y < minimum ) {
minimum = y;
}
if (z < minimum) {
minimum = z;
}
return minimum;
}
int main()
{
double numEmps, averageMin;
bool isValid = true;
while (isValid)
{
cout << "Please enter number of employees." << endl;
cin >> numEmps;
cout << "Please enter average minutes used by each employee. " << endl;
cin >> averageMin;
if ((numEmps < 0))
{
cout << "Please enter number of employees. " << endl;
cin >> numEmps;
isValid = true;
}
else
{
isValid = false;
}
if ((averageMin < 0))
{
cout << "Please enter average minutes used by each employee. " << endl;
cin >> numEmps;
isValid = true;
}
else
{
isValid = false;
}
}
cout << "\nStandard Packages" << endl;
cout << "Company A: For $29.99 per month, 450 minutes are included. Additional minutes are $0.35 per minute." << endl;
cout << "Company B: For $49.99 per month, 900 minutes are provided. Additional minutes are $0.30 per minute." << endl;
cout << "Company C: For $59.99 per month, unlimited minutes are provided." << endl;
minimum(numEmps, averageMin);
system("pause");
return 0;
}
I've been playing around with some code in my down-time from my degree and I've nested a do{}while() loop inside another one but the problem I'm having is that the code keeps going until the last van is full, even after the number of parcels has been fulfilled...
The code's below. If someone could take a look at it and tell me what I've done wrong that'd be awesome. Bare in mind I've only really been coding in C++ for about a month so I've still got hella lot to learn..
#include <iostream>
using namespace std;
char cBeltFull;
int iVanCount, iParcelCount, iParcelLoaded;
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity;
char cExit = 'N';
int main() {
iVanCount = 1;
iParcelLoaded = 1;
fHeaviestWeight = 0;
fVanCapacity = 410;
do {
//Get the number of parcels to dispatch
cout << "How many parcels need sending?" << endl;
cin >> iParcelCount;
do {
fCurrentPayload = 0;
do {
//Get parcel weight
cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?";
cin >> fParcelWeight;
//'Load' the parcel
cout << endl << endl << "Parcel loaded";
iParcelLoaded ++;
//Update the payload
fCurrentPayload = fCurrentPayload + fParcelWeight;
} while ((fCurrentPayload + fParcelWeight)) < fVanCapacity)
//Dispatch the van
cout << endl << endl << "Van dispatched.";
//Update the van count
iVanCount ++;
if (fCurrentPayload > fHeaviestVanWeight) {
//Update the heaviest weight
fHeaviestVanWeight = fCurrentPayload;
}
} while (iParcelLoaded <= iParcelCount);
cout << endl << endl << endl << "Vans dispatched: " << iVanCout;
cout << endl << endl << "Weight of heaviest van: " << fHeaviestWeight;
cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl;
cin >> cExit;
} while (cExit == 'N');
}
Change this
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity);
to this
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity
&& iParcelLoaded < iParcelCount);
That way you will load as many items the user inputs. You code contains many syntax errors.
I corrected them for you, but please be more careful next time you post.
#include <iostream>
using namespace std;
char cBeltFull;
int iVanCount, iParcelCount, iParcelLoaded;
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity;
char cExit = 'N';
int main() {
iVanCount = 1;
iParcelLoaded = 1;
fHeaviestVanWeight = 0;
fVanCapacity = 410;
do {
//Get the number of parcels to dispatch
cout << "How many parcels need sending?" << endl;
cin >> iParcelCount;
do {
fCurrentPayload = 0;
do {
//Get parcel weight
cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?";
cin >> fParcelWeight;
//'Load' the parcel
cout << endl << endl << "Parcel loaded";
iParcelLoaded ++;
//Update the payload
fCurrentPayload = fCurrentPayload + fParcelWeight;
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity && iParcelLoaded < iParcelCount);
//Dispatch the van
cout << endl << endl << "Van dispatched.";
//Update the van count
iVanCount ++;
if (fCurrentPayload > fHeaviestVanWeight) {
//Update the heaviest weight
fHeaviestVanWeight = fCurrentPayload;
}
} while (iParcelLoaded <= iParcelCount);
cout << endl << endl << endl << "Vans dispatched: " << iVanCount;
cout << endl << endl << "Weight of heaviest van: " << fHeaviestVanWeight;
cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl;
cin >> cExit;
} while (cExit == 'N');
}
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! :)
I'm trying to left justify one side of a line and right justify the other side of the line, but im having trouble getting the right side to right justify. Could anyone catch my error?
//The following program will declare
#include <iostream>
#include <iomanip>
using namespace std;
const int HOUSING = 3500; //housing cost
const int MEALS = 2000; //meal cost
const int N_CRED_PER = 285; //out of state credit cost
const int CRED_PER = 130; //amount of money per credit(cost)
const int TAB = 30; //precision
const int ADD_FEES = 6200; //out of state fees for non-resident
int main()
{
cout << "Grant Mercer section 1002 Assignment 6\n\n";
int size, studentID, credits, i, valid,total;
//size of loop, studentID to hold ID of student, credits to hold num
//of credits the student will take, i to start loop
char first, last, res, housing, meal; //first and last to hold initials of
//student, res to hold code
//if student is resident, meal to hold
//code if student has meals
cin >> size; //grab length of loop in file
cout << left; //manipulate stream
cout << fixed << showpoint << setprecision(2);
valid = size; //initially all entries are valid, but de
//cremented each time something goes wrong
while (i < size)
{
cin >> studentID >> first >> last >> credits >> res; //read in mandatory data to vars
cout << setw(TAB) << left << "Student #: " << right << studentID << endl; //display id
cout << setw(TAB) << left << "Initials: " << right << first << last << endl;//display initials
if(credits > 0 && credits < 21) //if credits are fine
{
switch(res) //find char in res
{
case 'R': //if RESIDENT
case 'r':
cout << setw(TAB) << left << "Residency status: " << right << "Resident" << endl;
cout << setw(TAB) << left << "Credits: " << right << credits << endl;
cout << setw(TAB-10) << left << "Tuition: " << setw(10) << "$" << right << (double)CRED_PER * credits << endl;
cout << setw(TAB-10) << left << "Total due: " << setw(10) << "$" << right << (double)CRED_PER * credits << endl;
total += CRED_PER * credits; //add to total cost
break;
case 'N': //if NON-RESIDENT
case 'n':
cout << setw(TAB) << left << "Residency status: " << right << "Non-resident" << endl;
cin >> housing >> meal; //since the student is non-resident, they
//have a mean and house code
cout << setw(TAB) << left << "Credits: " << right << credits << endl;
cout << setw(TAB - 10) << left << "Tuition: " << setw(10) << "$" << right << (double)credits * N_CRED_PER + ADD_FEES << endl;
if(housing == 'H' || housing == 'h')
{
cout << setw(TAB - 10) << left << "Housing: " << setw(10) << "$" << right << (double)HOUSING << endl;
if(meal == 'M' || meal == 'm') //if MEALS
{
cout << setw(TAB-10) << left << "Meal Plan: " << setw(10) << "$" << right << (double) MEALS << endl;
cout << setw(TAB-10) << left << "Total due: " << setw(10) << "$" << right << (double) MEALS + HOUSING + (credits * N_CRED_PER) + ADD_FEES << endl;
total += MEALS + HOUSING + (credits * N_CRED_PER) + ADD_FEES;
//add to total
}
else //if NO MEALS
{
cout << setw(TAB-10) << left << "Total due: " << setw(10) << "$" << right << (double)\
HOUSING + (credits *N_CRED_PER) + ADD_FEES << endl;
total += HOUSING + (credits * N_CRED_PER) + ADD_FEES;
//add to total
total += HOUSING + (credits * N_CRED_PER) + ADD_FEES;
//add to total
}
}
else //if NO HOUSING NO MEALS
{
cout << setw(TAB-10) << left << "Total due: " << setw(10) << "$" << right << (double)(cr\
edits * CRED_PER) + ADD_FEES
<< endl;
total += (credits * CRED_PER) + ADD_FEES;
}
break;
}
}
else
{
cout << "INVLIAD CREDIT AMOUNT...\n";
valid--;
}
i++;
cout << endl << endl;
}
cout << "\nFINAL TOTALS\n"; //display final totals from students
cout << right << setw(TAB) << left << "Valid request count: " << right << valid << endl;
cout << setw(TAB) << left << "Invalid requrest count: " << right << size - valid << endl;
cout << setw(TAB - 10) << left << "Total feed due: " << setw(10) << "$" << right << (double)total;
cout << endl;
return 0;
}
the output is all left justified, i dont understand what i need to do to make this all right justified. Thanks!
You're forgetting to use setw() before printing each right aligned string with cout. For example, take the following line in your code:
cout << setw(TAB) << left << "Residency status: " << right << "Resident" << endl;
Before printing each string justified to the left or right, you must set a new width:
cout << setw(20) << left << "Residency status: " << setw(20) << right << "Resident" << endl;
Without a setw(), it assumes the width is zero, which basically ignores the right alignment.