简体   繁体   中英

Why the last 'printf' function didn't execute?

I'm new to C, I'm confused why the last 'printf' function didn't execute. printf("%d:%.2d %cm, arriving at %d:%.2d %cm\n")

The outcome is 'Closest departure time is 9:45 pm, arriving at 11:58 pm' Why the outcome is not 'Closest departure time is 9:45 pm, arriving at 11:58 pm Closest departure time is 9:45 pm, arriving at 11:58 pm'

Now I realize it because of 'return 0' in the loop, but I don't know why.

Thanks for any help you can provide.

int i,
    user_time,
    hour,
    d_hour,
    d_minute,
    a_hour,
    a_minute,
    minute,
    dep[8] = {480, 583, 679, 767, 840, 945, 1140, 1305},
    arr[8] = {616, 712, 811, 900, 968, 1075, 1280, 1438};

printf("Enter a 24-hour time: ");
scanf("%d :%d", &hour, &minute);
user_time = hour * 60 + minute;

printf("Closest departure time is ");

for (i = 0; i < 7; i++) {
    if (user_time <= dep[i] + 
        (dep[i + 1] - dep[i]) / 2) {

        d_hour = dep[i] / 60 > 12 ? dep[i] / 60 - 12 : dep[i] / 60;
        d_minute = dep[i] % 60;
        a_hour = arr[i] / 60 > 12 ? arr[i] / 60 - 12 : arr[i] / 60;
        a_minute = arr[i] % 60;

        printf("%d:%.2d %c.m., arriving at %d:%.2d %c.m.\n", 
               d_hour, d_minute, dep[i] / 60 > 11 ? 'p' : 'a',
               a_hour, a_minute, arr[i] / 60 > 11 ? 'p' : 'a');
        return 0;
    }
}

d_hour = dep[7] / 60 > 12 ? dep[7] / 60 - 12 : dep[7] / 60;
d_minute = dep[7] % 60;
a_hour = arr[7] / 60 > 12 ? arr[7] / 60 - 12 : arr[7] / 60;
a_minute = arr[7] % 60;

printf("%d:%.2d %c.m., arriving at %d:%.2d %c.m.\n",
       d_hour, d_minute, dep[7] / 60 > 11 ? 'p' : 'a',
       a_hour, a_minute, arr[7] / 60 > 11 ? 'p' : 'a');
**d_hour** = dep[7] / 60 > 12 ? dep[7] / 60 - 12 : **dep[7] / 60**;
printf("**%d**:%.2d %c.m., arriving at %d:%.2d %c.m.\n",
d_hour, d_minute, dep[7] / 60 > 11 ? 'p' : 'a',
_hour, a_minute, arr[7] / 60 > 11 ? 'p' : 'a');

Your d_hour will have the result of dep[7] / 60 which results to float datatype. but i can able to see that printf is expecting %d which is integer data type. pls use data conversion or use '%f' to print the data.

The function exits from inside the for loop due to the return 0; , hence the last printf is never executed.

Added

If you do not know why the internal return skips the last print, you lack a basic knowledge about the language you use.

In C language, the return statement causes immediate termination of the function in which it is executed. In other words, it jumps to the closing brace with a specified return value, ie a result of the function.

If you want to terminate the loop but not exit the function, use break instead of return . Be aware though, that break breakes the 'closest' loop only—if you use it in a nested loop, like this:

for (i=0; i<10; i++)
{
    // do some stuff with i

    for (j=0; j<4; j++)
    {
        // do some stuff with j

        if (some_condition)
            break;              // <----- break here

        // do more stuff about j
    }

    // do more stuff about i
}
// the rest of the program

then the break inside the inner loop will bring the execution just past that loop's end, to the do more stuff about i point, not to the rest of the program .

Added

If you do understand how the return works but not why it was used there , then you lack a basic knowledge of your program's logic.

You seek a departure time that fits the user's needs, according to the input. For this you need to use each departure time and the next departure time. However, there is no next departure after the last one in the timetable, so you scan the table till one-but-last item.

Then you want to print the last entry from the timetable if none of earlier entries fits the user's input. If you find an appropriate departure in the loop, say at i==3 , then you print it and you needn't print the last entry. Then you can skip the rest of the function simply by returning from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM