2

My little program below shall take 5 numbers from the user, store them into an array of integers and use a function to print them out. Sincerly it doesn't work and my output is always "00000". I can't find a mistake, so i would be glad about any advice. Thanks.

#include <stdio.h>

void printarray(int intarray[], int n)
{
    int i;
    for(i = 0; i < n; i ++)
    {
        printf("%d", intarray[i]);
    }
}

int main ()    
{
    const int n = 5;
    int temp = 0;
    int i;
    int intarray [n];
    char check;

    printf("Please type in your numbers!\n");

    for(i = 0; i < n; i ++)
    {
        printf("");
            scanf("&d", &temp);         
        intarray[i] = temp;


        getchar();
        getchar();

    }

    printf("Do you want to print them out? (yes/no): ");
        scanf("%c", &check);

        if (check == 'y')
            printarray(intarray, n);


    getchar();
    getchar();

    return 0;
}

1 Answer 1

5

You want %d, not &d, in your scanf() format string. This bug is the kind that is very easy to identify by using a debugger - I recommend learning how to use whichever one works best with the rest of your development system.

Compiling with more warnings turned on would probably have detected this one, too. Something like "too many arguments for format".

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you but now i have another problem. The input is not printed out by the function. So even the "00000" is missing now.
@Ordo, the rest of your program looks fine. I tested it here and things look good (I did have to take out one of each set of doubled-up getchar() calls because I'm not on a windows machine). Did you try setting up a debugger?
There is a debugger included in MS Visual Studio but it's not showing me any problem. I will try another debugger.
@Ordo look at the auto and local variables in a step by step debugging session in MSVS, you will quickly see what is wrong.
@Soulseekah: the OP's code uses a variable length array, which was introduced with C99 and requires a C99 compiler to translate. C89 requires array lengths to be specified with a constant expression (i.e., something that can be evaluated at compile time).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.