[try Beta version]
Not logged in

 
while loop?

Mar 30, 2014 at 10:57pm
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 Mar 30, 2014 at 10:59pm
Mar 30, 2014 at 11:00pm
Why do you think it would execute?
Mar 30, 2014 at 11:02pm
The semicolon ends the while expression, therefore moving on to the next set of code.
Mar 30, 2014 at 11:04pm

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.