Need help with my program

So, i wrote a program, that couts digit 2 amount in the number, but there is one problem with the if.

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
#include <iostream>
using namespace std;
int main()
{
	int a,b,c,d,e;				//Digits
	int x;						//Number
	int am;					// Amount of digit 2
	cout << "Type in your first number:	"<<endl;
	cin >> x;
	cout << "Your number digits are:	"<<endl;
	a = x / 10000;
	b = (x / 1000) % 10;
	c = (x / 100) % 10;
	d = (x / 10) % 10;
	e = x % 10;
	cout <<"1. "<<a<<endl;
	cout <<"2. "<<b<<endl;
	cout <<"3. "<<c<<endl;
	cout <<"4. "<<d<<endl;
	cout <<"4. "<<e<<endl;
	am = 0;
	if (a = 2)
		am= am + a;
	if (b = 2)
		am = am + b;
	if (c = 2)
		am = am + c;
	if (d = 2)
		am = am + d;
	if (e = 2)
		am = am + e;
	cout <<"Amount of digits 2:	"<<am<<endl;
	system("PAUSE");
	return 0;
}


The amount always keeps the same. 2.
What i've done wrong, anyone could help me please? Im a begginer, totally new in this, so i don't know what ive done wrong. Waiting for help, thanks.
Last edited on
1
2
3
4
5
6
7
8
9
10
if (a = 2)
		am= am + a;
	if (b = 2)
		am = am + b;
	if (c = 2)
		am = am + c;
	if (d = 2)
		am = am + d;
	if (e = 2)
		am = am + e;


Don't confuse = with ==. If you are testing for equality, you probably meant to use ==.
Thanks!
Topic archived. No new replies allowed.