displaying barcode problem

hey i have work through every function all ready and i think i almost done, but when i run my code the 10 displaying barcode for zipcode are the same i can not figure out right now please help me! i am almost done!!


#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int createZipcode();
int extract(int zip, int location);
int correction(int zipcode);
void displaycode(int digit);
void displaybarcode(int digit, int location);

void displaybarcode(int zipcode, int location)
{
printf(" |");
for (location = 0; location <=5; location++)
{
displaycode(extract(zipcode,1));
}

printf("|\n");



}

void displaycode(int zipcode)
{
switch (extract(zipcode,1))
{
case 0:
printf("||:::");
break;
case 1:
printf(":::||");
break;
case 2:
printf("::|:|");
break;
case 3:
printf("::||:");
break;
case 4:
printf(":|::|");
break;
case 5:
printf(":|:|:");
break;
case 6:
printf(":||::");
break;
case 7:
printf("|:::|");
break;
case 8:
printf("|::|:");
break;
case 9:
printf("|:|::");
break;
}

}

int createZipcode()
{
return 10000 + rand() % 99999;

}

int extract(int zip, int location)
{
while (location <= 4)
{

location++;
zip/=10;
}

return zip % 10;
}
int correction(int zipcode)
{
int sum = 0;
sum = extract(zipcode,1) + extract(zipcode,2)+ extract(zipcode,3) + extract(zipcode,4) +extract(zipcode,5);
return sum;
}




int main()
{


printf("zip code digit barcode\n");
{
int zipcode;
int digit;
int digit2;
int digit3;
int digit4;
int digit5;
int digit6;


for (int k = 0; k <= 9; k++)
{

zipcode = createZipcode();
digit = extract(zipcode, 1);
digit2 = extract(zipcode, 2);
digit3 = extract(zipcode, 3);
digit4 = extract(zipcode, 4);
digit5 = extract(zipcode, 5);
digit6 = 10 - (correction(zipcode))%10;

printf("%d ", zipcode);
printf("%d", digit6);
displaybarcode(zipcode,1);

}



}
}
First off, please use code tags when posting code:
http://www.cplusplus.com/forum/articles/42672/

Second: the digit 6 correction your doing is not needed if you change the while loop in extract() to check "location < 4" rather than "location <= 4".

Third:
1
2
3
4
5
6
7
8
9
10
void displaybarcode(int zipcode, int location)
{
	printf(" |");
	for (location = 0; location <=5; location++)
	{
		displaycode(extract(zipcode,1));
	}

	printf("|\n");
}

notice how you're calling displaycode(extract(zipcode,1)); with a hard-coded 1 for the location? This should be displaycode(extract(zipcode, location));

Fourth: displaycode() also uses a hardcoded location parameter for extract();

Fifth: main() could be cleaned up a bit by getting rid of all the digit2, digit3, variables and doing it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	printf( "zip code digit barcode\n" );
	int zipcode;
	int digit;

	zipcode = createZipcode();
	printf( "%d ", zipcode );

	for( int k = 0; k <= 9; k++ )
	{
		for( int i = 0; i < 5; i++ )
		{
			digit = extract( zipcode, i );
			printf( "%d", digit );
		}
		displaybarcode( zipcode, 1 );
	}
}
Last edited on
Topic archived. No new replies allowed.