So I am trying to creat a time value of money calculator. The problem I am having is creating an error message and getting it to start back at the beginning of the program as well as having the program start over if the user wants to calculate another value. The calculator works fine as long as a value that is listed is put in. I would like to have the program give the error message I have and loop back to the beginning of the program. I have tried using a do while loop but the program ends up looping and won't stop so I have to close it before it crashes. I am very new at C++ so any help would be greatly appreciated. Below is the code I am using.
// TVM If Statement.cpp : Defines the entry point for the console application.
//
//Converts i as a double number to a percentage.
double pconvert (double i)
{
double pc;
pc=i/100;
return (pc);
}
int main()
{
using namespace std;
double FV;
double PV;
double i;
double n;
int x;
cout << "Please select the value you wish to obtain from the list below. \n";
cout << "1. Present Value \n";
cout << "2. Future Value \n";
cin >> x;
if (x == 1)
{
cout << "Please enter the FV: \n";
cin >> FV;
cout << "Please enter the interest rate percentage: \n";
cin >> i;
double pc = pconvert (i);
i = pc;
cout << "Please enter the time period: \n";
cin >> n;
PV = FV / pow((1+i),n);
cout << PV;
cout << "\n";
}
else if (x == 2)
{
cout << "Please enter the PV: \n";
cin >> PV;
cout << "Please enter the interest rate percentage: \n";
cin >> i;
double pc = pconvert (i);
i = pc;
cout << "Please enter the time period: \n";
cin >> n;
FV = PV*pow((1+i),n);
cout << FV;
cout << "\n";
}
else
{
cout << "Error: Please enter either 1 or 2. \n";
}
cout << "Would you like to run another calculation? \n";
cout << "1. Yes: Continue \n";
cout << "2. No: Quit \n";
well there are two basic loop functions, for and while loops, so do for or while loop around the section you want to loop until the correct input is entered. and please remove your system("pause") from the program. there are lots of ways to replicate how it runs without putting a huge whole in your program.