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;
Related
I am working on a project for the office today and I’m running into a bit of a stumble today on it. My code is properly debugged but it when ran it ignores all the parameters I set up and becomes smashed all together in a one line executable (if that makes sense). Here’s the prompt and what I have so far:
“A business needs to calculate the bonus points for sales representatives. The bonus points are based on how much each sales representative sold the year.
Write a program to prompt the user to enter the sales amount for a sales representative. Include different functions for each group of Sales representatives.
Display the bonus points as integers.
Please see the table showing the Groups, Sales, and Bonus points:
Groups Sales Bonus points:
A $0 - $100, 000 500 points
$100, 001 - $1,000,000 1, 500 points
B $1,000,001 - $2,000, 000 2,000 points
$2,000,001 - $3,000, 000 2, 500 points
C $3,000,001 - $4,000, 000 3, 000 points
$4,000,001 and over 5, 000 points
Input Validation: Do not accept negative numbers for sales.”
And here’s what I have tried so far:
#include <iostream>
#include <iomanip>
using namespace std;
float salesamounts; //sales made
int username; // Sales rep name
int main()
{
cout << “Please enter your sales made for the year: $”
cin >> salesamounts;
cout << endl;
// exception on program
if (salesamounts <= -1);
{
if (salesamounts <= -1);
cout << “Value cannot be negative. Please input again.”
}
// Group A placement
for (;salesamount <= 100000;)
{
if ((salesamounts <= 100000)
cout << “Congratulations! You earned 500 Bonus Points!”
break;
{
for (; salesamounts > 100001 < 1000000;)
{
if ((salesamounts > 100001 < 1000000));
cout << “Congrats! 10000 Bonus Points have been credited to you!”
break;
}
According to your requirement, you could use if-elseif-else to judge each group of Sales representatives, which is like:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double salesamounts; //sales made
int username; // Sales rep name
cout << "Please enter your sales made for the year : $" << endl;
while (cin >> salesamounts) {
// exception on program
if (salesamounts < 0.0) {
cout << "Value cannot be negative.Please input again." << endl;
}
//judge salesamounts
else if (salesamounts>0.0 && salesamounts <= 100000.0) {
cout << "Congratulations!You earned 500 Bonus Points!" << endl;
}
else if (salesamounts > 100000.0 && salesamounts <= 1000000.0) {
cout << "Congrats!1500 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 1000000.0 && salesamounts <= 2000000.0) {
cout << "Congrats!2,000 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 2000000.0 && salesamounts <= 3000000.0) {
cout << "Congrats!2,500 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 3000000.0 && salesamounts <= 4000000.0) {
cout << "Congrats!3,000 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 4000000.0 ) {
cout << "Congrats!5,000 Bonus Points have been credited to you!" << endl;
}
else {
break;
}
}
}
By the way, you could also use switch statement to do this.
You did the input part almost correctly, but after that you confused the if-else-if part with for loops.
When taking the user input, you can use a while loop to check whether the value is negative or not. This will make the program to prompt the user to enter a value again if the entered value does not match the criteria:
float salesamounts;
cout<<"Enter the sales amounts for the year:"; //the program will prompt the user initially
cin>>salesamounts; //user inputs the value here
while(salesamounts<0) //this line checks if the value is negative, if yes, it'll continue running this block till the value is non-negative
{
cout<<"Negative numbers not allowed. Please enter again: "; //Suppose the value is negative. Then the program will display this line and prompt the user to enter again
cin>>salesamounts; //user enters the value again
}
The next step in your program is to then check the user input and make the program act accordingly. The if-else statement will do that perfectly:
if (salesamount<=100000) //checks the value. If this is true, then then the following line will run. Else, the program will move to the next check.
cout<<"Bonus point is 500"; //displays the output if the above condition is correct
else if (salesamount<=1000000) //second check
cout<<"Bonus point is 1500"; //displays if the second condition is true
.
.
.
else
cout<<"Bonus point is 5000";
and so on.
Just to clarify, a loop is used only when you want a particular block of code to run repeatedly, whereas, to check or compare two values, you use the if-else statement.
Note that I've not written the second check as if (salesamounts>=100001 && salesamounts<=1000000) and also, I gave the last check as only else, not else if.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
We are supposed to make a retail sales cashier but I just can't figure out the loops at all. We have only learned simple selection and repetition statements so far and I know that's all I need but I just can't seem to figure it out.
Project Overview
Starter Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double price = 0.0;
double subtotal = 0.0;
double salesTax = 0.0;
double totalSale = 0.0;
int anotherSale = 1;
// Loop for repeat sales
// Enter the first Product ID for the first sale (-1 to exit)
// Main loop for each sale
// Switch statement to determine the price, and calculate sales tax, if any, for the item.
// Get next Product ID
// Print properly formatted output for each sale
// Another sale?
// Display how much is in the cash drawer at the end
}
What I have so far:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double price = 0.0;
double subTotal = 0.0; // for receipt purposes
double salesTax = 0.0; // for receipt purposes
double totalSale = 0.0; // for receipt purposes
int anotherSale = 1;
double taxRate = 0.075; // default tax rate
// Loop for repeat sales
while ()
{
// Enter the first Product ID for the first sale (-1 to exit)
cout << "Enter the first Product ID: ";
cin >> productID;
// Main loop for each sale
while (productID > 0)
{
// Switch statement to determine the price, and calculate sales tax, if any, for the item.
switch (productID)
{
case 101:
price = 65.00;
taxRate = 0.075;
break;
case 102:
price = 12.50;
taxRate = 0;
break;
case 103:
price = 24.50;
taxRate = 0.00;
break;
case 104:
price = 38.75;
taxRate = 0.075;
break;
case 105:
price = 17.80;
taxRate = 0.075;
break;
case 106:
price = 16.50;
taxRate = 0;
break;
case 107:
price = 42.85;
taxRate = 0.075;
break;
case 108:
price = 32.99;
taxRate = 0.075;
break;
case 109:
price = 28.75;
taxRate = 0.075;
break;
case 110:
price = 51.55;
taxRate = 0;
break;
default:
cout << "INVALID PRODUCT ID: Product ID not found." << endl;
}
cout << "Enter the quantity: ";
cin >> quantity;
subTotal += price * quantity;
salesTax += price * quantity * taxRate;
totalSale = subTotal + salesTax;
// Get next Product ID
cout << "Enter the next Product ID: ";
cin >> productID;
}
// Print properly formatted output for each sale
// Another sale?
}
// Display how much is in the cash drawer at the end
}
Any help is appreciated, thank you guys in advance.
The basic aspect of a loop statement is to repeat the same set of instructions until a specified condition is met. You have successfully figured it out for the inner loop of your code where you are checking if productID>0. You need to do the same for the outer loop and impose a similar condition on anotherSale i.e. while(anotherSale!=0). Every time the inner loop finish, just ask the user for the value of anotherSale; if the user enters 0, the loop should break.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm posting because I'm having issues figuring out why my "total aid available" is not printing the total of the pell grant, stafford loan, and work-study loan. I've tried fixing my function again and again (I used sources online and in reference book, but I don't know if the issue is that my function won't be called or not, since nothing is printing for the total aid available.
Everything else is fine, except that one thing, and it is really bugging me since no matter what changes I make, I'm in the same state. No errors showing either. I'm using microsoft visual studio for the first time as compiler, so I wonder if that is the issue.
Here is what I have:
#include <iostream>
using namespace std;
double pell_Grant(double); // forward declaration on pell_Grant ( which is a function for calculating pell grant)
double sum(double, int const, double); // declaration for sum function which gives total for the total aid available
int main()
{
double workstudy = 0, pellgrant = 5730, grossincome = 0, Total = 0; // variables
int yes;
int const stafford = 9500; //const declaration
cout << "Lemme Forecast Your FAFSA" << endl;
cout << "Enter your adjusted gross income: " << endl; cin >> grossincome; // input for gross income
if (grossincome >= 30000) // if gross income is higher than 30,000 then print message
{
cout << "Sorry Charlie, your income is too high to run this forecaster!";
return 0;
}
cout << "Can someone claim you as a dependent? [1=yes/0=no]: " << endl; // input to claim dependent or not
cin >> yes;
if (yes == 1) // if 1 for yes is selected then pell grant gets reduced by 750, if 0 or no selected, then program goes by standard procedure
{
pellgrant -= 750;
}
workstudy = 1465; // work study must be nationwide avergae 1,465
if (grossincome >= 19000) // if this condition is met then work study is not met and message is printed as follows...
{
cout << "Your Work-Study Award is not available for your income level" << endl;
workstudy = 0;
}
double foo = pell_Grant(grossincome); // value returned from pell_Grant stored here to give total
Total = sum(workstudy + stafford + pellgrant); // sum function is called and stores result in Total
if (workstudy != 0) // if work study loan isn't more than 19,000 then it will be calculated and printed in next statement
{
cout << "Your Work-Study Award (if available)= " << workstudy << endl;
}
cout << "Your Stafford Loan award (if needed)= " << stafford << endl; // prints stafford loan (const called)
cout << "Your Pell Grant= " << pellgrant << endl; // prints pell grant
cout << "Total Aid Available For You=" << Total << endl; // prints total
return (0);
}
double pell_Grant(double x) // pell_Grant function that calculates pell grant which is assigned 5,730
{
// x is gross income which is assigned 5,730. This is money received that does not need to be repaid.
if ((x > 12000) && (x < 20000)) // statement makes sure adjusted gross is bettween 12000 & 20000
{
double a = x / 1000; // for every 1,000 in adjusted gross income... reduce/subtract 400 from it
a *= 400;
x -= a;
}
if (x > 20000) // check if gross income is more than 20000
{
double a = x / 1000; // for every 1,000 in gross income, subtract 500
a *= 500;
x -= a;
}
return x;
}
double sum(double workstudy , int const stafford, double pellgrant) // function for adding up workstudy loan, stafford loan, and pellgrant loan
{
double Total;
Total = workstudy + stafford + pellgrant;
return (Total); // returns total
}
According to its declaration, the method sum() accepts 3 parameters.
double sum(double, int const, double);
But while calling you are passing only 1 parameter:
Total = sum(workstudy + stafford + pellgrant);
Instead, you need to pass 3 parameters, like this:
Total = sum(workstudy, stafford, pellgrant);
But, I don't understand why you aren't getting any errors! You are trying to call a non-existent function. You must get a compiler error.
You are calling your sum() function incorrectly. This is your code:
Total = sum(workstudy + stafford + pellgrant); // sum function is called and stores result in Total
But your sum() function has three parameters. The correct form to call the function would be:
Total = sum(workstudy, stafford, pellgrant); // sum function is called and stores result in Total
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 years ago.
Improve this question
A mail order house sells five different products whose retail prices
are: product 1 — $2.98, product 2—$4.50, product 3—$9.98, product 4—$4.49 and product 5—
$6.87. Write a program that reads a series of pairs of numbers as follows:
a) product number
b) quantity sold
Your program should use a switch statement to determine the retail price for each product. Your
program should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.
I get tons of error when I try to compile my code and I was wondering what I did wrong.
#include <iostream>
using namespace std;
int main()
{
int numberOfProducts = 0;
int costOfProducts = 0;
int productTotal = 0;
double amountP1 = 0;
double amountP2 = 0;
double amountP3 = 0;
double amountP4 = 0;
double amountP5 = 0;
double product1 = 2.98;
double product2 = 4.50;
double product3 = 9.98;
double product4 = 4.49;
double product5 = 6.87;
cout<<"How many products do you want to buy? -1 to finish shopping"<<endl;
cin>>numberOfProducts;
while(numberOfProducts != -1)
cout<<"Which products do you want to buy? -1 to finish shopping"<<endl;
cin>>costOfProducts;
switch(costOfProducts)
{
case product1:
cout<<"Product 1($2.98) has been purchased";
productTotal = productTotal + 2.98;
amountP1 = amountP1 + 1;
break;
case product2:
cout<<"Product 2($4.50) has been purchased";
productTotal = productTotal + 4.50;
amountP2 = amountP2 + 1;
break;
case product3:
cout<<"Product 3($9.98) has been purchased";
productTotal = productTotal + 9.98;
amountP3 = amountP3 + 1;
break;
case product4:
cout<<"Product 4($4.49) has been purchased";
productTotal = productTotal + 4.49;
amountP4 = amountP4 + 1;
break;
case product5:
cout<<"Product 5($6.87) has been purchased";
productTotal = productTotal + 6.87;
amountP5 = amountP5 + 1;
break;
default:
cout<<"Sorry, please select a product"; << endl;
}
cout<<"The total amount of products bought are: " << numberOfProducts;
cout<<"The total amount of product 1's bought is: $" << amountP1 << endl;
cout<<"The total amount of product 2's bought is: $" << amountP2 << endl;
cout<<"The total amount of product 3's bought is: $" << amountP3 << endl;
cout<<"The total amount of product 4's bought is: $" << amountP4 << endl;
cout<<"The total amount of product 5's bought is: $" << amountP5 << endl;
cout<<"The total price of all your products are: $" << productTotal << endl;
return 0;
}
Remove the semicolons at the end of the following lines:
while(numberOfProducts != -1);
switch(costOfProducts);
In addition, the compiler doesn't understand the following symbols (and neither do I):
product1
product2
product3
product4
product5
BTW, I strongly recommend that you add a couple of cin>> there, if you actually want this to work...
You need to learn about statement blocks.
while(numberOfProducts != -1)
cout<<"Which products do you want to buy? -1 to finish shopping"<<endl;
cin>>costOfProducts;
I'll play a game with you. You need to insert { to begin a statement block and } to end one.
Your role is to figure out where they go.
Hint 1: The expression inside a while statement applies to the statement block.
Hint 2: One or more statements can be placed in a statement block.
If you wish to cheat, you can look up the syntax of the while statement, for multiple statements in the loop.
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.