当用户按Y继续时,如何清除变量的所有先前值?
现在,当用户按Y时,此代码会保留所有值的堆叠。 我该怎么做才能清除以前所有变量的值?
double monthlyPayment; //Get the monthly payment from user
double balance; // formula to calculate the loan.
double interestRate; // get the interest rate from user
double interestPaid;
double total_amount_of_interest_paid;
double initialBala`enter code here`nce; // get the initial cost of the item/loan.
char continueMessage; // to continue the program.
int month = 1;
cout.setf(ios::fixed); // 2 decimal places for decimal values.
cout.setf(ios::showpoint);
cout.precision(2);
do{
cout << "Enter the amount of the loan: "; //Getting the amount of the loan from user.
//Error checking for balance
if(!(cin >> initialBalance) || initialBalance<= 0.0)
{
do
{
cin.clear(); //Reset the failed state
cin.ignore(1000, '\n'); //Discards up to 1000 characters or until it reaches a new line.
cout << "Please enter positive numbers only: ";
}while(!(cin >> initialBalance) || initialBalance <= 0.0);
}
cout << "Enter the interest rate per month in percentage(%): "; //Getting interest rate from user.
//Error checking for interest rate. If the value is less than 1 means user entered the value in decimal form.
if (!(cin >> interestRate) || interestRate < 1.0)
{
do
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Please make sure you enter postive numbers and in percentage form: " ;
}while(!(cin >> interestRate )|| interestRate <=0.0);
}
cout << "Please enter your monthly payment: ";// Getting monthly payment from user.
//Error checking for monthly payment. Value entered must be positive numbers.
if(!(cin >> monthlyPayment) || monthlyPayment < 0.0)
{
do
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Please enter positive numbers only: ";
}while(!(cin >> monthlyPayment) || monthlyPayment <0.0);
}
balance = initialBalance;
interestRate = interestRate/100;
balance = balance - (monthlyPayment - (balance * (interestRate)));
cout << "Your remaining debt on month 1 is: $"<<balance<<endl;
while(balance > 0)
{
if (balance < monthlyPayment){
balance = balance - balance; // when monthly payment more than balance means the debt is fully settled.
}else{
balance = balance - (monthlyPayment - (balance * (interestRate)));
}
month++;
cout << "Your remaining debt on month "<<month<<" is: $" << balance <<endl;
}
cout << "Number of months taken to clear the debt: "<<month<<endl;
total_amount_of_interest_paid = (monthlyPayment*month) - initialBalance;
cout << "Total amount of interest paid over the life of the loan: "<<total_amount_of_interest_paid;
//Ask the use whether wants to continue the program or not.
cout << "\n\nPress 'y' or 'Y' to continue (other characters to quit): ";
cin >> continueMessage;
}while(continueMessage == 'y' || continueMessage =='Y');
return 0;
}
答案 0 :(得分:0)
您只需要设置一个条件语句来检查continueMessage
,如果它是yes
,那么continue
否则break
循环。
答案 1 :(得分:0)
执行此操作的一种好方法是将大多数do
循环重新组织为具有局部变量的函数:
do {
calculateInterest();
// Ask the user whether wants to continue the program or not.
cout << "\n\nPress 'y' or 'Y' to continue (other characters to quit): ";
cin >> continueMessage;
} while (continueMessage == 'y' || continueMessage =='Y');
...
void calculateInterest(void) {
double monthlyPayment; //Get the monthly payment from user
double balance; // formula to calculate the loan.
double interestRate; // get the interest rate from user
...
}
这样,每次运行函数时都会隐式重置变量。这样做也有助于为可能需要阅读它的其他人更清楚地组织代码。