Solved

HELP - why runtime error in entering floating point value

Posted on 2002-06-14
10
226 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Goodbye, so long 10 91
How to create project in Eclipse ? 1 184
How to creat good software interface ? 1 57
What is atomic operation? 6 16
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now