Trouble with looping a function

With the following code:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;
bool key = true;
char swit;

void cont(char ans)
{
	switch (ans)
	{
			case 'y': 
				break;
			case 'n': 
				cout <<"Goodbye\n";
				key = false;
				break;
			default: 
				cout <<"Derp\n";
				key = false;
				break;
	}
}

int fact(int xx)
{
	int Fact(1);
	for (int i = 1; i <= xx; i++)
		Fact *= i;
	return Fact;
}

void main()
{
	int x;
	while (key = true)
	{
		cout <<"Enter an integer up to 10: "; cin >> x;
		if (x > 10)
			cout <<"Invalid input.\n";
		else if (x > -1)
			cout <<"Factorial of "<< x <<" = "<< fact(x) <<"\n";
		else
			cout <<"Invalid input.\n";
		cout <<"\nContinue? (y/n): "; cin >> swit; cout <<"\n";
		cont(swit);
	}
}

I get the following output:


Enter an integer up to 10: 11
Invalid input.

Continue? (y/n): y

Enter an integer up to 10: 0
Factorial of 0 = 1

Continue? (y/n): y

Enter an integer up to 10: -1
Invalid input.

Continue? (y/n): n

Goodbye
Enter an integer up to 10: -1
Invalid input.

Continue? (y/n): h

Derp
Enter an integer up to 10:

I want it to print the "Goodbye" and "Derp" messages like it does, but then end the program afterwards. Can someone please help me correct it? Thank you.
Line 35 while (key = true) this is an assignment statement, not an equality statement. So this will allways evaluate to true. I would just go with this while (key)
Last edited on
Removing = true worked. Thanks so much.
Topic archived. No new replies allowed.