Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

HELP - why runtime error in entering floating point value

Posted on 2002-06-14
10
Medium Priority
?
234 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 80 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 46

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 opening and writing to files 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.

876 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