Solved

HELP - why runtime error in entering floating point value

Posted on 2002-06-14
10
230 Views
Last Modified: 2010-04-15
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
Comment
Question by:Knight_G
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 11

Expert Comment

by:dimitry
ID: 7078953
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
 
LVL 6

Expert Comment

by:Triskelion
ID: 7079488
I changed price to a float and added price to the nonfic structure and it worked.
0
 

Author Comment

by:Knight_G
ID: 7079875
scanf("%f", &(rec[i].fic.price));
this statement result the same error
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 4

Expert Comment

by:jos010697
ID: 7080273
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
 
LVL 6

Accepted Solution

by:
Triskelion earned 20 total points
ID: 7080372
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
 

Author Comment

by:Knight_G
ID: 7080980
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
 
LVL 4

Expert Comment

by:jos010697
ID: 7081272
Did you apply the changes I suggested above? If so, what were the results?

kind regards
0
 

Author Comment

by:Knight_G
ID: 7081291
i tried method sugggested by Triskelion, its ok.

i tried yours but same thig happen.
0
 
LVL 1

Expert Comment

by:mattjsimps
ID: 7121199
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
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9480254
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question