Hello I am working on an assignment. I have spent numerous hours and cannot figure out why at the end of my program the "Total Commission" and "Total Due" are not adding up. I am a beginner so anything that could lead me in the right direction would be greatly appreciated. Thanks.
original code
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double PC_BASE_SALES = 6000.00, //PC is Personal Computers
PC_COMMISSION = 0.12,
PRINTER_BASE_SALES = 2500.00,
PRINTER_COMMISSION = 0.10,
ACCESSORIE_BASE_SALES = 2000.00,
ACCESSORIE_COMMISSION = 0.10,
MAINTENANCE_BASE_SALES = 1500.00,
MAINTENANCE_COMMISSION = 0.06;
double baseSalary = 0.00,
pcSales = 0.00,
printerSales = 0.00,
accessorieSales = 0.00,
maintenanceSales = 0.00,
totalCommission = 0.00,
totalDue = 0.00,
noCommission = 0.00,
pcCommission = baseSalary * PC_COMMISSION,
printerCommission = baseSalary * PRINTER_COMMISSION,
accessorieCommission = baseSalary * ACCESSORIE_COMMISSION,
maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
int idNumber; //salesman identification number
do
{
cout << "Please Enter Salesman Identification or -999 to Terminate ";
cin >> idNumber;
if (idNumber == -999)
return 0;
else
cout << "Please Enter Salesman Base Salary\t\t\t ";
cin >> baseSalary;
cout << "Please Enter Personal Computer Sales\t\t\t ";
cin >> pcSales;
cout << "Please Enter Printer Sales\t\t\t\t ";
cin >> printerSales;
cout << "Please Enter Accessories Sales\t\t\t\t ";
cin >> accessorieSales;
cout << "Please Enter Maintenance Sales\t\t\t\t ";
cin >> maintenanceSales;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << "\t\t\t My Computer Company\n ";
cout << endl;
cout << "\t\t\t Commission Statement\n ";
cout << endl;
cout << "\t\t\t Salesman Number " << idNumber << endl;
cout << endl;
cout << "\t\t ********************************\n";
cout << endl;
cout << fixed;
cout << setprecision(2);
cout << left << setw(20) << "Product";
cout << right << setw(20) << "Sales Amount";
cout << right << setw(20) << "Commission";
cout << "\n" << endl;
cout << left << setw(20) << "Personal Computers";
cout << right << setw(20) << pcSales;
while (pcSales > PC_BASE_SALES)
{
double pcCommission = baseSalary * PC_COMMISSION;
cout << right << setw(20) << pcCommission;
cout << "\n" << endl;
pcCommission += totalCommission;
break;
}
while (pcSales < PC_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
break;
}
cout << left << setw(20) << "Printers";
cout << right << setw(20) << printerSales;
while (printerSales > PRINTER_BASE_SALES)
{
double printerCommission = baseSalary * PRINTER_COMMISSION;
cout << right << setw(20) << printerCommission;
cout << "\n" << endl;
printerCommission += totalCommission;
break;
}
while (printerSales < PRINTER_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
break;
}
cout << left << setw(20) << "Accessories";
cout << right << setw(20) << accessorieSales;
while (accessorieSales > ACCESSORIE_BASE_SALES)
{
double accessorieCommission = baseSalary * ACCESSORIE_COMMISSION;
cout << right << setw(20) << accessorieCommission;
cout << "\n" << endl;
accessorieCommission += totalCommission;
break;
}
while (accessorieSales < ACCESSORIE_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
break;
}
cout << left << setw(20) << "Maintenance";
cout << right << setw(20) << maintenanceSales;
while (maintenanceSales > MAINTENANCE_BASE_SALES)
{
double maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
cout << right << setw(20) << maintenanceCommission;
cout << "\n" << endl;
maintenanceCommission += totalCommission;
break;
}
while (maintenanceSales < MAINTENANCE_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
cout << endl;
break;
}
cout << endl;
totalCommission = pcCommission + printerCommission + accessorieCommission +
maintenanceCommission;
totalDue = baseSalary + totalCommission;
cout << left << setw(20) << "Total Commission";
cout << right << setw(40) << totalCommission;
cout << "\n" << endl;
cout << left << setw(20) << "Base Pay";
cout << right << setw(40) << baseSalary;
cout << "\n" << endl;
cout << left << setw(20) << "Total Due";
cout << right << setw(40) << totalDue;
cout << "\n" << endl;
} while ( idNumber != -999);
system ("PAUSE");
return 0;
updated code
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double PC_BASE_SALES = 6000.00, //PC is Personal Computers
PC_COMMISSION = 0.12,
PRINTER_BASE_SALES = 2500.00,
PRINTER_COMMISSION = 0.10,
ACCESSORIE_BASE_SALES = 2000.00,
ACCESSORIE_COMMISSION = 0.10,
MAINTENANCE_BASE_SALES = 1500.00,
MAINTENANCE_COMMISSION = 0.06;
double baseSalary = 0.00,
pcSales = 0.00,
printerSales = 0.00,
accessorieSales = 0.00,
maintenanceSales = 0.00,
totalCommission = 0.00,
totalDue = 0.00,
noCommission = 0.00,
pcCommission = baseSalary * PC_COMMISSION,
printerCommission = baseSalary * PRINTER_COMMISSION,
accessorieCommission = baseSalary * ACCESSORIE_COMMISSION,
maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
int idNumber; //salesman identification number
do
{
cout << "Please Enter Salesman Identification or -999 to Terminate ";
cin >> idNumber;
if (idNumber == -999)
return 0;
else
cout << "Please Enter Salesman Base Salary\t\t\t ";
cin >> baseSalary;
cout << "Please Enter Personal Computer Sales\t\t\t ";
cin >> pcSales;
cout << "Please Enter Printer Sales\t\t\t\t ";
cin >> printerSales;
cout << "Please Enter Accessories Sales\t\t\t\t ";
cin >> accessorieSales;
cout << "Please Enter Maintenance Sales\t\t\t\t ";
cin >> maintenanceSales;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << "\t\t\t My Computer Company\n ";
cout << endl;
cout << "\t\t\t Commission Statement\n ";
cout << endl;
cout << "\t\t\t Salesman Number " << idNumber << endl;
cout << endl;
cout << "\t\t ********************************\n";
cout << endl;
cout << fixed;
cout << setprecision(2);
cout << left << setw(20) << "Product";
cout << right << setw(20) << "Sales Amount";
cout << right << setw(20) << "Commission";
cout << "\n" << endl;
cout << left << setw(20) << "Personal Computers";
cout << right << setw(20) << pcSales;
if (pcSales > PC_BASE_SALES)
{
double pcCommission = baseSalary * PC_COMMISSION;
cout << right << setw(20) << pcCommission;
cout << "\n" << endl;
totalCommission += pcCommission;
}
if (pcSales < PC_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
}
cout << left << setw(20) << "Printers";
cout << right << setw(20) << printerSales;
if (printerSales > PRINTER_BASE_SALES)
{
double printerCommission = baseSalary * PRINTER_COMMISSION;
cout << right << setw(20) << printerCommission;
cout << "\n" << endl;
totalCommission += printerCommission;
}
if (printerSales < PRINTER_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
}
cout << left << setw(20) << "Accessories";
cout << right << setw(20) << accessorieSales;
if (accessorieSales > ACCESSORIE_BASE_SALES)
{
double accessorieCommission = baseSalary * ACCESSORIE_COMMISSION;
cout << right << setw(20) << accessorieCommission;
cout << "\n" << endl;
totalCommission += accessorieCommission;
}
if (accessorieSales < ACCESSORIE_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
}
cout << left << setw(20) << "Maintenance";
cout << right << setw(20) << maintenanceSales;
if (maintenanceSales > MAINTENANCE_BASE_SALES)
{
double maintenanceCommission = baseSalary * MAINTENANCE_COMMISSION;
cout << right << setw(20) << maintenanceCommission;
cout << "\n" << endl;
totalCommission += maintenanceCommission;
}
if (maintenanceSales < MAINTENANCE_BASE_SALES)
{
cout << right << setw(20) << noCommission;
cout << "\n" << endl;
cout << endl;
}
cout << endl;
totalCommission = pcCommission + printerCommission + accessorieCommission + maintenanceCommission;
totalDue = baseSalary + totalCommission;
cout << left << setw(20) << "Total Commission";
cout << right << setw(40) << totalCommission;
cout << "\n" << endl;
cout << left << setw(20) << "Base Pay";
cout << right << setw(40) << baseSalary;
cout << "\n" << endl;
cout << left << setw(20) << "Total Due";
cout << right << setw(40) << totalDue;
cout << "\n" << endl;
} while ( idNumber != -999);
system ("PAUSE");
return 0;
}
Are you intentionally writing code for a obfuscated C++ competition?
This (as an example of several similar ones):
while (printerSales > PRINTER_BASE_SALES)
{
double printerCommission = baseSalary * PRINTER_COMMISSION;
cout << right << setw(20) << printerCommission;
cout << "\n" << endl;
printerCommission += totalCommission;
break;
}
is exactly the same as :
if (printerSales > PRINTER_BASE_SALES)
{
double printerCommission = baseSalary * PRINTER_COMMISSION;
cout << right << setw(20) << printerCommission;
cout << "\n" << endl;
printerCommission += totalCommission;
}
since the break is unconditional inside the loop, and thus makes the loop only ever run once. This is possibly what you wanted in the first place.
Also, since printerCommission is a local variable inside the loop, this will not do anything useful.
printerCommission += totalCommission;
did you mean:
totalCommission += printerCommission;
Also, as the comments say, using double or float to calculate money will eventually go wrong, because floating point values will sometimes round off incorrectly. For example, in binary floating point representation, the values 0.1, 0.2 and 0.6 and 0.7 would be 0.099999999999999, 0.199999999999999, 0.59999999999999 and 0.699999999999 respectively. It's the same as trying to express 1/3 in decimals, 0.333333333 - no matter how many digits are used, it can not be "exactly 1/3". If you do enough math using such values, it will end up "wrong". But you should get approximately the right value, give or take a cent or whatever the small part of the currency is called.
Here is an issue I found:
An else statement without { and } will only execute the next statement:
do
{
cout << "Please Enter Salesman Identification or -999 to Terminate ";
cin >> idNumber;
if (idNumber == -999)
return 0;
else // <--- This else will cause only the next line to be executed
// since there is no '{' following it.
cout << "Please Enter Salesman Base Salary\t\t\t ";
cin >> baseSalary;
Have you single stepped through the code with a debugger?
Related
I am having some troubles where my code is only displaying the first section I have inputed "Sphere" and is not letting me input any other values when I go to Debug.
The basis of the code is to have the user choose a selction, and be able to input values to find the surface areas and volumes of a shape. Below is my code. (Also I am new to this so if there are any pointers I would greatly appreciate it!)
This is for an exam I have so just some input as to where I am going wrong would be great. I want to learn it not have the whole thing completed for me
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main()
{
//Text
cout << "\nHello there! This program will help " << endl;
cout << "\ncalulate the surface areas and the " << endl;
cout << "\nvolumes of the displayed shapes below." << endl;
cout << endl;
cout << "----------------------------------------" << endl;
cout << " A: Sphere " << endl;
cout << " B: Cube " << endl;
cout << " C: Dodecahedron " << endl;
cout << " D: Cylinder " << endl;
cout << " E: Cone " << endl;
cout << "----------------------------------------" << endl;
cout << endl;
//Variables
double Sphere = 0.0,
Cube = 0.0,
Dodecahedron = 0.0,
Cylinder = 0.0,
Cone = 0.0;
float A = (Sphere),
B = (Cube),
C = (Dodecahedron),
D = (Cylinder),
E = (Cone);
//Question
enter code here
cout << "Please select one of the following..." << endl;
cin >> A;
cin >> B;
cin >> C;
cin >> D;
cin >> E;
{
//Sphere
int SphereRadius = 0.0;
double pi = 3.1415926535898;
double SphereSA = (4 * pi * (SphereRadius * SphereRadius));
double SphereV = ((int(4 / 3)) * (pi * (SphereRadius * SphereRadius * SphereRadius)));
if (Sphere);
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a radius. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> SphereRadius;
cout << endl;
cout << "\nWith the given radius... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << SphereSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << SphereRadius << endl;
}
{
//Cube
int CubeSide = 0.0;
double CubeSA = (6 * (CubeSide * CubeSide));
double CubeV = (CubeSide * CubeSide * CubeSide);
if (Cube);
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a side length. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the side length: " << endl;
cin >> CubeSide;
cout << endl;
cout << "\nWith the given side length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << CubeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << CubeV << endl;
cout << endl;
}
{
//Cone
int ConeH = 0.0;
double ConeR = 0.0;
double ConeSA = (pi * ConeR * (ConeR + sqrt((ConeH * ConeH) + (ConeR * ConeR))));
double ConeV = (pi * ((ConeR * ConeR) * (ConeH / 3)));
if (Cone);
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a radius and a height. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> ConeR;
cout << endl;
cout << "\nPlease input a positive number for the height: " << endl;
cin >> ConeH;
cout << endl;
cout << "\nWith the given side length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << ConeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << ConeV << endl;
cout << endl;
}
{
//Dodecahedron
int DodeEdge = 0.0;
double DodeSA = (3 * (sqrt(25 + 10 * sqrt(5) * DodeEdge)));
double DodeV = (DodeEdge * ((15 + (7 * sqrt(5)) / 4)));
if (Dodecahedron);
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need to have an edge. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the edge: " << endl;
cin >> DodeEdge;
string DodeEdge;
cout << "\nWith the given edge length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << DodeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << DodeV << endl;
cout << endl;
}
{
//Cylinder
int CylinderH = 0.0;
int CylinderR = 0.0;
double CylinderSA = ((2 * CylinderR * pi * CylinderH) + (2 * pi * (CylinderR * CylinderR)));
double CylinderV = (pi * (CylinderR * CylinderR) * CylinderH);
if (Cylinder);
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need to have a radius and a height. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> CylinderR;
cout << endl;
cout << "\nPlease input a positive number for the height: " << endl;
cin >> CylinderH;
cout << endl;
cout << "\nWith the given radius and height... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << CylinderSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << CylinderV << endl;
cout << endl;
}
return 0;
}
The error is that the code order is not correct:
int SphereRadius = 0.0;
double pi = 3.1415926535898;
double SphereSA = (4 * pi * (SphereRadius * SphereRadius));
double SphereV = ((int(4 / 3)) * (pi * (SphereRadius * SphereRadius * SphereRadius)));
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> SphereRadius;
I modified code by using switch-case:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#define Sphere 1
#define Cube 2
#define Dodecahedron 3
#define Cylinder 4
#define Cone 5
using namespace std;
double pi = 3.1415926535898;
int main()
{
//Text
cout << "\nHello there! This program will help " << endl;
cout << "\ncalulate the surface areas and the " << endl;
cout << "\nvolumes of the displayed shapes below." << endl;
cout << endl;
cout << "----------------------------------------" << endl;
cout << " 1: Sphere " << endl;
cout << " 2: Cube " << endl;
cout << " 3: Dodecahedron " << endl;
cout << " 4: Cylinder " << endl;
cout << " 5: Cone " << endl;
cout << "----------------------------------------" << endl;
cout << endl;
cout << "Please select one of the following..." << endl;
//Variables
int UserSelect = 0;
cin >> UserSelect;
while (UserSelect < 1 || UserSelect>5)
{
cout << "Please select the right option" << endl;
cin >> UserSelect;
cout << endl;
}
switch (UserSelect)
{
case Sphere:
{
//Sphere
int SphereRadius = 0.0;
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a radius. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> SphereRadius;
cout << endl;
double SphereSA = (4 * pi * (SphereRadius * SphereRadius));
double SphereV = ((int(4 / 3)) * (pi * (SphereRadius * SphereRadius * SphereRadius)));
cout << "\nWith the given radius... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << SphereSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << SphereRadius << endl;
}
break;
case Cube:
{
//Cube
int CubeSide = 0.0;
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a side length. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the side length: " << endl;
cin >> CubeSide;
cout << endl;
double CubeSA = (6 * (CubeSide * CubeSide));
double CubeV = (CubeSide * CubeSide * CubeSide);
cout << "\nWith the given side length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << CubeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << CubeV << endl;
cout << endl;
}
break;
case Dodecahedron:
{
//Dodecahedron
int DodeEdge = 0.0;
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need to have an edge. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the edge: " << endl;
cin >> DodeEdge;
double DodeSA = (3 * (sqrt(25 + 10 * sqrt(5) * DodeEdge)));
double DodeV = (DodeEdge * ((15 + (7 * sqrt(5)) / 4)));
//string DodeEdge;
cout << "\nWith the given edge length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << DodeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << DodeV << endl;
cout << endl;
}
break;
case Cylinder:
{
//Cylinder
int CylinderH = 0.0;
int CylinderR = 0.0;
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need to have a radius and a height. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> CylinderR;
cout << endl;
cout << "\nPlease input a positive number for the height: " << endl;
cin >> CylinderH;
cout << endl;
double CylinderSA = ((2 * CylinderR * pi * CylinderH) + (2 * pi * (CylinderR * CylinderR)));
double CylinderV = (pi * (CylinderR * CylinderR) * CylinderH);
cout << "\nWith the given radius and height... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << CylinderSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << CylinderV << endl;
cout << endl;
}
break;
case Cone:
{
//Cone
int ConeH = 0.0;
double ConeR = 0.0;
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a radius and a height. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> ConeR;
cout << endl;
cout << "\nPlease input a positive number for the height: " << endl;
cin >> ConeH;
cout << endl;
double ConeSA = (pi * ConeR * (ConeR + sqrt((ConeH * ConeH) + (ConeR * ConeR))));
double ConeV = (pi * ((ConeR * ConeR) * (ConeH / 3)));
cout << "\nWith the given side length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << ConeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << ConeV << endl;
cout << endl;
}
break;
default:
break;
}
return 0;
}
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;
}
}
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 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;
}
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;