So basically, I am trying to write a program that calculates the price per unit and percent off per unit of software sold. The program is supposed to calculate the percent off based on the amount of units inputted by the user, and then display it. My problem is that when I run my code, I get two outputs for the price per unit, and it screws up my total price. Do I have my if/else statements formatted correctly?
//This program calculates the price per unit and balance due for units of software.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
//set an integer for the amount of units sold
int unitsSold;
//set the double for the and the balance due
double balanceDue;
//set values for the price breaks on amount of units sold
constfloat tenToNineteen = 0.80;
constfloat twentyToFortynine = 0.70;
constfloat fiftyToNinetynine = 0.60;
constfloat oneHundredPlus = 0.50;
//set the value for the price per unit sold
float pricePer = 99.0;
float pricePerUnit;
//request amount of software units sold
cout << "How many software units sold? ";
cin >> unitsSold;
//if the amount sold that is inputted is negative, end the program.
if (unitsSold < 0)
cout << "Invalid response. Run program again. ";
else;
//display the amount of units sold
cout << "Software units sold: " << setw(39) << unitsSold << endl;
//display the price per unit based on the amount of units sold
if (unitsSold >= 0 && unitsSold <= 9)
{
cout << fixed << showpoint << setprecision(2);
cout << "Price per unit: " << setw(34) << "$" << setw(10) << pricePer << endl;
}
elseif (unitsSold >= 10 && unitsSold <= 19)
{
cout << fixed << showpoint << setprecision(2);
pricePerUnit = pricePer * tenToNineteen;
cout << "Price per unit: " << setw(34) << "$" << setw(10) << pricePerUnit << endl;
}
elseif (unitsSold >= 20 && unitsSold <= 49)
{
cout << fixed << showpoint << setprecision(2);
pricePerUnit = pricePer * twentyToFortynine;
cout << "Price per unit: " << setw(34) << "$" << setw(10) << pricePerUnit << endl;
}
elseif (unitsSold >= 50 && unitsSold <= 99)
{
cout << fixed << showpoint << setprecision(2);
pricePerUnit = pricePer * fiftyToNinetynine;
cout << "Price per unit: " << setw(34) << "$" << setw(10) << pricePerUnit << endl;
}
else ( unitsSold >= 100);
{
cout << fixed << showpoint << setprecision(2);
pricePerUnit = pricePer * oneHundredPlus;
cout << "Price per unit: " << setw(34) << "$" << setw(10) << pricePerUnit << endl;
}
//calculate and display the balance due
balanceDue = pricePerUnit * unitsSold;
cout << "Balance due: " << setw(37) << "$" << setw(10) << balanceDue;
return 0;
}
If this helps, here is my desired output vs my actual output:
DESIRED
How many software units sold? 5
Software Units Sold: 5
Price Per Unit: $ 99.00
Balance Due: $ 495.00
ACTUAL
How many software units sold? 5
Software units sold: 5
Price per unit: $ 99.00
Price per unit: $ 49.50
Balance due: $ 247.50