while loop?

Okay, yet another question. This time, it has to do with while loops. Let me paste the code first.
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
  #include <iostream>
#include <string>
using namespace std;

int main()
{
	string userselection = "no";

	while (userselection == "yes")
	{
		cout << "Enter two numbers: " << endl;
		int num1 = 0;
		int num2 = 0;
		cin >> num1;
		cin >> num2;

		cout << num1 << " X " << num2 << " = " << num1 * num2 << endl;

		cout << "Press 'yes' to redo operation, any other to exit! " << endl;
		cin >> userselection;
	}

	cout << "Goodybe" << endl;
	return 0;
}


When I execute this, it doesn't even execute the codes inside the second braces. When I add a semicolon after the while statement, the braces execute, however the loop won't happen. Help me out here?
Last edited on
Why do you think it would execute?
The semicolon ends the while expression, therefore moving on to the next set of code.

userselection is assign "no"

--> Is userselction "yes"?
False, do not execute loop

--> "Goodybe" (Typo)


Maybe you are looking for a do-while loop. Or to initialize userselection with "yes" instead.
Topic archived. No new replies allowed.