having some problems with a class exercise
#include <iostream>
#include <string>
using namespace std;
int main()
{
int employeeNumber, grossPay, stateTax, federalTax, ficaHold;
int totalGross, totalState, totalFederal, totalFica, totalPay = 0;
do // do-while loop for employee number
{
cout << "Enter the employee number: " << endl;
cout << "(Enter 0 to quit.)" << endl;
cin >> employeeNumber;
} while (employeeNumber < 0);
while (employeeNumber != 0)
{
do // gross pay greater than state tax + federal tax + FICA
{
do // entry and validation for gross pay
{
cout << "How much gross pay ? ";
cin >> grossPay;
} while (grossPay < 0);
do //entry and validation for state tax
{
cout << "How much state tax ? ";
cin >> stateTax;
} while (stateTax < 0 || stateTax > grossPay);
do // entry and validation federal tax
{
cout << "How much federal tax ? ";
cin >> federalTax;
} while (federalTax < 0 || federalTax > grossPay);
do // entry and validation for FICA holdings amount
{
cout << "How much FICA withholdings ? ";
cin >> ficaHold;
} while (ficaHold < 0 || ficaHold > grossPay);
if (grossPay < stateTax + federalTax + ficaHold) // Message to verify taxes are not greater than pay
cout << "State tax, federal tax, and FICA holdings cannot be greater than gross pay. Please re-enter these values." << endl;
} while (grossPay < stateTax + federalTax + ficaHold);
totalGross += grossPay;
totalState += stateTax;
totalFederal += federalTax;
totalFica += ficaHold;
totalPay = totalGross - (totalState + totalFederal + totalFica);
do // do-while loop for employee number
{
cout << "Enter the employee number: " << endl;
cout << "(Enter 0 to quit.)" << endl;
cin >> employeeNumber;
} while (employeeNumber < 0);
}
cout << endl << endl << "The total gross pay is: $" << totalGross << endl;
cout << "The total state tax is :" << totalState << endl;
cout << "The total federal tax :" << totalFederal << endl;
cout << "The total FICA withholdings :" << totalFica << endl;
cout << "Net pay :" << totalPay << endl << endl;
return 0;
}
getting some errors for the variables on lines 95-98, eg "uninitialized local variable 'totalState'" and not really sure what to do, i already gave those variables value in the declaration before any loops, and im not sure if i can move them before anything while keeping the goal of the program
The first time you use totalState, you're both reading and writing: totalState += stateTax;. The problem is that you've never initialized totalState, so there's no guarantee what the value will be when you try to read it.
For the record, you have other uninitialized variables as well: totalGross, totalFederal, and totalFica.
Related
I have already checked all the other questions but I just can't fix it.. I am a nuub at coding.
I don't know why it says it needs a while or where to put it, and it gives the wrong answer for the LOCS function also is there anything i can do about the default pointer warning. this is just a start i will be extending this later so it would be a big help and i have tried while and closing brackets everywhere lol
Btw if anyone can tell me how I can use the input as decision as you can see I am using 1 and 2, but If I could use permanent and casual that would be great.
// Calculate an employee's weekly salary
// Use a do while loop, and an if else statement to have user input data and display the correct values
#include <iostream>
using namespace std;
void main()
{
//Declaring the constant variables
const double BONUS_RATE = 5.0;
//Declaring the variables
int hours;
int sales;
int Status;
string permanent, casual, Name, status, result, employee;
double rate, sale_bonus, netPay, gross;
//set decimal point to 2 positions
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//Display name
cout << "Calculate an employee's weekly salary\n\n";
//Do while loop to get hourly rate
do {
cout << "Enter employee name: ";
cin >> Name;
cout << "Please enter 1 if employee is permanent or 2 if casual: ";
cin >> Status;
Status = 0;
while (Status < 1 || Status > 2);
if (Status = 1)
{
cout << "Permanent employees have a fixed salary of $1000 per week" << endl;
sales = 0;
(sales < 1 || sales > 10);
cout << "If any please enter how many sales employee made this week:";
cin >> sales;
sale_bonus = sales * BONUS_RATE;
netPay = 1000 + sale_bonus;
cout << endl;
cout
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
else if (Status = 2) {
cout << "Casual employee's hourly rate is $15";
rate = 15.00;
cout << endl;
//while loop for hours
hours = 0;
while (hours < 1 || hours > 60)
{
cout << "Please enter how many hours you have worked this week:" << endl;
cout << "Minimum hours is 1" << endl;
cout << "Maximum hours are 60" << endl;
cout << "Enter hours worked: ";
cin >> hours;
}
//while loop for bonus
sales = 0;
while (sales < 1 || sales > 10)
{
cout << "Please enter how many sales you made this week:";
cin >> sales;
}
//Calculate pay
gross = hours * rate;
sale_bonus = sales * BONUS_RATE;
netPay = gross + sale_bonus;
//Display the results
cout << endl
<< "Hourly Rate: \t" << rate << endl
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
}
}
#include <iostream>
using namespace std;
void main()
{
//Declaring the constant variables
const double BONUS_RATE = 5.0;
//Declaring the variables
int hours;
int sales;
int Status;
string permanent, casual, Name, status, result, employee;
double rate, sale_bonus, netPay, gross;
//set decimal point to 2 positions
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//Display name
cout << "Calculate an employee's weekly salary\n\n";
//Do while loop to get hourly rate
do {
cout << "Enter employee name: ";
cin >> Name;
cout << "Please enter 1 if employee is permanent or 2 if casual: ";
cin >> Status;
} while (Status < 1 || Status > 2); // add while after do block with }
if (Status == 1)// use '==' equality check not '=' .
{
cout << "Permanent employees have a fixed salary of $1000 per week" << endl;
sales = 0;
(sales < 1 || sales > 10);
cout << "If any please enter how many sales employee made this week:";
cin >> sales;
sale_bonus = sales * BONUS_RATE;
netPay = 1000 + sale_bonus;
cout << endl;
cout
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
else if (Status == 2) { // use '==' equality check not '=' .
cout << "Casual employee's hourly rate is $15";
rate = 15.00;
cout << endl;
//while loop for hours
hours = 0;
while (hours < 1 || hours > 60)
{
cout << "Please enter how many hours you have worked this week:" << endl;
cout << "Minimum hours is 1" << endl;
cout << "Maximum hours are 60" << endl;
cout << "Enter hours worked: ";
cin >> hours;
}
//while loop for bonus
sales = 0;
while (sales < 1 || sales > 10)
{
cout << "Please enter how many sales you made this week:";
cin >> sales;
}
//Calculate pay
gross = hours * rate;
sale_bonus = sales * BONUS_RATE;
netPay = gross + sale_bonus;
//Display the results
cout << endl
<< "Hourly Rate: \t" << rate << endl
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
}
I have made the suitable changes :
Use == to check equality instead of = operator!
Syntax of do while : do { //code... } while(condition).
The while must follow the do after closing the block!
Your Mistake
the main mistake is you have not close the do while loop, ask in comment for more clarification!
PS. I recommend you first copy my code and then run, and then analyse the issue!
Use this code this will work
#include
using namespace std;
void main()
{
//Declaring the constant variables
const double BONUS_RATE = 5.0;
//Declaring the variables
int hours;
int sales;
int Status;
string permanent, casual, Name, status, result, employee;
double rate, sale_bonus, netPay, gross;
//set decimal point to 2 positions
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//Display name
cout << "Calculate an employee's weekly salary\n\n";
//Do while loop to get hourly rate
while(1){
cout << "Enter employee name: ";
cin >> Name;
cout << "Please enter 1 if employee is permanent or 2 if casual: ";
cin >> Status;
Status = 0;
while (Status < 1 || Status > 2);
if (Status = 1)
{
cout << "Permanent employees have a fixed salary of $1000 per week" << endl;
sales = 0;
(sales < 1 || sales > 10);
cout << "If any please enter how many sales employee made this week:";
cin >> sales;
sale_bonus = sales * BONUS_RATE;
netPay = 1000 + sale_bonus;
cout << endl;
cout
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
else if (Status = 2) {
cout << "Casual employee's hourly rate is $15";
rate = 15.00;
cout << endl;
//while loop for hours
hours = 0;
while (hours < 1 || hours > 60)
{
cout << "Please enter how many hours you have worked this week:" << endl;
cout << "Minimum hours is 1" << endl;
cout << "Maximum hours are 60" << endl;
cout << "Enter hours worked: ";
cin >> hours;
}
//while loop for bonus
sales = 0;
while (sales < 1 || sales > 10)
{
cout << "Please enter how many sales you made this week:";
cin >> sales;
}
//Calculate pay
gross = hours * rate;
sale_bonus = sales * BONUS_RATE;
netPay = gross + sale_bonus;
//Display the results
cout << endl
<< "Hourly Rate: \t" << rate << endl
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
}
}
I am currently working on a homework problem that asks me to Write a program that displays a weekly payroll report.A loop in the program should ask the user :
employee number
gross pay
state tax
federal tax,
FICA withholdings. I am getting the correct output, however my professors grader keeps telling giving me the error statement:
Format Ok. Build Ok. Link Ok. Run Timed out. Runtime 9020 milliseconds. Your program is waiting for input or in an endless loop.
I am having some trouble finding this loop and would appreciate if anyone could point it out to me.
I have tried changing the main while(employeeNumber !=0) loop to include a do/while statement that repeats if (totalWithholdings > grossPay)
int main()
{
int employeeNumber;
double grossPay = 0.0,
grossPayTotal = 0.0,
stateTax = 0.0,
stateTaxTotal = 0.0,
federalTax = 0.0,
federalTaxTotal = 0.0,
ficaWithholdings = 0.0,
ficaWithholdingsTotal = 0.0,
netPay = 0.0,
netPayTotal = 0.0,
totalWithHoldings = 0.0;
cout << "Enter the following information:\n" << endl;
cout << "Employee Number(0 to quit) :\n";
cin >> employeeNumber;
while (employeeNumber < 0)
{
cout << "Employee number may not be less than zero.\n";
cout << "Re-enter employee Number (0 to quit): ";
cin >> employeeNumber;
}
while (employeeNumber != 0)
{
cout << "Gross pay :";
cin >> grossPay;
while (grossPay < 0)
{
cout << "Gross pay may not be less than zero.\n";
cout << "Re-enter Gross pay: ";
cin >> grossPay;
}
cout << "Federal Withholding :";
cin >> federalTax;
while (federalTax < 0)
{
cout << "Federal witholding may not be less than zero.\n";
cout << "Re-enter Federal Withholding: ";
cin >> federalTax;
}
cout << "\nState Withholding :";
cin >> stateTax;
while (stateTax < 0)
{
cout << "\nState witholding may not be less than zero.\n";
cout << "\nRe-enter State Withholding: ";
cin >> stateTax;
}
cout << "FICA Withholding : ";
cin >> ficaWithholdings;
while (ficaWithholdings < 0)
{
cout << "FICA witholding may not be less than zero.\n";
cout << "Re-enter FICA Withholding: ";
cin >> ficaWithholdings;
}
totalWithHoldings = (federalTax + stateTax + ficaWithholdings);
if (totalWithHoldings > grossPay)
{
cout << "\nERROR: Withholdings cannot exceed gross pay.\n"
<< "\nPlease re-enter the data for this employee.\n";
cin >> employeeNumber;
}
else
{
cout << "Processing the Next employee:\n"
<< "Employee Number(0 to quit) :\n";
cin >> employeeNumber;
}
grossPayTotal = grossPayTotal + grossPay;
federalTaxTotal = federalTaxTotal + federalTax;
stateTaxTotal = stateTaxTotal + stateTax;
ficaWithholdingsTotal = ficaWithholdingsTotal + ficaWithholdings;
netPay = grossPay - totalWithHoldings;
netPayTotal = netPayTotal + netPay;
}
cout << "Total Gross Pay : $" << setw(4) << setprecision(2)
<< fixed << grossPayTotal << endl;
cout << "Total Federal Tax : $" << setw(4) << setprecision(2)
<< fixed << federalTaxTotal << endl;
cout << "Total State Tax : $" << setw(4) << setprecision(2)
<< fixed << stateTaxTotal << endl;
cout << "Total FICA : $" << setw(4) << setprecision(2)
<< fixed << ficaWithholdingsTotal << endl;
cout << "Total Net Pay : $" << setw(4) << setprecision(2)
<< fixed << netPayTotal << endl;
return 0;
}
The problem occurs when you type such symbols which can not be interpreted as double. For example, if you input gross pay "100,50" you input comma but you need to input point: "100.50". If you input comma than gross pay is set to 100 and ",50" left in the std::cin stream. ",50" can not be converted to double in the next input (cin >> federalTax;). As result, federalTax is not changed (if you use c++03) or set to 0 (if you use c++11). Details are in the article. So, (for c++03) each place where you input data (std::cin) does not change corresponding variable, ",50" is still in input stream (so, no waiting of the next user input), and at the end of each loop employeeNumber also is not changed, and than condition of while (employeeNumber != 0) of course is true, where an endless loop is formed. You can check each input using the next example:
cout << "Gross pay :";
while (!(cin >> grossPay)) {
std::cin.clear(); // clear all error state flags
std::string inputLine;
std::getline(std::cin, inputLine); // "remove" invalid data from std::cin
cout << "Please enter a valid gross pay" << endl;
cout << "Gross pay :";
}
I'm fairly new to c++ and I am writing a program that calculates the balance of a savings account at the end of a three-month period. I am supposed to use loops, which I have done and don't have much of a problem. The problem I am having is that all the numbers for deposit, withdrawal, current balance, etc. are supposed to be displayed as x.xx, and I am getting that output, but it also does that for the month. How do I make it so that the month doesn't display as x.xx?
Here's my code:
#include "pch.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
double startbalance;
double annualrate;
double monthlyrate;
double deposit;
double withdrawal;
double totaldeposit = 0;
double totalwithdrawal = 0;
double totalinterest = 0;
double monthstart = 0;
double monthend = 0;
printf("Welcome to Your Bank!\n");
cout << "What is your starting Balance? $";
cin >> startbalance;
cout << "What is the annual interest rate?. Please enter whole value. For example 6 for 6% :";
cin >> annualrate;
monthend += startbalance;
for (double month = 1; month <= 3; month++)
{
cout << "Month #" << month << endl;
do
{
cout << setprecision(2) << fixed;
cout << "Current Balance: $" << monthend << endl;
cout << "Please enter total amount of deposits: $";
cin >> deposit;
if (deposit < 0)
{
cout << "Deposit must be a positive number!\n";
}
} while (deposit < 0);
totaldeposit += deposit;
monthend += deposit;
do
{
cout << "Please enter total amount of withdrawals: $";
cin >> withdrawal;
if (withdrawal < 0 || withdrawal > monthend)
{
cout << "Withdrawal must be a positive number and not be larger than balance: $" << monthend << endl;
}
} while (withdrawal < 0 || withdrawal > totaldeposit);
cout << endl;
totalwithdrawal += withdrawal;
monthend -= withdrawal;
monthlyrate = ((monthstart + monthend) / 2 * (annualrate / 12));
totalinterest += monthlyrate;
cout << "New Balance: $" << monthend << "\n";
}
cout << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Start Balance: " << setw(9) << "$" << startbalance << "\n";
cout << "Total Deposits: " << setw(9) << "$" << totaldeposit << "\n";
cout << "Total Withdrawals: " << setw(9) << "$" << totalwithdrawal << "\n";
cout << "Total Interest Earned: " << setw(9) << "$" << totalinterest << "\n";
cout << "Final balance: " << setw(9) << "$" << monthend << "\n";
return 0;
}
Just type-cast your month variable to int before displaying.
cout << "Month #" << (int)month << endl;
That should fix your issue.
You can make monthend an int or a long. .........
Please make Data Type of your month as int instead of double.
double is a floating point data type. int is a whole number like 1, 2, 3, 4 and so on. Double is numbers with decimals like 1.1 or 45.564, float is a even more specific version of double
Example:
//if you just going to work with whole numbers
int a;
a = 1
//if you're working with numbers with a bit more precision
float b;
b = 1.1234
//if you're working with numbers with massive precision..
double c;
c = 1.123456
Your variable types seem to be fine for the calculation; I believe your problem is within this statement:
for (double month = 1; month <= 3; month++) {
cout << "Month #" << month << endl;
it is within your loop as why your month is printing out: 1.0, 2.0 etc.
change it to this:
for ( int month = 1; month <=3; month++ ) {
cout << "Month #" << month << endl;
Logically, variable month should be an integer.
Declare its datatype as int instead of double.
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
struct EmployeeData
{
string employeeName;
float overtime;
float grossPay;
float hoursWorked;
float hourlyRate;
float statetaxOwed;
float statetaxRate;
float fedtaxOwed;
float fedtaxRate;
float netPay;
float totalgp;
float totalft;
float totalst;
float totalnp;
};
EmployeeData employee[4]; //array of 4 employees
void calculate_stats(EmployeeData& employee);
int main()
{
for (int i = 0; i < 4; ++i)
{
cout << "Please enter Employee #" << (i+1) << "'s" << " name: ";
cin.ignore();
getline(cin, employee[i].employeeName);
cout << "Please enter Employee #" << (i+1) << "'s hours worked: ";
cin >> employee[i].hoursWorked;
cout << "Please enter Employee #" << (i+1) << "'s hourly rate: ";
cin >> employee[i].hourlyRate;
cout << "Please enter Employee #" << (i+1) << "'s Federal Tax Rate: ";
cin >> employee[i].fedtaxRate;
cout << "Please enter Employee #" << (i+1) << "'s State Tax Rate: ";
cin >> employee[i].statetaxRate;
cout << endl;
}
for (int i = 0; i < 4; ++i)
{
calculate_stats(employee[i]);
cout << setprecision(2) << showpoint << fixed;
cout << "Employee #" << (i+1) << "'s name is: " << employee[i].employeeName << endl;
cout << "Employee #" << (i+1) << "'s Gross Pay is: $" << employee[i].grossPay << endl;
cout << "Employee #" << (i+1) << "'s Federal Taxes owed is: $" << employee[i].fedtaxOwed << endl;
cout << "Employee #" << (i+1) << "'s State Taxes owed is: $" << employee[i].statetaxOwed << endl;
cout << "Employee #" << (i+1) << "'s Net Pay is: $" << employee[i].netPay << endl;
cout << endl;
}
cout << "Total Gross Pay: " << employee[4].totalgp << endl; //here is the problem
cout << "Total Federal Tax Owed: " << employee[4].totalft<< endl;
cout << "Total State Tax Owed: " << employee[4].totalft<< endl;
cout << "Total Net Pay: " << employee[4].totalft << endl;
}
void calculate_stats(EmployeeData& employee)
{
if (employee.hoursWorked>40) {
employee.hoursWorked = ((employee.hoursWorked-40) * (1.5)) + 40;
}
else {
employee.hoursWorked = employee.hoursWorked;
}
employee.grossPay = employee.hoursWorked * employee.hourlyRate;
employee.fedtaxOwed = employee.grossPay * (employee.fedtaxRate/100);
employee.statetaxOwed = employee.grossPay * (employee.statetaxRate/100);
employee.netPay = (employee.grossPay - employee.fedtaxOwed- employee.statetaxOwed);
employee.totalgp = employee.totalgp + employee.grossPay;
employee.totalft = employee.totalft + employee.fedtaxOwed;
employee.totalst = employee.totalst + employee.statetaxOwed;
employee.totalnp = employee.totalnp + employee.netPay;
}
I was sure to include that cout block outside of the for-loops but still in the main. Using calculate stats did not change the output for me. The output is giving me all 0.00...they should be complete totals across all 4 Employees (so if the gross pay for all 4 employees is 1000, then the total gross pay should be 4000, which is a grand total which I accounted for in my calculations: employee.totalgp = employee.totalgp + employee.grossPay;. Here is what the output looks like (a snapshot of the wrong display, the rest works fine)...
Total Gross Pay: 0.00
Total Federal Tax Owed: 0.00
Total State Tax Owed: 0.00
Total Net Pay: 0.00
Press any key to continue . . .
How do I fix this? Thank you!
The values are printing out as zero mainly due to luck on your part. Your array, defined as EmployeeData employee[4] allows for access to employee[0] to employee[3]. Accessing employee[4] for your print is accessing memory beyond what was allocated for your array.
Beyond that, you store nothing at the memory location with your code, which is not such a bad thing. You do store each set of data twice in each record:
employee.totalgp = employee.totalgp + employee.grossPay;
employee.totalft = employee.totalft + employee.fedtaxOwed;
employee.totalst = employee.totalst + employee.statetaxOwed;
employee.totalnp = employee.totalnp + employee.netPay;
Each call to calculate_stats is storing the global values in different locations, defeating your attempt to have a cumulative count. You need to define a separate EmployeeData instance to store the accumulated totals, and write the values into that object.
Once you have a new structure in place to store your totals, change the above four lines in your existing calculate_stats method to update the global totals structure.
My code compiles nicely, but the math formulas that I am using aren't providing the right outcome. I need to calculate the balance, withdrawn, and interest for all 3 months. I am also required to validate user's input. For these purposes I am using nested loops. Please let me know if you spot my mistake. Thank you lovely people!
cout << "Please enter the starting balance: ";
cin >> startBalance;
cout << "Please enter the annual interest rate: ";
cin >> annualInterest;
for (int month = 1; month <= 3; month++) {
do {
cout << setprecision(5);
cout << "Please enter the total amount deposited on month " << month << ": ";
cin >> balance;
if (balance <0) {
goodChoice = false;
cout << "\n\t***ERROR " << balance << " ***";
cout << "*** Choice must be positive***\n" << endl;
}
else {
goodChoice = true;
}
startBalance += balance; //need to calculate the balance for all 3 months
} while (!goodChoice);
do {
cout << setprecision(5);
cout << "Please enter the total amount withdrawn on " << month << ": ";
cin >> withdrawn;
if ( (withdrawn <0) || (withdrawn > startBalance) ) {
goodChoice = false;
cout << "***ERROR " << withdrawn << " ***";
cout << "*** Choice must be positive or greater than the balance***" << endl;
}
else {
goodChoice = true;
}
finalWithdrawn += withdrawn; // the total amount of withdrawn
finalBalance = startBalance - withdrawn;
monthInterest = ((startBalance + finalBalance) / 2) * (annualInterest / 12);
totalInterest += monthInterest; //total interest for all 3 months
endBalance = monthInterest + finalBalance;
} while (!goodChoice);
}
cout << "Total Deposit: " << endBalance << endl;
cout << "Total Withdrawn: " << finalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << endBalance << endl;
You have a lot of extra variables defined which aren't needed. Also, your interest rate may have been in percentage instead of a decimal number, i.e. 10% = 0.1. Also, your monthInterest is taking an average then applying interest. I wrote this up and it seems to work.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float totalDeposit = 0.0f;
float totalWithdrawn = 0.0f;
float totalInterest = 0.0f;
float totalBalance = 0.0f;
float interestRate = 0.0f;
setprecision(5); //?
cout << "Enter starting balance: ";
cin >> totalBalance;
cout << "Enter annual interest rate (%): ";
cin >> interestRate;
// Convert to monthly and non-percent; i.e. 10% = 0.1 = 10 / 100
interestRate = interestRate / 100.0f / 12.0f;
for (int month = 1; month <= 3; month++)
{
float deposit = -1.0; // Default to an error state
while (deposit < 0.0)
{
cout << "Enter total deposited in month " << month << ": ";
cin >> deposit;
if (deposit < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
float withdrawn = -1.0f; // Default to an error state
while (withdrawn < 0.0f)
{
cout << "Enter total withdrawn in month " << month << ": ";
cin >> withdrawn;
if (withdrawn < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
totalDeposit += deposit;
totalWithdrawn += withdrawn;
totalBalance = totalBalance + deposit - withdrawn;
totalBalance += totalBalance * interestRate;
totalInterest += totalBalance * interestRate;
}
cout << "Total Deposit: " << totalDeposit << endl;
cout << "Total Withdrawn: " << totalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << totalBalance << endl;
int wait; // Pause so console window doesn't close. Delete this line.
cin >> wait;
return 0;
}