Why is this C-loop not working as expected?

int main()
{
    int t, i;
    int *nums;
    scanf("%d", &t);
    nums = malloc(t * sizeof(int));
    for(i = 0; i < t; i++)
    {
        scanf("%d", &nums[i]);          
    }
    printf("hey");
}

      

It hangs for some reason, waiting for more input! Any ideas?

+3


source to share


3 answers


This code is correct, except that you are not freeing your memory (not a big problem for this simple code) and you are not checking scanf

for errors, which may be the cause of your problem.

A more efficient implementation with error checking and proper memory handling is described below:



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

int main()
{
    int t, i, ret;
    int *nums;

    ret = scanf("%d", &t);          
    if(ret != 1)
    {
        /* something wrong */
    }

    nums = malloc(t * sizeof(int));
    if(nums==NULL)
    {
        /* memory error */
    }

    for(i = 0; i < t; i++)
    {
        ret = scanf("%d", &nums[i]);          
        if(ret != 1)
        {
            /* something wrong */
        }

    }
    free(nums);
    printf("hey");

    return 0;
}

      

+1


source


Just guess here ...

But I assume you ran it and entered:

4
1234

      

For example, you put 4 and then 1234 and it freezes. Well, that would be because 1234 is the first number, not 4 different numbers, so it waits for the second number. You need to hit enter or some such separator between each number.

Try using this set of inputs:

4
1234
29
4
5

      

You should receive:



hey

      

Programmatically, you should check the return values ​​from your function calls. Make sure malloc doesn't return zero. Make sure scanf returns the number of inputs you expected to read. Add to printouts to make sure the values ​​it reads are what you expected / wanted to read.

EDIT: Guessing that you have a typo in the program not showing up here. eg:

scanf("%s", &t);

      

Or you get hey and you just don't see it.

[ov@MARVIN sotest]$ ./a.out
5 5 4 3 23 1
hey[ov@MARVIN sotest]$ 

      

See "hey" hiding in my prompt because you're missing the "\ n" line in the printout?

+1


source


Don't forget to flush when you're done.

In the OP's chat comments "No ... BTW, if I add printf to a loop that prints the current input, it prints everything but the last ...". This meant that the problem was in the final exit.

The following sends the print data. But stdout

usually buffered. The actual withdrawal may not occur until after a while. The output is flushed when 1) the output contains the end of the line '\n'

, 2) fflush()

is called, 3) the program ends. I'm surprised the output didn't appear when the program ended, but maybe the OP's true code is different from the post.

printf("hey");

      

Change to

printf("hey\n");

      

Or @Cool Guy

printf("hey");
fflush(stdout);

      

BTW: This is also outlined in @Diversity's answer .


Note. It's always a good idea to check the result.scanf()

// scanf("%d", &t)
if (1 != scanf("%d", &t)) Handle_BadInput_or_EOF();

      

+1


source







All Articles