#Can someone tell me what's wrong with my code?

8 messages · Page 1 of 1 (latest)

wanton vault
#

I'm following C a modern approach and I'm stuck on the "upc.c" exercise. My code looks as follows:

#include <stdio.h>

int main(void)
{
int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5,
first_sum, second_sum, total ;

printf("Enter the first (single) digit: ") ;
scanf("%ld", &d);
printf("Enter first group of five digits: ") ;
scanf("%ld%ld%ld%ld%ld", &i1, &i2, &i3, &i4, &i5) ;
printf("Enter second group of five digits: ") ;
scanf("%ld%ld%ld%ld%ld", &j1,&j2,&j3,&j4,&j5 ) ;

first_sum = d + i2 + i4 + j1 + j3 + j5 ;
second_sum = i1 + i3 + i5 + j2 + j4 ;
total = 3 * first_sum + second_sum ;

printf("Check digit: %d\n", 9-((total - 1) % 10)) ;

return 0;

}

when the input is respectively: 0, 13800 and 15173, the output (check digit) should be 5 but when I try it on my machine the output isn't even a consistent number (but usually around 14/16), so what did I do wrong?

tawny kilnBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

wanton vault
#

^might be a very silly mistake I haven’t had the time for C in ages

vivid swift
#

I'm not that familiar with scanf, but I think you might need a space at the front of the format string for most of the calls

#

Just to match the newline character leftover

wanton vault
tawny kilnBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.