I have been smashing my head against my keyboard for days and haven't gotten any help in any of my usual places. Someone please wave a wand and tell me what I'm doing wrong! When I run my code I get proper output for up to 19 rooms, once 20 is hit it's not outputting a discount. I have tried to rewrite the "if" statements several different ways and even went to a switch/break setup and it's the same issue every single time. I have a logical error and CANNOT find it!
The assignment is "The cost of renting a room at a hotel is, say $100.00 per night. For special occasions, such as a wedding or conference, the hotel offers a special discount as follows.
If the number of rooms booked is:
at least 10, the discount is 10%
at least 20, the discount is 20%
at least 30, the discount is 30%
Also if rooms are booked for at least three days, then there is an additional 5% discount.
Instructions
Write a program that prompts the user to enter:
The cost of renting one room
The number of rooms booked
The number of days the rooms are booked
The sales tax (as a percent).
The program outputs:
The cost of renting one room
The discount on each room as a percent
The number of rooms booked
The number of days the rooms are booked
The total cost of the rooms
The sales tax
The total billing amount.
My code is
#include<iostream>
#include<`iomanip`>
`using namespace std`;
`int main`()
{
const float discount_10=0.10;
const float discount_20=0.20;
const float discount_30=0.30;
const float additional_discount=0.05;
cout << fixed << showpoint;
cout << setprecision(2);
//Variables
double total_discount, final_bill, tax, base_cost, total_cost, room_cost, sales_tax;
int num_rooms, num_of_days;
cout<<"Enter the cost of renting one room: $";
cin>>base_cost;
cout<<"Enter the number of rooms booked: ";
cin>>num_rooms;
cout<<"Enter number of days the rooms are booked: ";
cin>>num_of_days;
cout<<"Enter sales tax(%): ";
cin>>sales_tax;
if(num_rooms>=30)
total_discount = discount_30;
else if(num_rooms>=20 && num_rooms<=29)
total_discount = discount_20;
else if(num_rooms>=10 && num_rooms<=19)
total_discount = discount_10;
else if (num_rooms >=9)
total_discount =0;
if(num_of_days>=3)
total_discount += additional_discount;
else if (num_of_days<3)
total_discount = 0;
room_cost = (base_cost * num_of_days * num_rooms);
//Total cost for all rooms
total_cost = room_cost - (room_cost * total_discount);
tax = total_cost * (sales_tax/100);
//Final bill
final_bill=total_cost+tax;
cout<<"Cost of one room:" << base_cost <<endl;
cout<<"Discount:" << total_discount *100<<"%"<<endl;
cout<< "Rooms booked:" << num_rooms <<endl;
cout<<"Days booked: " << num_of_days <<endl;
cout<<"Total Cost: $" << total_cost<<endl;
cout<<"Sales tax: "<<tax<< "%"<< endl;
cout<<"Total bill: $" << final_bill << endl;
return 0;
}
I've rewritten this thing about a dozen times and can't find it. I'm going nuts.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am learning and practicing with C++. I an error when I tried to run the program. I tried different things like changing the sign (<=,>=,<,>) but I don't think they are the problem. I was planning to create different classes for each range of bonus salary but I don't think it is needed to add different classes. I tried to combine 'bonus' and 'total' in one line of code but I need 'bonus' information to be displayed. The instructions are if your old salary is up to $14,999.99 raise 5%, $15,000.00 to $49,999.99 raise 7%, $50,000.00 to $99,999.99 raise 10% and $100,000.00 and higher raise 15%
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: -1.
This is the code
// Salary Calculator Program
// Intro C++, Lesson 6
// Written by Phong Dau, Jun 2022
#include <iostream>
using namespace std;
int main( )
{
//Declare variables
double oldSalary = 0.00;
double bonus = 0.00;
double total = 0.00;
//Prompt the user for inputs
cout << "Enter your old salary: ";
cin >> oldSalary;
//Decide salary bonus
if (oldSalary < 15000.00)
{
bonus = oldSalary * 0.05;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
}
else if (oldSalary >= 15000.00 < 50000.00)
{
bonus = oldSalary * 0.7;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
}
else if (oldSalary >= 50000.00 < 100000.00)
{
bonus = oldSalary * 0.1;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
}
else
{
bonus = oldSalary * 0.15;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
}
//end if
system("pause");
return 0;
} //end of main
When I enter few numbers to test the program, the output printed twice except for over $100,000.00. I only want it to be printed once.
Enter your old salary:10000
You will receive a raise of $500,for a new yearly salary of $10500
You will receive a raise of $500,for a new yearly salary of $10500
Press any key to continue . . .
Enter your old salary:20000
You will receive a raise of $1400,for a new yearly salary of $21400
You will receive a raise of $1400,for a new yearly salary of $21400
Press any key to continue . . .
Enter your old salary:80000
You will receive a raise of $8000,for a new yearly salary of $88000
You will receive a raise of $8000,for a new yearly salary of $88000
Press any key to continue . . .
Enter your old salary:150000
You will receive a raise of $22500,for a new yearly salary of $172500
Press any key to continue . . .
As identified already in the comments, you have a few issues. I'm going to spell them out for you as an answer.
The first issue is that your final else is only controlling a single line because it does not enclose the multiple statements in brackets.
else
bonus = oldSalary * 0.05;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
The above is equivalent to:
else {
bonus = oldSalary * 0.05;
}
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
To fix that, put those statements in a block:
else {
bonus = oldSalary * 0.05;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
}
The second issue is multiple issus actually. To start, this kind of thing is totally bogus:
if (oldSalary >= 15000.00 < 50000.00)
This does not do what you think. You probably wanted:
if (oldSalary >= 15000.00 && oldSalary < 50000.00)
But that's still incorrect because it's never reached due to your logic being backwards:
if (oldSalary > 14999.99) {
// All salaries above 14999.99
}
else if (oldSalary > 49999.99) {
// Not reachable
}
else if (oldSalary > 99999.99) {
// Not reachable
}
else
{
// All salaries less than or equal to 14999.99
}
One approach is to reverse the tests such that the things being tested by the "else" are logical possibilities.
if (oldSalary > 99999.99) {
// (99999.99, +inf) -> 15%
}
else if (oldSalary > 49999.99) {
// (49999.99, 99999.99] -> 10%
}
else if (oldSalary > 14999.99) {
// (14999.99, 49999.99] -> 7%
}
else
{
// (-inf, 14999.99] -> 5%
}
Also, in this instance you have a very minor boundary issue. You say that it's 5% for anything below 15000.00. However, 14999.99999 is below that but you wrongly assume 14999.99 is the highest possible value below 15000.
So instead of reversing the order of the statements, let's reverse the comparisons. While we're at it, check out in every possible scenario you are performing the same salary calculation and the same output. So those do not have to be repeated. The only thing different is the bonus calculation.
Let's put all that together and roll the bonus multiplier in there too... Look how simple (and readable) it becomes:
if (oldSalary < 15000.0)
bonus = 0.05;
else if (oldSalary < 50000.0)
bonus = 0.07;
else if (oldSalary < 100000.0)
bonus = 0.10;
else
bonus = 0.15;
bonus *= oldSalary;
total = oldSalary + bonus;
cout << "You will receive a raise of $" << bonus
<< ", for a new yearly salary of $" << total
<< endl;
I am working on an assignment in C++ but i am relatively new to the C++ programming language, however, I am getting errors in my output.
Question
The manager of the Crosswell Carpet Store has asked you to write a program to print customers’ bills. The manager has given you the following information:
The length and width of a room are expressed in terms of meters and centimeters. For example, the length might be reported as 16.7 meters.
The store does not sell fractions of a meter. Thus, the length and width must always be rounded up.
The carpet charge is equal to the number of square meters purchased times the carpet cost per square meter. Sales tax equal to 14% of the carpet cost must be added to the bill.
The labour cost is equal to the number of square meters purchased times R24.00, which is the labor cost per square meter. No tax is charged on labour.
Each customer is identified by a five-digit number, and that number should appear on the bill. Large-volume customers, identified by a customer number starting with a '0', may be given a discount. The discount applies to the cost before sales tax is added.
The sample output follows:
CROSWELL CARPET STORE STATEMENT
Customer name : xxxxx
Customer number : xxxxx
Carpet price : xx.xx
Labour : xx.xx
Subtotal : xx.xx
Less discount : xx.xx
Subtotal : xx.xx
Plus tax : xx.xx
Total : xx.xx
And my answer to this question is as follows:
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
int calculateCarpetSize (int length, int width)
{
int carpetSize;
carpetSize = ceil(length * width);
return carpetSize;
}
float calculateCarpetCost (int carpetSize , float sellingPrice)
{
float carpetCost;
carpetCost = carpetSize * sellingPrice;
return carpetCost;
}
float calculateLabourCost(int carpetSize)
{
float labourCost;
labourCost = carpetSize * 24.00;
return labourCost;
}
bool qualifyForDiscount(string customerNo)
{
string dis = "0";
if (customerNo.compare(0, dis.length(), dis) == 0)
{
return true;
}
else{
return false;
}
}
float computeDiscount ()
{
int discountPercentage;
float discount;
cout << "Enter the Percetage Discount: ";
cin >> discountPercentage ;
discount = discountPercentage / 100;
return discount;
}
void printCustomerStatement(string customerName, string customerNo, float carpetCost, float labourCost, float discount)
{
float subtotal = carpetCost + labourCost - discount;
float vat = subtotal*0.14;
cout << "\n CROSWELL CARPET STORE"<<endl;
cout << " STATEMENT"<<endl;
cout << "Customer name : "<<customerName<<endl;
cout << "Customer number : "<<customerNo <<'\n'<<endl;
cout << "Carpet price : "<<carpetCost<<endl;
cout << "Labour : "<<labourCost <<'\n'<<endl;
cout << "Subtotal : "<<carpetCost+labourCost<<endl;
cout << "Less discount : "<<discount <<'\n'<<endl;
cout << "Subtotal : "<<subtotal<<endl;
cout << "Plus Tax : "<<vat<<endl;
cout << "Total : "<<subtotal - vat<<endl;
}
int main()
{
string customerName;
string customerNo;
float carpetSize;
float sellingPrice;
float length;
float width;
float carpetCost;
float labourCost;
float discount;
cout <<"ENTER CUSTOMER NAME:";
cin >>customerName;
cout <<"ENTER CUSTOMER NUMBER:";
cin >>customerNo;
cout <<"ENTER ROOM WIDTH:";
cin >>width;
cout <<"ENTER ROOM LENGTH:";
cin >>length;
cout <<"ENTER SELLING PRICE:";
cin >>sellingPrice;
calculateCarpetSize(length, width);
calculateCarpetCost(carpetSize, sellingPrice);
calculateLabourCost(carpetSize);
if(qualifyForDiscount(customerNo))
{
computeDiscount();
}else
{
discount = 0.00;
}
printCustomerStatement(customerName, customerNo, carpetCost, labourCost, discount);
return 0;
}
I think the problem may be my function or my datatypes, or perhaps both.
EDIT
I get an output of this sort
ENTER CUSTOMER NAME:Quatban
ENTER CUSTOMER NUMBER:02234
ENTER ROOM WIDTH:20
ENTER ROOM LENGTH:20
ENTER SELLING PRICE:35
ENTER DISCOUNT PERCENTAGE: 2
CROSWELL CARPET STORE
STATEMENT
Customer name : Quatban
Customer number : 02234
Carpet price : 1.4e+004
Labour : 9.6e+003
Subtotal : 2.4e+004
Less discount : 0
Subtotal : 2.4e+004
Plus Tax : 3.3e+003
Total : 2e+004
First
Thus, the length and width must always be rounded up
Which means your function calculateCarpetSize should look more like :
int calculateCarpetSize (float length, float width)
{
int carpetSize;
carpetSize = ceil(length) * ceil(width);
return carpetSize;
}
Then
In your main :
calculateCarpetSize(length, width);
calculateCarpetCost(carpetSize, sellingPrice);
calculateLabourCost(carpetSize);
You should be storing the result of these functions like :
carpetSize = calculateCarpetSize(length, width);
carpetCost = calculateCarpetCost(carpetSize, sellingPrice);
labourCost = calculateLabourCost(carpetSize);
Edit
This actually gives you as output :
ENTER CUSTOMER NAME:Quatban
ENTER CUSTOMER NUMBER:02234
ENTER ROOM WIDTH:20
ENTER ROOM LENGTH:20
ENTER SELLING PRICE:35
Enter the Percetage Discount: 2
CROSWELL CARPET STORE
STATEMENT
Customer name : Quatban
Customer number : 02234
Carpet price : 14000
Labour : 9600
Subtotal : 23600
Less discount : 0
Subtotal : 23600
Plus Tax : 3304
Total : 20296
This looks like your (expected ?) result, even if I believe the last line should be subtotal + tax and not subtotal - tax but I may be wrong.
Here is the prompt I haven't gotten to all of it yet though:
Implement a class named GasPump that will be used to model a pump at a gas station.
A GasPump object should be able to perform the following tasks:
- Display the amount of gas dispensed
- Display the total amount charged for the amount of gas dispensed
- Set the cost per gallon on gas
- Display the cost per gallon of gas
- Reset the amount of gas dispensed and amount charged before each new usage
- Keep track of the amount of gas dispensed and the total charge
When implementing the GasPump class , you should assume that the gas pump dispenses
.10 gallons of gas per second. Write a test program in main() that prompts the user
to enter the cost per gallon of gas and how many seconds they want to pump gas for.
Then, display the number of gallons of gas pumped, the cost per gallon of gas, and
the total cost of the gas.
I am having problems calculating the amount paid and keep getting logical errors. As this code stands it will compile but it gives garbage for a calculation for amount charged.
#include <iostream>
#include <iomanip>
using namespace std;
class GasPump{
public:
void setCostPerGallon(double cpg){
costPerGallon = cpg;
}
double getCostPerGallon(){
return costPerGallon;
}
void setAmountDispensed(int seconds){
const double dispense = 0.10;
sec = seconds;
amountDispensed = dispense * sec;
}
int getAmountDispensed(){
return amountDispensed;
}
//here is the function I am having problems with, at least I think.
void setAmountCharged(double costPerGallon, double amountDispensed){
amountCharged = costPerGallon * amountDispensed;
}
double getAmountCharged(){
return amountCharged;
}
private:
double costPerGallon;
int sec;
double amountCharged, amountDispensed;
};
int main() {
double cpg = 0.0;
int seconds = 0;
GasPump pump;
cout << "Enter the cost per gallon of gas:";
cin >> cpg;
while(cpg <= 0.0) {
cout << "Enter a value greater than 0:";
cin >> cpg;
}
pump.setCostPerGallon(cpg);
cout << "Enter the amount of seconds you want to pump gas for:";
cin >> seconds;
while(seconds <= 0.0) {
cout << "Enter a value greater than 0:";
cin >> seconds;
}
pump.setAmountDispensed(seconds);
cout << "The gas pump dispensed " << pump.getAmountDispensed() << " gallons of gas." << endl
<< "At $" << pump.getCostPerGallon() << " per gallon, your total is $"
<< fixed << setprecision(2) << pump.getAmountCharged() << "." << endl;
return 0;
You never call pump.setAmountCharged(...), so the member variable amountCharged is whatever the compiler decided to initialize it to when you instantiated pump (typically 0);
To fix this, either get rid of the member variable amountCharged and do the calculation for the amount when getAmountCharged is called, or call setAmountCharged appropriately before calling getAmountCharged.
Here's the first solution:
class GasPump {
...
double getAmountCharged() {
return costPerGallon * amountDispensed;
}
...
};
i have a project for a cs class due late next week and ive got it almost done but i am having a few problems and ive been trying everything and cant get it to work. Our project consists of taking 3 months of 3 customers information and monthly utility charges from an input file, storing all of this in arrays, then calculating subtotals, tax, discount, and total paid then storing this into arrays, and then outputting 1 quarterly receipt for each customer. We have to do this using functions. my main problem is that it is only outputting the first customers receipt and ive double checked my for loop and to me it looks like it should work.
Thanks A lot
heres the output file
Austin City Office, Texas
RECEIPT #59, September28, 2013, 09:00PM
Customer ID: 127654
Name: Jack Jones
Address: 2059 Joe Lane, Austin TX, 78646
Phone Number: 512-520-5862
Electricity Charges: $6
Water Charges: $24
Gas Charges: $12
Subtotal: $42
Discount Amount: $0.84 (2% discount since your subtotal is less than $100)
Subtotal After the Discount: $41.16 (With 2% discount added)
Sales Tax Amount: $2.4696 (6% Tax since your subtotal after the discount is less than $100)
Total Amount Paid: $43.6296 (With 6% Sales Tax added)
.....................................................................................................
Refund Policy: 100% if an error is reported within 30 days from the date of payment. Only 75% refund
after 30 days.
Thank you for prompt payment.
heres my input file
Austin City Office, Texas
RECEIPT #59, September28, 2013, 09:00PM
Refund Policy: 100% if an error is reported within 30 days from the date of payment. Only 75% refund
after 30 days.
Thank you for prompt payment.
127654
Jack Jones
2059 Joe Lane, Austin TX, 78646
512-520-5862
2
8
4
2
8
4
2
8
4
Austin City Office, Texas
RECEIPT #59, September28, 2013, 09:00PM
Refund Policy: 100% if an error is reported within 30 days from the date of payment. Only 75% refund
after 30 days.
Thank you for prompt payment.
124325
Jack Williams
2788 Eagle Drive, Austin TX, 78646
512-623-7676
2
8
20
2
8
20
2
8
20
Austin City Office, Texas
RECEIPT #59, September28, 2013, 09:00PM
Refund Policy: 100% if an error is reported within 30 days from the date of payment. Only 75% refund
after 30 days.
Thank you for prompt payment.
125672
John Jones
3422 Hawk Drive, Austin TX, 78646
512-522-4564
2
8
40
2
8
40
2
8
40
and heres my code
#include<iostream>
#include<iomanip>
#include<string>
#include<string>
#include<fstream>
using namespace std;
//define varibles
int ncustomer1;
double discount, tax;
string dc, dsc, tc, dtpc;
//validation constant
const int MIN_N = 1, MAX_N = 3, MAX_TITLE=200, MAX_CINFO=200;
const float MIN_CHARGE=1.00, MAX_CHARGE= 1000.00;
// Array constant
const int MAX_NUMCUST=3, MAX_NUMMONTH=3, MAX_NUMCHARGE=3, MAX_NUMHEAD=6, MAX_NUMINFO=9, MAX_NUMTOTAL=8;
//customer info array
string NonNum[MAX_NUMCUST][MAX_NUMINFO];
//charges array
double Num[MAX_NUMCUST][MAX_NUMMONTH][MAX_NUMCHARGE] = {0};
//calculated array
double custTotals[MAX_NUMCUST][MAX_NUMTOTAL] = {0};
//functions
void input(string NonNum[MAX_NUMCUST][MAX_NUMINFO], double Num[MAX_NUMCUST][MAX_NUMMONTH][MAX_NUMCHARGE], int& count);
double subtotal(double custTotals[MAX_NUMCUST][MAX_NUMTOTAL], int&count);
double discount1(double custTotals[MAX_NUMCUST][MAX_NUMTOTAL], int&count);
double tax1(double custTotals[MAX_NUMCUST][MAX_NUMTOTAL], int&count);
void receipts(double custTotals[MAX_NUMCUST][MAX_NUMTOTAL], string NonNum[MAX_NUMCUST][MAX_NUMINFO], int&count);
int main()
{
//Ask number of customers from user
cout<< "Enter a number for amount of customers you would like to make a receipt for number should be between 1 and 3."<<endl;
cin >> ncustomer1;
//validate users entry
while(ncustomer1 > MAX_N || ncustomer1 < MIN_N )
{
cout << "Error: the number of customers must be between "<< MIN_N << " and "<< MAX_N <<endl;
cout<< "Re-Enter the number of customers"<<endl;
cin>> ncustomer1;
}
//output to screen when users entry is correct
cout<< "Ok, individual receipt(s) will be added to the output file for "<< ncustomer1<< " customer(s)."<<endl;
//customer for loop
for (int count = 0; count < ncustomer1; ++count)
{
input(NonNum, Num, count);
subtotal(custTotals, count);
discount1(custTotals, count);
tax1(custTotals, count);
receipts(custTotals, NonNum, count);
}
return 0;
}
//functions
void input(string NonNum[MAX_NUMCUST][MAX_NUMINFO], double Num[MAX_NUMCUST][MAX_NUMMONTH][MAX_NUMCHARGE], int& count)
{
//objects to help read input file
ifstream inputFile;
//open the input file
inputFile.open("Project5_a02418790_Input.txt");
//validation of input file
if(!inputFile)
{
cout<<"error opening input file.";
}
// For loop for non numeric data id, number...
for(int head = 0; head < 9; ++head)
{
//Get customer data as strings from input
getline(inputFile,NonNum[count][head]);
/* Validate inputed customer data
if(NonNum[count][head].length()>MAX_CINFO)
{
cout<<"customer "<<count<<"(customers are from 0-X, so customer 1=0) heading "<<head<<" String is too long"<<endl;
continue;
}
*/
}//end non numeric data for loop
//number of months For loop
for(int mnth = 0; mnth < 3; ++mnth)
{
//number of charges For loop
for(int charge = 0; charge < 3; ++charge)
{
//input charges
inputFile >> Num[count][mnth][charge];
//Running totals of the 3 charges
custTotals[count][charge] += Num[count][mnth][charge];
}//end of number of charges for loop
}//end of number of months foor loop
}
double subtotal(double custTotals[MAX_NUMCUST][MAX_NUMTOTAL], int&count)
{
// calculate the subtotal
//subtotal = Elec total + Water Total + Gas Total
custTotals[count][3]=custTotals[count][0]+custTotals[count][1]+custTotals[count][2];
}
double discount1(double custTotals[MAX_NUMCUST][MAX_NUMTOTAL], int&count)
{
//figure out the discount based on the subtotal
//if(subtotal=,<,> a number)
// discount%= x, comment= x;
if(custTotals[count][3]<100)
discount= .02, dc = "(2% discount since your subtotal is less than $100)", dsc="(With 2% discount added)";
if(custTotals[count][3]>=100&&custTotals[count][3]<250)
discount= .03, dc = "(3% discount since your subtotal is greater or equal to $100)", dsc="(With 3% discount added)";
if(custTotals[count][3]>=250&&custTotals[count][3]<500)
discount=.04, dc = "(4% discount since your subtotal is greater or equal to $250)", dsc="(With 4% discount added)";
if(custTotals[count][3]>=500)
discount=.05, dc = "(5% discount since subtotal is greater or equal to $500)", dsc="(With 5% discount added)";
//calculate the amount of discount
//discount amount = subtotal * discount %
custTotals[count][4] = custTotals[count][3]*discount;
//calculate the subtotal after the discount
//subtotal after dis= subtotal - discount
custTotals[count][5]=custTotals[count][3]-custTotals[count][4];
}
double tax1(double custTotals[MAX_NUMCUST][MAX_NUMTOTAL], int&count)
{
//figure out how sales tax percent and what captions
//if(subtotal after dis =,<,> a num)
// tax percent= x, comment= x;
if(custTotals[count][5]< 100 )
tax= .06, tc="(6% Tax since your subtotal after the discount is less than $100)", dtpc= "(With 6% Sales Tax added)";
if( custTotals[count][5] >=100&& custTotals[count][5] <250)
tax= .07, tc="(7% Tax since your subtotal after the discount is greater or equal to $100)", dtpc= "(With 7% Sales Tax added)";
if( custTotals[count][5] >=250&& custTotals[count][5] <500)
tax=.08, tc="(8% Tax since your subtotal after the discount is greater or equal to $250)", dtpc= "(With 8% Sales Tax added)";
if( custTotals[count][5] >=500)
tax=.09, tc="(9% Tax since your subtotal after the discount is greater or equal to $500)", dtpc= "(With 9% Sales Tax added)";
//calculate the sales tax amount
//amount of tax = subtotal after dis * tax percent
custTotals[count][6]= custTotals[count][5]*tax;
//calculate total amount paid
//total paid = subtotal after dis + amount of tax
custTotals[count][7]= custTotals[count][5] + custTotals[count][6];
}
void receipts(double custTotals[MAX_NUMCUST][MAX_NUMTOTAL], string NonNum[MAX_NUMCUST][MAX_NUMINFO], int&count)
{
//objects to help read output file
ofstream outputFile;
//open the output file
outputFile.open("Project5_a02418790_Output.txt");
//validation of output file
if(!outputFile)
{
cout<<"error opening output file.";
}
//OUTPUT ALL NEEDED INFO (STILL INSIDE CUSTOMER FOR LOOP)
//OUTPUT HEADER
outputFile<<setw(58)<<NonNum[count][0]<<endl<<setw(70)<<NonNum[count][1]<<endl;
//OUTPUT CUSTOMER INFO FOR LOOP
for(int z = 5; z < 9; ++z)
{
while(z == 5 )//Customer ID
{
outputFile<<endl<<"Customer ID: "<<NonNum[count][z]<< endl;
break;
}
while(z == 6 )//Name
{
outputFile<<"Name: "<<NonNum[count][z]<< endl;
break;
}
while(z == 7 )//Address
{
outputFile<<"Address: "<<NonNum[count][z]<< endl;
break;
}
while(z == 8 )//Phone Number
{
outputFile<<"Phone Number: "<<NonNum[count][z]<< endl;
break;
}
}//END OF OUTPUT CUSTOMER INFO FOR LOOP
//OUTPUT CHARGES AND TOTALS FOR LOOP
for(int y = 0; y < 8; ++y)
{
while(y == 0 )//Electricity Charges
{
outputFile<<endl<<"Electricity Charges: $"<<custTotals[count][y] << endl;
break;
}
while(y == 1 )//Water Charges
{
outputFile<<"Water Charges: $"<<custTotals[count][y] << endl;
break;
}
while(y == 2 )//Gas Charges
{
outputFile<<"Gas Charges: $"<<custTotals[count][y] << endl<<endl;
break;
}
while(y == 3 )//Subtotal
{
outputFile<<"Subtotal: $"<<custTotals[count][y] << endl;
break;
}
while(y == 4)//Discount Amount
{
outputFile<<"Discount Amount: $"<<custTotals[count][y]<<" "<<dc<< endl<<endl;
break;
}
while(y == 5 )//Subtotal After the Discount
{
outputFile<<"Subtotal After the Discount: $"<<custTotals[count][y]<<" "<<dsc<< endl<<endl;
break;
}
while(y == 6 )//Sales Tax Amount
{
outputFile<<"Sales Tax Amount: $"<<custTotals[count][y]<<" "<<tc<< endl<<endl;
break;
}
while(y == 7 )//Total Amount Paid
{
outputFile<<"Total Amount Paid: $"<<custTotals[count][y]<<" "<<dtpc<< endl<<endl;
break;
}
}//END OF OUTPUT CHARGES AND TOTALS FOR LOOP
//OUTPUT FOOTER BREAK
outputFile<<"....................................................................................................."<<endl;
//OUTPUT REFUND FOR LOOP
for(int w = 2; w < 4; ++w)
{
outputFile<< NonNum[count][w]<<endl;
}
//OUTPUT THANKYOU
outputFile<<setw(58)<<NonNum[count][4]<<endl;
//OUTPUT NEW LINE AND DIVIDER FOR NEW CUSTOMER
outputFile<<endl<<"_____________________________________________________________________________________________________"<<endl<<endl;
}
Are you sure it writes the first one each time? It looks to me that it would write the last one because you write over the file each call to receipt instead of appending to it.
The key reason to getting one set of outputs IMHO is this line
outputFile.open("Project5_a02418790_Output.txt");
Because you call it every call to receipts you end up overwriting the file each time and get just one set of outputs.
There are a lot of other things that are very bad coding practice.
Issue1: Use of arrays where you should use Structures / or classes:
For instance: You keep your customer information in arrays of arrays of strings.
std::string NonNum[MAX_NUMCUST][MAX_NUMINFO]
Then NonNum[1][5] is the ID of customerID of customer 1.. you should build a struct it will make your code much easier to understand. Name it properly NonNum tells us it contains strings which we know from glancing at the code, it doesn't tell us the important info of what kind of information is stores (Customer Id / Address etc..)
Also look at this:
for(int z = 5; z < 9; ++z) {
while(z == 5 )//Customer ID {
outputFile<<endl<<"Customer ID: "<<NonNum[count][z]<< endl;
break;
while(z==6) { ...
}
What is the point of the inner while? Why do you need to for loop? Wouldn't it be much easier to write:
outputFile<< "Customer ID:" << CustomerInfo[i].CustomerID << endl;
Issue2: Dependencies between functions
subtotal fills custTotals[i][3] and is dependent on custTotals[i][0], custTotals[i][1] & custTotals[i][2]
discount1 fills custTotals[i][4] based on the previous ones.. Again you are using an array where you should use a structure. But also it will make life much easier for you if the inputs and outputs to a function are clear and easy to understand.
Currently any change in the order of the functions will mess up the logic but it is impossible to see that from the code.
If you had a separate subTotals array that you would pass to subTotals so it would write to and then pass into Discount1 so it would read from everything becomes clearer.
Issue 3: you pass a int& count to all these functions although you do not plan to change it. I expected the error to be that you wrote to count in one of the inner function and didn't realize it. If you pass a non const ref to an object it indicates that this is a output of the function not an input.
I trying to write a simple program for school and I have a bit of problem. I have to enter twice for it work right.
for example i have input 3 two time for it to give me 90..
What can i do to fix it.
Does the coding look right at the moment
#include <iostream>
#include <string>
using namespace std;
int main(){
string Cartype, rateoption;
double total, miles, days;
const double CdailyRate=30;
const double PdailyRate=40;
const double FdailyRate=50;
const double CmileRate=0.25;
const double PmileRate=0.35;
const double FmileRate=0.45;
cout<<"Thank you for choosing Car Rite Rental for your rental needs!\n"
<<"\a Before we get started calculating your total owed please remember\n"
<<"that here at Car Rite Rental we havea MINIMUM PAYMENT OF $30.\n\n"
<<"Please enter the type of car you have rented: \n\n"
<<"[please enter corresponding letter] \n"
<<"C-Chevrolet\n"<<"P-Pontiac\n"<<"F-Ford\n";
cin>>Cartype;
cout<<"Please choose your payment option from the following: \n\n"
<<"[please enter corresponding number] \n"
<<"D-Daily Rate\n"<<"M-Mileage Rate\n";
cin>>rateoption;
if(rateoption=="D"||rateoption=="d"){
cout<<"Please enter the number of days you have rented this vehicle: \n";
cin>>days;
}
else
cout<<"Please enter the number of miles traveled in your rental car:\n";
cin>>miles;
if (Cartype=="C"||Cartype=="c" && rateoption=="D"||rateoption=="d"){
total=CdailyRate*days;
cout<<"Your total owed today is: $"<<total<<"\nThank you again for choosing Car Rite Rental!\n";
}
return 0;
}
Because you have a missing set of braces.
else
cout<<"Please enter the number of miles traveled in your rental car:\n";
cin>>miles;
Although it's indented, the "cin>>miles;" statement is always executed, it is not conditional on the else.
else {
cout<<"Please enter the number of miles traveled in your rental car:\n";
cin>>miles;
}
will fix it.