• C

HELP - why runtime error in entering floating point value

can some1 help me to debug pls? thanx
why there will be a runtime error?



#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
     union book {
          struct fiction {
               char title[20];
               char author[20];
               double price;
          } fic;

          struct nonfiction{
               char title[20];
               char author[20];
               char publisher[20];
          } nonfic;
     };

     union book rec[5];

     int i;
     int type;
     FILE *fptr;

     for (i=0; i<5; i++)
     {
          do {
               printf("(1)Fiction (2)Non-fiction > ");
               scanf("%d", &type);
          } while (type!=1 && type!=2);

          fflush(stdin);

          if (type==1)
          {
               printf("Book title: ");
               gets(rec[i].fic.title);

               printf("Author : ");
               gets(rec[i].fic.author);

               printf("Price (RM): ");
               scanf("%f", &rec[i].fic.price);  //runtime error here
          }
          else
          {
               printf("Book title: ");
               gets(rec[i].nonfic.title);

               printf("Author : ");
               gets(rec[i].nonfic.author);

               printf("Publisher: ");
               gets(rec[i].nonfic.publisher);
          }

          printf("\n");

     }

     
     if ((fptr = fopen("7-book.dat", "wb"))==NULL)
          perror("Error"), exit(1);

     fwrite(&rec, sizeof(union book), 1, fptr);
     printf("File saved.\n");
     
     fclose(fptr);
     

}
Knight_GAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
TriskelionConnect With a Mentor Commented:
I got (and have always gotten) suspicious results with scanf(), so I...

/*
     1) Removed "scanf()"
     2) Moved structures to higher scope and made typedef
     3) Added in reception and printing of publisher
     4) Declared temporary string
     5) Made certain variables static
     6) Initialized non-static variables at creation
     7) Turned "if" into "switch/case" to allow for more book types
     8) Changed the fwrite to write all 5 records
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef     struct fiction
{
     char          title[20];
     char          author[20];
     char          publisher[20];
     double     price;
}     strucFic;

typedef     struct nonfiction
{
     char          title[20];
     char          author[20];
     char          publisher[20];
}     strucNonFic;

typedef     union     book
{
     strucFic          fic;
     strucNonFic     nonfic;
};

void main(void)
{
     static     union book rec[5];
     auto          int     i=0;
     auto          int     type=0;
     static     FILE*     fptr=NULL;
     static     char     strTemp[1024]={0};

     for (i=0; i<5; i++)
     {
          do {
               printf("(1)Fiction (2)Non-fiction > ");
               gets(strTemp);
               type=atoi(strTemp);
               } while (type!=1 && type!=2);

          switch(type) /*Allow for more than two types of books*/
               {
               case 1:
                    {
                    printf("Book title: ");
                    gets(strTemp);
                    strncpy(rec[i].fic.title, strTemp, sizeof(rec[i].fic.title));

                    printf("Author : ");
                    gets(strTemp);
                    strncpy(rec[i].fic.author, strTemp, sizeof(rec[i].fic.author));

                    printf("Publisher : ");
                    gets(strTemp);
                    strncpy(rec[i].fic.publisher, strTemp, sizeof(rec[i].fic.publisher));

                    printf("Price (RM): ");
                    gets(strTemp);
                    rec[i].fic.price=atof(strTemp);
                    break;
                    }

               case 2:
                    {
                    printf("Book title: ");
                    gets(strTemp);
                    strncpy(rec[i].nonfic.title, strTemp, sizeof(rec[i].nonfic.title));

                    printf("Author : ");
                    gets(strTemp);
                    strncpy(rec[i].nonfic.author, strTemp, sizeof(rec[i].nonfic.author));

                    printf("Publisher: ");
                    gets(strTemp);
                    strncpy(rec[i].nonfic.publisher, strTemp, sizeof(rec[i].nonfic.publisher));
                    break;
                    }

               default:
                    break;
               }
    }

     if (NULL == (fptr = fopen("7-book.dat", "wb")))
          {
          perror("Error");
          exit(1);
          }

     fwrite(&rec, sizeof(rec), 1, fptr);
     printf("File saved.\n");

     fclose(fptr);
}
0
 
dimitryCommented:
You can try to set:
scanf("%f", &(rec[i].fic.price));
in order to be sure that you taking address from the filed you want.
 
0
 
TriskelionCommented:
I changed price to a float and added price to the nonfic structure and it worked.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Knight_GAuthor Commented:
scanf("%f", &(rec[i].fic.price));
this statement result the same error
0
 
jos010697Commented:
A few things:

- change 'void main()' to 'int main()'
- don't fflush(stdin), its behaviour is undefined
- if you want to read a float number, use "%f",
- otherwise if you're reading a double, use "%lf".

kind regards
0
 
Knight_GAuthor Commented:
ok, it is a way to solve them, but why there was a runtime error? i try to write more statements for the program eg fseek,fread(), then it is ok, no error... amazing
0
 
jos010697Commented:
Did you apply the changes I suggested above? If so, what were the results?

kind regards
0
 
Knight_GAuthor Commented:
i tried method sugggested by Triskelion, its ok.

i tried yours but same thig happen.
0
 
mattjsimpsCommented:
try dropping the &, or change it to a *. i really hate scanf. it can exhibit some really strange behaviour, especially under vc 6. one of these changes might make a difference. it might help to know what the runtime error is?
0
 
Kent OlsenData Warehouse Architect / DBACommented:
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Accept Triskelion's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Kent (Kdo)
EE Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.