I'm having trouble with a program with class.
When it runs, on the screen and on the file it shows one output, the last one in the input file. It USED to work, untill I tried to get fancy and add some other code... that will be below the main block of code. I would like to make the bottom one work if I can. I don't want to 'cheat' but any other code types (something other than while?) would be greatly appreciated!
Here is my algorithm:
Declare constants: CHECK_FEE = 25, SAVINGS_FEE = 10, CHECKING_TOP_LIMIT = 5000, CHECK_LOW_INTEREST = .03,
CHECK_HIGH_INTEREST = .05, SAVING_INTEREST = .04
Declare ints: AccountNumber, MinimumBlanace
Declare doubles: InputBalance, FinalBalance
Declare char: AccountTypeLoop starts here, ends when number goes above 5 (user inputted number
read external file
store first number as AccountNumber
store next character as AccountType
store next number as MinimumBlanace
store next number as InputBalanceIf character is 'S' or 's'
if InputBalance is less than MinimumBlanace, charge SAVINGS_FEE against account, store result in InputBalance.
else, contunue the program.if InputBalance is equal to or is higher than MinimumBalance, apply SAVING_INTEREST to account.
store result as FinalBalance.
else
store InputBalance as FinalBalance.output results
If character is 'C' or 'c'
if InputBalance is less than MinimumBlanace, charge CHECK_FEE against account, store result in InputBalance.
else, contunue the program.break
if InputBalance is equal to or is higher than MinimumBalance do:
{if InputBalance is in between MinimumBlanace and CHECKING_TOP_LIMIT, then interest CHECK_LOW_INTEREST is applied to account.
Store new result as FinalBalance}
else interest applied to InputBalance is CHECK_HIGH_INTEREST, store new result as FinalBalance.
else
store InputBalance as FinalBalance.If neither output error message.
output results
loop to 'read external file' until finished with file
input file:
46728 S 1000 2700
87324 C 1500 7689
79873 S 1000 800
89832 C 2000 3000
98322 C 1000 750
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
const double CHECK_FEE = 25, SAVINGS_FEE = 10, CHECKING_TOP_LIMIT = 5000, CHECK_LOW_INTEREST = .03;
const double CHECK_HIGH_INTEREST = .05, SAVING_INTEREST = .04;
void main()
{
int AccountNumber, loop, loopEnd;
double InputBalance, MinimumBlanace, FinalBalance, CheckTopLimit;
char AccountType;
ofstream fout;
ifstream fin;
fin.open("bankdata.nf0");
fout.open("bankdataout.nf0", ios::app);
cout << "Please enter the amount of accounts that will be updated for this month." << endl;
cin >> loopEnd;
for(loop = 0; loop < loopEnd; loop++)
{
fin >> AccountNumber >> AccountType >> MinimumBlanace >> InputBalance;
switch(AccountType) // Depending on the AccountType variable, one of three choices will be made.
{
case 's': // If AccountType is a 's' or 'S' run below commands.
case 'S':
if (InputBalance < MinimumBlanace) // If starting balance is less than minimum balance, apply fee.
InputBalance = InputBalance - SAVINGS_FEE;
else // If starting balance is not less than minimum balance, apply interest.
InputBalance = (SAVING_INTEREST * InputBalance) + InputBalance;
FinalBalance = InputBalance;
break;
case 'c': // If AccountType is a 'c' or 'C' run below commands.
case 'C':
if (InputBalance >= MinimumBlanace) // If account balance is less than or equal to, run below commands.
{
CheckTopLimit = CHECKING_TOP_LIMIT + MinimumBlanace;
if (InputBalance <= CheckTopLimit) // If account balance is or is less than the checking top limit, apply
InputBalance = (InputBalance * CHECK_LOW_INTEREST) + InputBalance; // lower interest rate.
else // If account is more than the checking top interest, apply higher interest rate.
InputBalance = (InputBalance * CHECK_HIGH_INTEREST) + InputBalance;
}
else // If the account balance is not greater than the minimum balance, apply fee.
InputBalance = InputBalance - CHECK_FEE;
FinalBalance = InputBalance;
break;
default:
cout << "You entered the wrong account type. Please check input file.";
}
} // end of correct/false account type.
fout << fixed << showpoint << setprecision(2);
fout << setw(15) << "ACCOUNT NUMBER" << setw(14) << "ACCOUNT TYPE" << setw(22) << "NEW CURRENT BALANCE" << endl;
fout << setw(15) << AccountNumber << setw(14) << AccountType << setw(22) << FinalBalance << endl;
fout << endl << endl << endl << endl;
cout << fixed << showpoint << setprecision(2);
cout << setw(15) << "ACCOUNT NUMBER" << setw(14) << "ACCOUNT TYPE" << setw(22) << "NEW CURRENT BALANCE" << endl;
cout << setw(15) << AccountNumber << setw(14) << AccountType << setw(22) << FinalBalance << endl;
cout << endl << endl << endl << endl;
fout.close();
fin.close();
cout << "Press Enter to end the program.";
cin.ignore();
cin.ignore();
}
Varient:
// Get file input, preform calculations, output results to file and screen.
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
const double CHECK_FEE = 25, SAVINGS_FEE = 10, CHECKING_TOP_LIMIT = 5000, CHECK_LOW_INTEREST = .03;
const double CHECK_HIGH_INTEREST = .05, SAVING_INTEREST = .04;
void main()
{
int AccountNumber, loop, loopEnd;
double InputBalance, MinimumBlanace, FinalBalance, CheckTopLimit;
char AccountType;
bool validAccount = false;
ofstream fout;
ifstream fin;
fin.open("bankdata.nf0");
fout.open("bankdataout.nf0");
while (validAccount == false) // if false, re-run data.
{
cout << "Please enter the amount of accounts that will be updated for this month." << endl;
cin >> loopEnd;
for(loop = 0; loop < loopEnd; loop++)
{
if (validAccount == false)
{
fin >> AccountNumber >> AccountType >> MinimumBlanace >> InputBalance;
switch(AccountType) // Depending on the AccountType variable, one of three choices will be made.
{
case 's': // If AccountType is a 's' or 'S' run below commands.
case 'S':
if (InputBalance < MinimumBlanace) // If starting balance is less than minimum balance, apply fee.
InputBalance = InputBalance - SAVINGS_FEE;
else // If starting balance is not less than minimum balance, apply interest.
InputBalance = (SAVING_INTEREST * InputBalance) + InputBalance;
FinalBalance = InputBalance;
validAccount = true;
break;
case 'c': // If AccountType is a 'c' or 'C' run below commands.
case 'C':
if (InputBalance >= MinimumBlanace) // If account balance is less than or equal to, run below commands.
{
CheckTopLimit = CHECKING_TOP_LIMIT + MinimumBlanace;
if (InputBalance <= CheckTopLimit) // If account balance is or is less than the checking top limit, apply
InputBalance = (InputBalance * CHECK_LOW_INTEREST) + InputBalance; // lower interest rate.
else // If account is more than the checking top interest, apply higher interest rate.
InputBalance = (InputBalance * CHECK_HIGH_INTEREST) + InputBalance;
}
else // If the account balance is not greater than the minimum balance, apply fee.
InputBalance = InputBalance - CHECK_FEE;
FinalBalance = InputBalance;
validAccount = true;
break;
}
}
else
cout << "You entered the wrong account type. Please check input file.";
} // end of correct/false account type.
fout << fixed << showpoint << setprecision(2);
fout << setw(15) << "ACCOUNT NUMBER" << setw(14) << "ACCOUNT TYPE" << setw(22) << "NEW CURRENT BALANCE" << endl;
fout << setw(15) << AccountNumber << setw(14) << AccountType << setw(22) << FinalBalance << endl;
fout << endl << endl << endl << endl;
cout << fixed << showpoint << setprecision(2);
cout << setw(15) << "ACCOUNT NUMBER" << setw(14) << "ACCOUNT TYPE" << setw(22) << "NEW CURRENT BALANCE" << endl;
cout << setw(15) << AccountNumber << setw(14) << AccountType << setw(22) << FinalBalance << endl;
cout << endl << endl << endl << endl;
fout.close();
} // end of enter data loop.
fin.close();
cout << "Press Enter to end the program.";
cin.ignore();
cin.ignore();
}
/*
Checking
has Minimum balance, is variable.
if balance is too low, there is a 25.00 fee.
else, no fee.
also, if blanace is or above minimun, interest is applied:
inbetween minimum balance and 5000, then interest is 3%
else it is 5%
Savings
has Minimum balance, is variable.
if balance is too low, there is a 10.00 fee.
else, no fee.
if blanace is the minimum blance or above
then 4% interest
*/
/*
Declare constants: CHECK_FEE = 25, SAVINGS_FEE = 10, CHECKING_TOP_LIMIT = 5000, CHECK_LOW_INTEREST = .03,
CHECK_HIGH_INTEREST = .05, SAVING_INTEREST = .04
Declare ints: AccountNumber, MinimumBlanace
Declare doubles: InputBalance, FinalBalance
Declare char: AccountType
read external file
store first number as AccountNumber
store next character as AccountType
store next number as MinimumBlanace
store next number as InputBalance
If character is 'S' or 's'
if InputBalance is less than MinimumBlanace, charge SAVINGS_FEE against account, store result in InputBalance.
else, contunue the program.
if InputBalance is equal to or is higher than MinimumBalance, apply SAVING_INTEREST to account.
store result as FinalBalance.
else
store InputBalance as FinalBalance.
output results
If character is 'C' or 'c'
if InputBalance is less than MinimumBlanace, charge CHECK_FEE against account, store result in InputBalance.
else, contunue the program.
break
if InputBalance is equal to or is higher than MinimumBalance do:
{if InputBalance is in between MinimumBlanace and CHECKING_TOP_LIMIT, then interest CHECK_LOW_INTEREST is applied to account.
Store new result as FinalBalance}
else interest applied to InputBalance is CHECK_HIGH_INTEREST, store new result as FinalBalance.
else
store InputBalance as FinalBalance.
output results
If neither output error message.
loop to 'read external file' until finished with file
*/