c language split my zip code

my project want me to create 10 random zip code and convert the sum of each digit in zip code this is my code and when i run it, it only showing the last digit of each zip code instead, i want showing every single digit, and i can not find what is wrong with my coding here it is:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

int createZipcode();
int extract(int zip, int location);
int correction(int zipcode);

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

}

int extract(int zip, int location)
{
{

while (location <= 4)

location++;
zip / 10;
}




return zip % 10;
}


int main()
{
int zipcode;
int digit;
int digit2;

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

zipcode = createZipcode();
digit = extract(zipcode, 2);

printf("%d\n", zipcode);
printf("%d\n", digit);
;

}



}
several problems:
1
2
3
4
5
6
7
8
9
int extract(int zip, int location)
{
{ 

while (location <= 4)

location++;
zip / 10;
}


for starters, the line "zip / 10;" doesn't compile. I'm guessing you meant zip/=10;

also, the braces around the while() loop are strange. Here's how your code looks properly indented:
1
2
3
4
5
6
7
8
9
int extract(int zip, int location)
{
	{
		while (location <= 4)
			location++;
		zip /= 10;
	}
	return zip % 10;
}


what I think you meant is:
1
2
3
4
5
6
7
8
9
int extract(int zip, int location)
{
	while (location <= 4)
	{
		location++;
		zip /= 10;
	}
	return zip % 10;
}


if I'm correct, then what the extract function does is just returns the value of the 2nd digit in the passed zip. Is this what you intended? If so, there is still a small logical error in this function. the while( location <= 4 ) needs to just be while( location < 4 ) otherwise it'll count too high and all your results will be off by 1 position.

Now the reason it is only showing one digit in the output is because there is no loop in main() which extracts each digit. you're only calling extract() once, to get the 2nd digit in this case.
Last edited on
thanks that works every well!
Topic archived. No new replies allowed.