C++, Interest Calculator not displaying correct output - c++

The question asks "Write a program that reads an initial investment balance and an interest rate, then prints the number of years it takes for the investment to reach one million dollars."
The inputs I've been putting in are 100 for amount, and 3 for interest rate. But when i compile and run, the output is 29 which is incorrect as the amount is only 187 which isn't at all close to a million.
/*
Question: Write a program that reads an initial
investment balance and an interest rate, then
prints the number of years it takes for the
investment to reach one million dollars.
*/
#include <iostream>
using namespace std;
int main()
{
//Obtain user amount
double amount;
cout << "Please enter an initial investment balance ($0.00): $";
cin >> amount;
//Obtain user interest rate
double interest_rate;
cout << "Please enter an interest rate: ";
cin >> interest_rate;
//Convert interest rate to decimal
interest_rate = interest_rate / 100;
int time = 1;
//Calculate how many years
while (amount < 1000000)
{
amount = amount * (1 + (interest_rate * time));
++time;
}
//Display years
cout << "Years to reach one million: " << time;
return 0;
}
The output i am expecting is:
"Years to reach one million: 333300"
since 333300 is exactly one million.

In one year, the amount will grow to
amount * (1 + interest_rate)
and in two years, the amount grows to
amount * (1 + interest_rate) * (1 + interest_rate)
assuming annual compounding of your interest rate. Your inclusion of time, and the continuous multiplication by amount are errors.
Note that there is a closed form solution. For rate r, initial amount I, final amount A, the number of years t is
t = ln(A / I) / ln(1 + r)
which you need to round up.

Related

writing a code for time to double annual interest rate

