console application question

alright so this little code is supposed to ask the users input to change a variable to a number. if they get the number right then they get a message. if they get it wrong they would get a different message. when i run the program and enter a number the program closes. this is a console application in visual studios

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>
using namespace std;
int main()
{
	int A = 0;
	int B = 5;
	if (B = A) {
		cout << "This is pretty cool isnt it?\n I think so!";

		cin.ignore();
	}
	else {
		cout << "If A equals 0 and B equals 5, make A equal 5 then: ";
		cin >> A;
		cout << "you entered:" << A;
	}
	if (A = 5) {
		cout << "Congrats you entered 5!";
	}
	else {
		cout << "Shucks, you entered " << A << "\n and that is incorrect!";
		cin.ignore();
	}

	
	return 0;
}
@lreyes53

You are using a single equals on lines 7 and 17, here they should be a double equals. Remember, a single equals sign, assigns to a variable, whereas, the double equals, is for comparing. So, on line 7, you are making B equal to A.

Lastly, your console closes because of return 0;, which ends your program. Add a cin >> A; to keep it open.

@whitenite1


thank you!
Topic archived. No new replies allowed.