hi there
i am very now to programming and have just got my first assignment.
I give the user three lines of options with the choice of 1,2 or 3. i am now stress testing the program and if for instance 2.5 is entered into the “choice” variable the program goes haywire! any help would be much appreciated! here is the program
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int choice;
double totalrepay;
double eachrepay;
double interest;
double purchase;
char response;
do
{
cout<<" Hire Purchase calculation Programme
“;
cout<<” ***************************************************
";
cout<<"1. Immediate Payment (10% reduction)
";
cout<<"2. Medium Term Payment (10% Increase, 12 Payments)
";
cout<<"3. Long term Payment (30% Increase, 36 Payments)
“;
cout<<” Please enter your number of your choice
";
cin>>choice;
if (choice<1 || choice>3)
do
{
cout<<"Your choice is invalid, please re-enter your choice
";
cin>>choice;
}
while (choice<1 || choice>3);
cout<<"Please enter the Cost of your purchase.
";
cin>>purchase;
if(choice==1)
{
totalrepay=0;
eachrepay=0;
interest=0;
cout<<fixed<<setprecision(2)<<"The total cost of your repayments is €"<<totalrepay;
cout<<endl;
cout<<fixed<<setprecision(2)<<"The cost of each repayment is €"<<eachrepay;
cout<<endl;
cout<<fixed<<setprecision(2)<<"The cost of your interest is €"<<interest;
cout<<endl;
}
else if(choice==2)
{
totalrepay=purchase*1.1;
eachrepay=totalrepay/12;
interest=purchase*0.1;
cout<<fixed<<setprecision(2)<<"The total cost of your repayments is €"<<totalrepay;
cout<<endl;
cout<<fixed<<setprecision(2)<<"The cost of each repayment is €"<<eachrepay;
cout<<endl;
cout<<fixed<<setprecision(2)<<"The cost of your interest is €"<<interest;
cout<<endl;
}
else if(choice==3)
{
totalrepay=purchase*1.3;
eachrepay=totalrepay/36;
interest=purchase*0.3;
cout<<fixed<<setprecision(2)<<"The total cost of your repayments is €"<<totalrepay;
cout<<endl;
cout<<fixed<<setprecision(2)<<"The cost of each repayment is €"<<eachrepay;
cout<<endl;
cout<<fixed<<setprecision(2)<<"The cost of your interest is €"<<interest;
cout<<endl;
}
else
cout<<"I don't know how you got here
"<<endl;
cout<<"Do you want another go? y/n
";
cin>>response;
cout<<endl;
}
while (response==‘y’ || response==‘Y’);
system ("pause");
return 0;
}