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:
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.