I tried making a simple program on codeblocks that gets max 50 numbers and puts them into an array. It stops if you type -1 or 0. After that, it prints out all the numbers you typed in.
Codeblock says everything is correct and that there are no errors. When I run the program, nothing appears: the screen is black.
int main()
{
    int i;
    int usernumbers[i];
    const N = 50;
    printf("write an integer (-1 or 0 to stop)\n");
    scanf("%d", usernumbers[i]);
    while(usernumbers[i] != -1 && usernumbers[i] != 0){
        for( i=0; i<N; i++){
            printf("Next number: \n");
            scanf("%d", usernumbers[i]);
        }
    }
    printf("The numbers you entered are: \n");
    for(i=0; i<N; i++){
        printf("%d", usernumbers[i]);
    }
    return 0;
}
Process returned -1073741571 (0xC00000FD)
Always initialize your vars before use.
int main(int argc, char* argv[])
{
    const int N = 50;
    // initialize your array with zero's
    int userNumbers[N] = { 0 };
    printf("write an integer (-1 or 0 to stop)\n");
    // initialize the var with 0.
    int index = 0;
    // in this case I don't initialize because I will initialize below, on scanf_s
    int number;
    // Check against scanf_s result and if index is less then N, we don't want index out of bounds
    while (scanf_s("%d", &number) && index < N)
    {
        // the numbers you defined to stop
        if (number == -1 || number == 0)
            break;
        // put the number into array and add 1 to index
        userNumbers[index++] = number;
    }
    printf("The numbers you entered are: \n");
    for (int i = 0; i < N; i++)
    {
        printf("%d\n", userNumbers[i]);
    }
    return 0;
}
The only thing different here is your loop. Don't make sense. You have a while with a for inside. Your while will run one time and your for will run N times, so to your check if the entered number is -1 or 0 will not be handled as expected.
The following proposed code:
usernumbers[]and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
#define N 50
int main( void )
{
    int usernumbers[ N ] = {0};
    printf("write an integer (-1 or 0 to stop)\n");
    for( int i = 0; i < N; i++ )
    {
        if( scanf( "%d", &usernumbers[i] ) != 1 )
        {
            fprintf( stderr, "scanf failed to read number\n" );
            exit( EXIT_FAILURE );
        }
        if( usernumbers[i] == -1 || usernumbers[i] == 0)
        {
            break;
        }
        printf("Next number: \n");
    }
    printf("The numbers you entered are: \n");
    for( int i=0; i<N; i++ )
    {
        if( usernumbers[i] == -1 || usernumbers[i] == 0 )
        {
            break;
        }
        printf( "%d ", usernumbers[i] );
    }
    return 0;
}
The definition of usernumbersis wrong.
Also I'm surprised the line const N = 50; does not trigger a compiler error.
You should do something like :
#define N 50
int usernumbers[N];
User contributions licensed under CC BY-SA 3.0