Do while loop not working

closed account (DN7SE3v7)
At the end of the code, the program asks: "Run the program again?(Type Yes to continue.)" however the loop continues no matter what answer is given. Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
using namespace std;
double inflation_rate(double current_price,double past_price);

int main()
{
	string answer;
    do
	{
	double current_price, past_price;
	cout << "Please enter the current price of an item:\n";
    cin >> current_price;
    cout << "Please enter the past price of an item\n";
    cin >> past_price;
    cout << inflation_rate(current_price, past_price);
	cout << "\n";
    cout << "Run the program again?(Type Yes or No.)\n";
    cin >> answer;
	}while(answer == "Yes" || "yes");
    
    
}
double inflation_rate(double current_price, double past_price)
{
    double rate_of_inflation = (current_price - past_price)/past_price;
    return rate_of_inflation;
}
while(answer == "Yes" || "yes") should be while(answer == "Yes" || answer == "yes") what you are doing is checking if it is equal to Yes then you check if yes is not equal to 0. You should be checking to see if it is equal to yes or it is equal to Yes
See comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
using namespace std;
double inflation_rate(double current_price, double past_price);

int main()
{
	string answer;
	do
	{
		double current_price, past_price;
		cout << "Please enter the current price of an item:\n";
		cin >> current_price;
		cout << "Please enter the past price of an item\n";
		cin >> past_price;
		cout << inflation_rate(current_price, past_price);
		cout << "\n";
		cout << "Run the program again?(Type Yes or No.)\n";
		cin >> answer;

	} while (answer == "Yes" || answer == "yes"); //C++ does not support 'implied' comparisons.


}
double inflation_rate(double current_price, double past_price)
{
	double rate_of_inflation = (current_price - past_price) / past_price;
	return rate_of_inflation;
}
Topic archived. No new replies allowed.