[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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);
     

}
0
Knight_G
Asked:
Knight_G
  • 3
  • 2
  • 2
  • +3
1 Solution
 
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
 
Knight_GAuthor Commented:
scanf("%f", &(rec[i].fic.price));
this statement result the same error
0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
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
 
TriskelionCommented:
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
 
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

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now