Write a C++ program that accepts the time that funds are left on deposit and dis
ID: 3558174 • Letter: W
Question
Write a C++ program that accepts the time that funds are left on deposit and displays the interest rate that corresponds to the time entered.
Time on Deposit Interest Rate
1.Greater than or equal to 5 years ;0.095
2.Less than 5 years but greater than Or equal to 4 years; 0.09
3.Less than 4 years but greater than or Equal to 3 years; 0.085
4.Less than 3 years but greater than or Equal to 2 years; 0.075
5.Less than 2 years but greater than or equal To 1 year; 0.065
6.Less than 1 year; 0.058
****************************************************************************************************************
MY QUESTION IS:
Will this program that I wrote run and what can I add to make it better??
#include <iostream>
using namespace std;
int main()
{
int choice;
int interest_rate=0;
const int YEAR5=1,
YEAR4=2,
YEAR3=3,
YEAR2=4,
YEAR1=5,
YEAR0=6;
do
{
cout <<
Explanation / Answer
You should use switch or if-else statements. Do while are for looping and not for checking for condition.
Here is the corrected code:
#include <iostream>
using namespace std;
int main()
{
int choice;
float interest_rate;
do{
cout <<"How many years has it been since the deposit? ";
cin >> choice;
}while (choice<0 ||choice>6);
if(choice >= 5)
interest_rate = 0.095;
else if(choice >= 4)
interest_rate = 0.09;
else if(choice >= 3)
interest_rate = 0.085;
else if(choice >= 2)
interest_rate = 0.075;
else if(choice >= 1)
interest_rate = 0.065;
else if(choice >= 0)
interest_rate = 0.058;
cout<<"Your interest rate is: "<<interest_rate<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.