What's wrong with this program? It doesn't run properly

-3

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)

c
asked on Stack Overflow Sep 9, 2019 by angel • edited Sep 9, 2019 by harper

3 Answers

0

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.

answered on Stack Overflow Sep 9, 2019 by Kevin Kouketsu • edited Sep 9, 2019 by Kevin Kouketsu
0

The following proposed code:

  1. cleanly compiles
  2. performs the desired functionality
  3. properly checks for and handles I/O errors
  4. corrects the problems with the OPs posted code
  5. does not print numbers that are not initialized by user input
  6. inserts appropriate horizontal and vertical spacing for readability and ease of understanding
  7. properly declares and initializes the array 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;
}
answered on Stack Overflow Sep 9, 2019 by user3629249
-1

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];
answered on Stack Overflow Sep 9, 2019 by Guillaume Petitjean • edited Sep 9, 2019 by Guillaume Petitjean

User contributions licensed under CC BY-SA 3.0