program acting weird

I wrote this code and have been working on it the whole week.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream> 
#include <iomanip>
#include <string> 
#include <conio.h> 
using namespace std; 

int main()
{ 
	int total = 0; 
	char ch[5];
	int check; 

	cout << "Please input your 5 digit zip code: "; 
	
	
	for (int i=0; i<5; i++)
{
	ch[i] = getch();
	cout << ch[i];
	total = total + (ch[i] - '0'); 

}

	cout << endl;
	check = total % 10;
	if (check <= 5 ) 
		
	{ 
		cout << "Check Digit:" << check << endl;
	} 
	else if ( check > 5) 
	{ 
		cout << "Check Digit:" << 10 - check << endl; 
	}

	cout << "Your bar code is:" << endl;

	cout << "|";

	for (int i=0; i<5; i++)
{
	if (ch[i] = 9) 
	{
		cout << "|:|::";
	}
	else if (ch[i] = 8)
	{
		cout << "|::|:";
	}
	else if (ch[i] = 7)
	{
		cout << "|:::|";
	}
	else if (ch[i] = 6)
	{
		cout << ":||::";
	}
	else if (ch[i] = 5)
	{
		cout << ":|:|:";
	}
	else if (ch[i] = 4)
	{
		cout << ":|::|";
	}
	else if (ch[i] = 3) 
	{
		cout << "::||:";
	}
	else if (ch[i] = 2)
	{
		cout << "::|:|";
	}
	else if (ch[i] = 1)
	{
		cout << ":::||";
	}
	else if (ch[i] = 0)
	{
		cout << "||:::";
	}


}

	if (check == 5)
	{
		cout << ":|:|:";
	}
	else if (check == 4)
	{
		cout << ":|::|";
	}
	else if (check == 3) 
	{
		cout << "::||:";
	}
	else if (check == 2)
	{
		cout << "::|:|";
	}
	else if (check == 1)
	{
		cout << ":::||";
	}
	else if (check == 0)
	{
		cout << "||:::";
	}

	cout << "|" << endl;

	
return 0;
}


I'm struggling with getting the for loop to read each individual character entered by the user. For example when I enter 95014 it keeps reading 9 and doesn't read the next characters. So it ends up outputting the same part for 9. I was told to use == instead of = but when I do use == nothing displays. If you can help me what I'm doing wrong I would appreciate it a lot. Also another thing is when I enter 95014 the check digit is 1 but it doesn't output the info for 1 instead it outputs the info for 9. Which is strange because when I input 4445 the check digit also equals 1 and it does display the correct output. Can you also explain why this is happening?
Last edited on
Modern Man19

Change if (ch[i] = 9) and each of the following ones, down to the 0, to if (ch[i] == '9')
The array, ch[] is a char array, not int, plus you are assigning the the single =, not comparing, as with a double equal. ==.
thank you dude it worked out perfectly fine with all types of numbers
Topic archived. No new replies allowed.