I'm trying to write a code for annual interest rate that lets you enter any amount, and it will show you approximately how many years it takes for your money to at least double. The given interest rate is 5% yearly. Thing is, it's not working properly and it's displaying absurdly high numbers, like 200 years or so.
#include <iostream>
using namespace std;
int main() {
int deposit;
int counter;
cout << "Deposit an amount NO LESS than 1000." << endl;
cin >> deposit;
for (deposit ;; deposit = 1.05 * deposit) {
counter = counter+1;
if (deposit >= 2 * deposit) {
cout << endl;
cout << "Your money will double in "<< counter <<" years." << endl;
break;
}
}
}
Instead of using a loop, you could calculate the time taken to double the money directly.
The amount of money is not interesting, so you don't need to store the amount of money. It's only the rate of return that's interesting.
You can calculate it directly as log(2) / log(r) where r is the rate of return. For example log(2) / log(1.05) gives you the exact time to double an initial amount of money with a 5% return.
Include the standard <cmath> header to get std::log().
#include <iostream>
#include <cmath>
int main() {
double yearsToDouble = std::log(2) / std::log(1.05);
std::cout << "Your money will double in "<< yearsToDouble << " years." << std::endl;
}
Use a variable to store the initial deposit so that it can be compared to the cumulative amount with interest.
for (float initdeposit = deposit;; deposit = 1.05 * deposit)
{
counter = counter+1;
if (deposit >= 2 * initdeposit)
{
cout << endl;
cout << "Your money will double in "<< counter <<" years." << endl;
break;
}
}
a.exe
Deposit an amount NO LESS than 1000.
1000
Your money will double in 16 years.
Note: No matter what the amount is, the time taken to double will be the same always. :)
if (deposit >= 2 * deposit) {
cout << endl;
cout << "Your money will double in "<< counter <<" years." << endl;
break;
}
In the above if statement you are expecting deposit to be greater than or equal to 2 times of deposit. Which can only be true in case if the value of deposit is zero or less than zero.
I will suggest you to use a temp variable to keep the input value of deposit and proceed.
To add to the other answers, which are largely correct in pointing out that deposit > 2*deposit can never be true (you need a second variable to record the initial value!), the only reason your loop ends at all is because deposit gets so large that 2*deposit "wraps around" due to overflow.
This appears to make 2*deposit bigger than deposit (logically impossible — you need to fix this comparison!) although strictly speaking the results are undefined.
Apparently this happens to you after 200 or so iterations.
As for suggestions to switch to a floating-point type like double, this is tempting, and may be sufficient in this simple case, but as a general rule you should avoid floating-point when you don't need it as it introduces complexities and inaccuracies for very little gain.
I would recommend counting in integer pennies, or tenths of pennies, instead. You can achieve it by multiplying the input by 100 or 1000. The resulting incremental multiplication by 1.05 will have a rounding factor, then, but this is what the banks will be doing too!
This line
if (deposit >= 2 * deposit) {
Will not evaluate to true (unless deposit is negative or barring someedge case). You probably wanted to compare it to an initial value. So after this:
cin >> deposit;
I would put
double initialDeposit = deposit;
And then change the other line to
if (deposit >= 2 * initialDeposit) {

C++ Creating a parking charge

I am having to create a program that reads from the user what time they entered a parking garage and what time they left. The program uses this information to then figure out how much it costed the person to park there. For under 30 minutes, it's free. After 30 minutes and up to 2 hours, it is a $3 base charge, plus 5 cents every minute in excess of 30 minutes. Beyond 2 hours, it is an $8 base charge, plus 10 cents every minute in excess of 2 hours. I have so far got to convert the time the user inputted into all minutes. I am now stuck on what to do for the rest of my functions. I am new to programming, and the arguments in a function still confuse me. If you are able to help or provide any feedback, please tell in the comment section how the arguments were implemented to get the code to run correctly. Here is my code thus far:
#include <iostream>
using namespace std;
int elapsed_time(int entry_time, int exit_time)
{
int total = 0;
total = (exit_time / 100) * 60 + (exit_time % 100) - (entry_time / 100) * 60
+ (entry_time % 100);
return total;
} // returns elapsed time in total minutes
double parking_charge(int total_minutes) {
double total = 0;
double cents = 0;
if(total_mins < 30){
return 0;
}
else if (total_mins <= 120){
cents = (total_mins - 30) * 0.05;
return total = 3 + cents;
}
else{
cents = (total_mins - 120) * 0.10;
return total = 4.5 + 8 + cents;
}
} // returns parking charge
void print_results(total_minutes, double charge)
{
cout << "The charge of parking was: " << parking_charge(total_minutes)
}
int main() {
int entry_time = 0;
int exit_time = 0;
cout << "What was the entry time? (Enter in 24 hour time with no colon) ";
cin >> entry_time;
cout << "What was the exit time? (Enter in 24 hour time with no colon) ";
cin >> exit_time;
cout << print_results(total_minutes, charge);
}
I have updated my code with a working parking charge function. I am now aiming to get the print_results function working correctly and finding out how to get all of this to work together in the main function. Thanks to all for the help so far.
Your are almost done, You have need to call functions properly in main function. Declare two variables in main function totalTime and totalChargethen call the function
#include <iostream>
using namespace std;
int elapsed_time(int entry_time, int exit_time)
{
int total = 0;
total = (exit_time / 100) * 60 + (exit_time % 100) - (entry_time / 100) * 60
+ (entry_time % 100);
return total;
} // returns elapsed time in total minutes
double parking_charge(int total_minutes)
{
int charge;
if (total_minutes <= 30)
charge = 0;
else if (120 > total_minutes < 30)
charge = (3 + (0.05 * total_minutes));
else
charge = (8 + (0.10 * total_minutes));
} // returns parking charge
void print_results(double charge)
{
cout << "The charge of parking was: " << charge;
}
int main()
{
double charge,minutes;
int entry_time,exit_time;
cout << "What was the entry time? (Enter in 24 hour time with no colon) ";
cin >> entry_time;
cout << "What was the exit time? (Enter in 24 hour time with no colon) ";
cin >> exit_time;
minutes=elapsed_time(entry_time,exit_time);
charge=parking_charge(minutes);
print_results( charge);
}

Coin machine regarding decimals

Everything works as intended except one minor screwy line of code. The very last line that calculates the fee. For example if you type in 105 it says you entered $1.05, which is good, then it calculates the fee for the transaction which gives you $0.93555 as your take-home pay. I only want it to display up to the hundredths place no matter the dollar amount, not the hundred thousandth place. So it should display $0.93 because that's realistic. Note that depending on the integer you enter at the start, sometimes the decimal is placed correctly and sometimes the thousandth place is displayed, it's being screwy like that an I am not sure what to fix.
#include <iostream>
using namespace std;
int main() {
int cents;
double total;
cout<<"Enter total amount of coins (whole number): "; //Enter any whole number
cin>>total;
cents = total;
cout<<"You entered " << cents / 25 << " quarters";
cents = cents % 25;
cout<<", " << cents / 10 << " dimes";
cents = cents % 10;
cout<<", " << cents / 5 << " nickels";
cents = cents % 5;
cout<<", " << cents / 1 <<" pennies.";
cents = cents % 1;
cout<<" That is " << "$" <<total / 100 << "."<<endl; //Converting to dollar amount
cout<<"After the fee, you take home " << "$" << (total - (0.109 * total)) / 100 << "."; //What you're left with after the fee
You can use setprecision() if you include <iomanip> in the header and then use fixed to set how ever many digits you want to display after the decimal.
These pages explain it pretty well:
http://www.cplusplus.com/reference/ios/fixed/
http://www.cplusplus.com/reference/iomanip/setprecision/
In the last statement, the expression
(total - (0.109 * total)) / 100
should be:
(total - int(0.109 * total))/100
(In this case you can get away with not using <iomanip> or any other extras. Simply cast the product to int)
The bug:
The take home fee does not give only two decimals places. Example: Entering 105 coins give a take home fee of 0.93555.
Expected behavior:
The take home fee should be rounded down to two decimal place. (I assume "The Company" want more money. So they want round down the take home money. Every penny counts.)
Tracing the possible causes of the bug:
The take home fee is printed by the last line. So the last line may cause the bug.
The last line's formula ((total - (0.109 * total)) / 100) depends on the variable total.
Only the cin line (cin >> total;) and the definition of total (double total;) affects the variabletotal
These are all of the possible causes of the bug.
Simplified program that contains all possible causes of the bug:
#include <iostream>
using namespace std;
int main() {
double total;
// Enter any whole number
cout << "Enter total amount of coins (whole number): ";
cin >> total;
// What you're left with after the fee
cout << "After the fee, you take home " << "$"
<< (total - (0.109 * total)) / 100
<< ".";
}
Some ideas:
Idea 1: To round down a float number to integer, just cast it to integer. (e.g. cout << int(30.10) << "\n";)
Idea 2: Round down a float number to two decimal places is multiply the float number by 100, round down the result to integer, and then divide by 100.0.
If that sounds too tedious, can also use a library. see Rounding to 2 decimal points
A solution:
#include <iostream>
using namespace std;
int main() {
// Enter any whole number
cout << "Enter total amount of coins (whole number): ";
double total;
cin >> total;
// calculate the take home fee without rounding
double take_home_money = (total - 0.109 * total) / 100;
// Round down the take home fee to two decimal places
take_home_money = int(take_home_money * 100) / 100.0;
// What you're left with after the fee
cout << "After the fee, you take home $"
<< take_home_money << ".";
}
Also see the answer at C++ , A code to get an amount of money to convert into quarters, dimes , nickels, pennies

C++ passing variables in classes and getting logical errors

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;
}
...
};

using functions to input 1 int and 1 string array and looping

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.