1. ## First Program need advice

Well i am a complete and utter novice to programming. I know very little about c++ and what i do know i have learned from both the tutorials on this site and a book for beginners that i am borrowing from a friend.

It's A program to calculate compound interest.

If you have any advice or critisism i'm interested. I'm still learing.

Also i've heard that you shouldn't use "goto" but i don't know a better way.

It was all written by me except for the invalid input code. (Can someone break that down and explain to me how it works?)

i can't seem to upload so you'll just have to compile the code yourself to see it.

it might be a little hard to read
Code:
```/* Compound interest calculator. For stuff like investment accounts
(C)2006 Shane Loretz.
*/
#include <iostream>
#include <ios>
#include <limits>

using namespace std;

float intCalc(float apy, float smoney, float yrinc, float y);

int main()
{
cout<<"--------------------------------------------------------------------------------";//looks nice huh
cout<<"\nCompound Interest Calculator \n (C)2006 Shane Loretz\n\n";
cout<<"--------------------------------------------------------------------------------";
restart:
float errorCheck;
float y;
float apy;
float smoney;
float yrinc;

//user input
y:
cout<<"\nPlease enter how many years till you collect the money (At least 2): ";
cin>> y;
if (y >= 150) cout<<"\n Please keep the years below 150\n\n"; //limits entry size
cout<<"\nPlease enter your expected anual interest in decimal form.\n Example: 5% = 0.05: ";
cin>> apy ;
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
cout<<"\nPlease enter the initial money in the account: ";
cin>> smoney ;
Can also be negative if you take money out each year: ";
cin>> yrinc ;
intCalc(apy, smoney, yrinc, y);
//exit or redo question
ques:
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) goto restart;// checks answer
else if ((int)pend == 2) goto exit;
else goto idiot;
//If you enter another number besides 1 or 2.
idiot:
while (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\t..::Close And Re-open Program::..\n\n\n\n\n\n\n\n\n\n"){
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');

// Try again
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;
}}
float intCalc(float apy, float smoney, float yrinc, float 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<< " year: "<< w << " = " << result << " \n" ;
//following years loop
do{
result += yrinc;
result = result * apy;
w++;
cout<< " year: "<< w << " = " << result << " \n" ;
}while ( w < y );
return 0;
}```

2. Work on loops. Start with something small, then build up from that. Your gotos should all be replaced with loops. You have a few while loops in there, but I'm not sure you understand how they work. For example, instead of goto restart, just use a loop that runs the code between the restart label and the goto until the user selects exit. The code can get messy, but you break it down with functions.

For example, you can write a function that gets a value from the user. Start by moving just the code that asks for years. Then write a function that just asks for the value for the apy variable. If you see a pattern, you might be able to make a generic function and call it more than once.

Also, you should be using double instead of float, since double is the default floating-type type in C++, you have no reason for needing float, and you might want the extra precision in some cases.

3. >you might want the extra precision in some cases.

Just to help you on this what daved suggested, you can set the precision of the decimal when you use double.

Code:
`cout << "My value is: " << setprecision( 2 ) << fixed << value << endl;`
setprecision is defined in the

Code:
`#include <iomanip>`
header file, and fixed - which ensures the deimal is shown, is defined in the iostream library.

If you have a large set of values to precision, you can skip having to do each one individually by simple entering

Code:
`cout << fixed << setprecision( 2 );`
before you display them. This saves time and

4. I was actually referring to the precision of the type, double is more accurate than float as the number of significant digits increases. But the setprecision manipulator advice is good, too.

5. ## Okay but now i have a bug

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 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

cout<<"\nPlease enter the initial money in the account: ";
smoney = smoney_yrinc(smoney);
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
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.
{
}```

6. *Bump*

still haveing trouble with the user haveing to enter input twice. can anyone help?

7. 1) No, its not.
2) Indetation.

8. Originally Posted by manutd
1) No, its not.
2) Indetation.
what?

oh you saw the post b4 i edited it.

do you know why my function error is makeing the user input twice?

9. Because this is here:
Code:
``` 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";
}```
THis is identical to your error() function.

10. Okay i removed that but the program still makes the user input data twice. It doesn't happed if i don't call the error() function.

11. Because you are asking for input, then calling the error function that asks for input again.

Remove the calls to cin >> before the calls to the error function.

12. Okay that works thanks for that.

here's my code. everything works. thanks guys

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 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

cout<<"\nPlease enter the initial money in the account: ";
smoney = smoney_yrinc(smoney);
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.

}

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    ..::Press any number plus enter to retry::..\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 >= 100){
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 >= 100) {cout<<"\n Please keep the years below 100\n\n";} //limits inputed data size
return y;
}
}
double apyAsk(double apy) //gets anual percent yeild data from user
{
while (apy >= 10){
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 >= 10) cout<<"\n Please keep interest below 10.0 or %1,000\n\n";} // keeps idiots from making the program output huge numbers
}
return apy;
}
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;
pend = error(pend);
if ((int)pend == 1) {
restart = 11; //anything more then ten restarts program
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 main to evaluate 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      ..::Press any number plus enter to close program::..\n\n\n\n\n\n\n\n\n\n";
}```
now i'm gonna work on other options like quarterly interest instead of anually. and maybe instananeous interest

13. >cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t

You could put all those newline characters in one loop

Code:
```for ( int i = 0; i < 16; i++ )
{
cout << "\n";
}```

14. Wow i didn't realise i had written that many.

I'll try that thanks

15. Even better (at least when you learn it), use a string:
Code:
`cout << std::string(16, '\n');`