algright here's my code now.
My problem lies with fuction error. It works to stop invalid input from makeing
hell on my screen but now the user has to enter data in twice.
compile it and see what i mean
oh and ty btw for the help.
EDIT: i commented it for easier reading
Code:
/* Compound interest calculator. For stuff like investment accounts
(C)2006 Shane Loretz.
*/
#include <iostream>
#include <ios>
#include <limits>
#include <iomanip>
using namespace std;
double intCalc(double apy, double smoney, double yrinc, double y);
double error(double);
double yAsk(double);
double apyAsk(double);
double smoney_yrinc(double);
int ques(int);
int idiot(int);
int main()
{
cout<<"--------------------------------------------------------------------------------";//looks nice huh
cout<<"\nCompound Interest Calculator \n (C)2006 Shane Loretz\n\n";
cout<<"--------------------------------------------------------------------------------";
int restart;
do{ //for when prompted to do another calc
double errorCheck;
double y;
double apy = 101;
double smoney;
double yrinc;
//user input these functions should be self explanatory
y = yAsk(y);
apy = apyAsk(apy);
cout<<"\nPlease enter the initial money in the account: ";
smoney = smoney_yrinc(smoney);
cout<<"\nPlease enter how much you are going to add to your accout per year.\n \
Can also be negative if you take money out each year: ";
yrinc = smoney_yrinc(yrinc);
intCalc(apy, smoney, yrinc, y); //calculates recieved data
//exit or redo question
restart = ques(restart); //restart function
} while (restart > 10); //for when prompted to do another calc
return 0;
//If you enter another number besides 1 or 2.
int input;
//I took the invalid input code form cprogramming.com
//It seems that when the program freaks it goes to the last part of main()
//so i leave it here.
while ( !( cin>> input ) ) {
// Clear the error state
cin.clear();
// Remove the unrecognized characters
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t ..::Error Invalid Input::..\n\n\t\tDon't use Characters other then the following:\n\t\t\t 0 1 2 3 4 5 6 7 8 9 . \n\n\t\t\t..::Close And Re-open Program::..\n\n\n\n\n\n\n\n\n\n";
}
exit:
return 0;
}
double intCalc(double apy, double smoney, double yrinc, double y)
{
int w = 1;
long double result;
apy += 1;//adds 1 to apy
//first year calculation. this NEEDS to be here or smoney is impossible to use in the calculation
result = smoney * apy;
result += yrinc;
cout<< fixed << setprecision( 2 ) << " year: "<< w << " = " << result << " \n" ;
//following years loop
do{
result += yrinc;
result = result * apy;
w++;
cout<< fixed << setprecision( 2 ) << " year: "<< w << " = " << result << " \n" ; //prints result fixed and setprecision ensure 2 decimal places will be used
}while ( w < y );
return 0;
}
double error(double input) //checks for invalid input
{
while ( !( cin>> input ) ) {
// Clear the error state
cin.clear();
// Remove the unrecognized characters
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t ..::Error Invalid Input::..\n\n\t\tDon't use Characters other then the following:\n\t\t\t 0 1 2 3 4 5 6 7 8 9 . \n\n\t\t\t..::Close And Re-open Program::..\n\n\n\n\n\n\n\n\n\n";
}
return input;
}
double yAsk(double y) //gets year data from user
{
y = 151;
while (y >= 150){
cout<<"\nPlease enter how many years till you collect the money (At least 2): ";
cin>> y;
y = error(y); //checks for invalid input
if (y >= 150) {cout<<"\n Please keep the years below 150\n\n";} //limits entry size
return y;
}
}
double apyAsk(double apy) //gets anual percent yeild data from user
{
while (apy >= 100){
cout<<"\nPlease enter your expected anual interest in decimal form.\n Example: 5% = 0.05: ";
cin>> apy ;
apy = error(apy);//checks for invalid input
{if (apy >= 100) cout<<"\n Please keep interest below 100.0 or %10,000\n\n";} // keeps idiots from making the program output huge numbers
}
}
double smoney_yrinc(double value) //used for both start money and yearly deposites
{
cin>> value;
value = error(value);//checks for invalid input
return value;
}
int ques(int restart)
{
int pend; //pend variable should be with other variables but i like it here
cout<<"\nPress 1 to do another calculation or press 2 to exit: ";
cin>> pend;
if ((int)pend == 1) {
restart = 11; //anything more then ten restarts program
}// checks answer
else if ((int)pend == 2) {
restart = 1; //anything less then 10 closes program
cout<<"Good bye";
}
else
{
idiot(pend); //anything else and the user goes to idiot
cin>>pend; //i don't remember why i did this
}
return restart; //sends restart data back to mainto evaluae whether the program is closed or not
}
int idiot(int x) //for the idiots that can't press 1 or 2.
{
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t ..::Error Invalid Menu Option::.. \n\n\t\t ..::Please Close and Reopen Program::..\n\n\n\n\n\n\n\n\n\n";
}