sum in a loop

Hello,

I am new to c++ and I am creating a program for postal barcode. Enter a zip, it will convert it to the "high, low" bars in the array. I am having trouble with my check digit (add up the digits in the zip, subtract from the next highest multiple of 10, ex. 90514 = 19 = 20-19= 1, and add the 1 to end of the barcode. Below is what I have, can anyone help me with adding the sum of the digits?

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
 

int main(){

	string zips [10] = {"||:::",":::||","::|:|","::||:",":|::|",":|:|:",":||::","|:::|","|::|:","|:|::"};

string zip; 
string barcode = "|";
int x,sum,check,digit;

	cout << "Please enter a 5 digit zip code " << endl; 

cin >> zip; 


for(int i=0; i<zip.length(); i++){
	
		x = zip[i]-48;

		barcode+=zips[x];

		sum = i;
		sum += x;
	
}
 
check = 10-(sum % 10);  
 
zips[check];

barcode+=zips[check]+="|";

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

cout << barcode << endl; 
You need to set 'sum' to 0 before the loop, and don't set it to 'i' inside the loop.

btw, line 29 does nothing.
Last edited on
Line 22: What's the point of this line? You are initializing sum each time through the loop. You want to initialize sum BEFORE you enter the loop.

Line 29: This line does nothing.

Line 31: You're going to modify zips. zips should be const
thank you guys so much! Easy fix, sorry for such a n00b question. Next step i have to make this into a function outside the main, and have it called in the main. I will try to work this out myself. Thanks again!!! :)
Topic archived. No new replies allowed.