Spina97
Spina97

Reputation: 199

NetBeans exit error code value -1.073.741.819 in C

I have a script on NetBeans that asks for user input. Because the "console type" internal terminal was giving me an error I changed the console to standard output.

It works with a common simple code but when I try to run it on my actual code it returns the error "RUN FAILED (exit value -1.073.741.819, total time: 79ms)" right after it appears the message before the input.

Here is the code:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
    int product, price, n_product, sum, num, canti, total;
    printf("What's the number of products? ");
    scanf( "%d", &n_product );
    num=1;
    sum=0;
    while(num<=n_product) {
      printf("What was the product? ");
      scanf( "%s", product );
      printf("What was the unit price? ");
      scanf( "%d", &price );
      printf("How much did you buy? ");
      scanf( "%d", &canti );
      total=price*canti;
      sum=sum+total;
      num++;
     }

    printf("Total is %n", sum);
    return (EXIT_SUCCESS);
}

I have searched the error but no results appear so I don't really know what is wrong with it.

There isn't a single variable on the code which is not declared. Since it stops right after the first printf I have considered that the error may be on the scanf but I don't know.

EDIT:

I changed the code as follows:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
    int price, n_product, sum, num, canti, total;
    char product[20];
    printf("What's the number of products? ");
    scanf( "%d", &n_product );
    num=1;
    sum=0;
    while(num<=n_product) {
      printf("What was the product? ");
      scanf( "%19s", product );
      printf("What was the unit price? ");
      scanf( "%d", &price );
      printf("How much did you buy? ");
      scanf( "%d", &canti );
      total=price*canti;
      sum=sum+total;
      num++;
     }

    printf("Total is %d", sum);
    return (EXIT_SUCCESS);
}

Now external terminal gives me no error but I can't input at all and just let me close it

Upvotes: 1

Views: 256

Answers (2)

Spina97
Spina97

Reputation: 199

So it seems that NetBeans is kinda broken for absolutely no reason and all I needed to do was to try this code on another file:

#include <stdio.h>
int main(void) {
    int product,num,sum, price, canti, total;
    char name[20];
    puts("What's the number of products? ");
    scanf( "%d", &product );
    num=1;
    sum=0;

    while(num<=product) {
        puts("What's the product name? ");
        scanf("%19s", name);
        puts("What's the unit price? ");
        scanf( "%d", &price );
        puts("How much did you buy? ");
        scanf( "%d", &canti );
        total=price*canti;
        sum=sum + total;
        num++;
    }

    printf("We've bought %d", sum);
}

Still can't understand why but that is how I fixed it. And no, it doesn't work on the same file I was trying to edit, I had to make a new one so it would work.

Upvotes: 1

dbush
dbush

Reputation: 225787

The product variable is declared as an int, however when you attempt to input a value for it you do this:

scanf( "%s", product );

This would be correct if product was defined as a character array, however it is not. This instead takes the current value of product (which is unknown because it is uninitialized) and passes it to scanf, which interprets it as a pointer to a character array. This invokes undefined behavior, which in this case manifests as a crash.

Change the type of product to a character array, and modify the call to scanf to tell it the size of that array:

char product[20];
...
scanf("%19s", product);

That's the first problem. The second one is here:

printf("Total is %n", sum);

The %n format specifier is used to store the total number of characters printed so far and expects a int * argument. Because you instead pass in an int, this causes the same problem as before.

Since you presumably want to print the value of sum, you should instead use the %d format specifier:

printf("Total is %d", sum);

Upvotes: 1

Related Questions