Compound interest program

I need to write a program that takes in two values entered by the user, compares them with 50 and 5000, chooses the max and min of these values and takes them as initial deposits. It works out the compound interest at 5% on 15,000 in monthly instalments and shows the interest for each initial deposit for 2 years and 10 years. I need to include fill characters and field separators but none of it seems to be working for me. any mistakes found would be a great help. (its a rough draft so is very messy)

#include <iomanip>
#include <iostream>
#include <cmath>
#include <windows.h>
#include <string>

using namespace std;.

float calc(float r, int n);
float compound2y(int p, float calc, double x, int initialdeposit);
float compound10y(int p, float calc, double y, int initialdeposit);

int main (int arc, char* argv[] )
{
system("COLOR F0");

int p=15000;
float r=0.05;

HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);


double n=12, max, min;
int x=n*2;
int y=n*10;
int a,b;
float ci2y, ci10y;
char ans = 'Y' || 'y' || 'N' || 'n';
int initialdeposit;

cout << "enter a ";
cin >> a;
cout << "enter b";
cin >> b;

cout << left << setw(20) << "\ninitial deposit";
cout << left << setw(20) << "?";
cout << left << setw(20) << "2 years";
cout << left << setw(20) << "?";
cout << left << setw(20) << "10 years" << endl;
do
{
if (a<50 && a<b)
{
min = a;
}

else if (b<=a && b<50)
{
min = b;
}

else if (50<=a && 50<=b)
{
min = 50;
}

if (a>50 && a>b)
{
max = a;
}

else if (b>=a && b>50)
{
max =b;
}

else if (50>=a && 50>=b)
{
max = 50;
}
initialdeposit=0.0;

for (int i=min; i<=max; i+=500)
{
initialdeposit=i;

cout << setfill('e') << left << setw(20) << fixed << initialdeposit;
cout << left << setw(20) << '?';
cout << setfill('e') << left << setw(20) << fixed << compound2y;
cout << left << setw(20) << '?';
cout << setfill('e') << left << setw(20) << fixed << compound10y;
}
cout << "\nDo you want to try another interval? (Y/N) ";
cin >> ans;

if (ans == 'N' && ans == 'n')//condition if user answers no to question
{
cin.get();//end program
}

else if (ans != 'Y' && ans !='y' && ans != 'N' && ans != 'n')
{

SetConsoleTextAttribute(hConsole,244);
cout << "\nInvalid input. Please answer Y / N. ";
SetConsoleTextAttribute(hConsole,240);
cin >> ans;
}
}
while (ans == 'Y' || ans == 'y' && initialdeposit<=max);


cout << "\n\nPress enter to end this program" << std::endl;
cin.get();


}

float calc(float r, int n)
{
return (1+r/n);
}

float compound2y(int p, float calc, double x, int initialdeposit)
{
return (p-initialdeposit)*pow(calc, x)-p;
}

float compound10y(int p, float calc, double y, int initialdeposit)
{
return (p-initialdeposit)*pow(calc,y)-p;
}
You logic is flawed.

In this expression:
if (ans == 'N' && ans == 'n')//condition if user answers no to question
It is impossible that ans can be both 'N' and 'n' which would be the requirement in order that the expression will ever be true.

This:
while (ans == 'Y' || ans == 'y' && initialdeposit<=max);
makes more sense. Take a look at the operator precedence:

http://en.cppreference.com/w/cpp/language/operator_precedence

&& is higher than ||. Hence the expression is like this:
(ans == 'Y') || ((ans == 'y') && (initialdeposit<=max))
You see the problem? Better use parenthesis.
Topic archived. No new replies allowed.