0

After running the code with the following inputs, a runtime error occurs:

id : 123
name : stackoverflow
quantity : 123
price : 123

I need help to solve this.

Previously, I put the ampersand/& at:

fprintf(fp, "%d %s %d %d\n\n", a.id, a.name, a.quantity, a.price);

And a funny number came out:

2686724 stackoverflow 2686688 2686720

Code:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <conio.h>

struct product
{
    int quantity, reorder, i;
    char name[20];
    float price, id;
};


int main()
{

    FILE * fp;  

    int i=0;
    struct product a;
    system("cls");

    char checker;

    int counter;
    do
    {
        fp = fopen("addproduct.txt","a+t");
        system("cls");

        printf("Enter product ID : ");
        scanf(" %d", &a.id);

        printf("Enter product name : ");
        scanf(" %s", a.name);

        printf("Enter product quantity : ");
        scanf(" %d", &a.quantity);

        printf("Enter product price : ");
        scanf(" %d", &a.price);

        fprintf(fp, "%d %s %d %d\n\n", a.id, a.name, a.quantity, a.price);

        printf("Record saved!\n\n");

        fclose(fp);

        printf("Do you want to enter new product? Y / N : ");

        scanf(" %c", &checker);
        checker = toupper(checker);
        i++;
        system("cls");
    }
    while(checker=='Y');

    return(0);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Alif Khair
  • 87
  • 1
  • 1
  • 7
  • 1
    Don't use the & address of operator in your calls to fprintf() - fprintf(fp, "%d %s %d %d\n\n", a.id, a.name, a.quantity, a.price); – Nunchy Aug 14 '16 at 03:44
  • I already change it to : fprintf(fp, "%d %s %d %d\n\n", a.id, a.name, a.quantity, a.price); But, run time error occurs. :( – Alif Khair Aug 14 '16 at 03:52
  • first, `float price, id ;` : `id` change type to `int`. – BLUEPIXY Aug 14 '16 at 03:53
  • `fp = ` --> `FILE *fp = ` and use `%f` for `price` – BLUEPIXY Aug 14 '16 at 03:59
  • sorry. miss out at FILE *fp; already put it back. i change the type to int. same, funny number. 1123418112 stackoverflow 1123418112 1123418112 – Alif Khair Aug 14 '16 at 04:03
  • I think that already operate correctly if you modify all the pointed out portion. – BLUEPIXY Aug 14 '16 at 04:09
  • @BLUEPIXY Alright. I got it running. You rock man ! 1 more thing. What is a+t exactly means ? Can I change to fp = fopen("addproduct.txt","a"); ? – Alif Khair Aug 14 '16 at 04:14
  • yes, you can use only `"a"`. `t` means text mode(default), `+` meant can write and **read**. Your program does not require even `+` and `t`. – BLUEPIXY Aug 14 '16 at 04:20

3 Answers3

1

It is because & points to the reference of the variable. Instead please try printing a.id.

fprintf (fp, "%d%s %d %d\n\n", a.id, a.name, a.quantity, a.price)
v.coder
  • 1,822
  • 2
  • 15
  • 24
  • I already change it to : fprintf(fp, "%d %s %d %d\n\n", a.id, a.name, a.quantity, a.price); But, run time error occurs. :( – Alif Khair Aug 14 '16 at 03:52
0

you have declared id,price as float data type . use %f instead of %d in the scanf statement.

A.Varma
  • 10
  • 3
0

There is a simple mistake. Here, the main issue is a collision between integer and float. You can change the 'id' & 'price' to 'int' from 'float' and it will solve the issue! Also, you can modify %d with %f when you are taking float input and output for price and id. I don't know why you declared 'id' as 'float' :|