Thread: Can someone please look at this

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    51

    Question printf %f help

    Why will this code not print the %f values?


    Code:
    #include<stdio.h>
    #include<conio.h>
    
    struct database{
           char prod[30];
           float amount[5];
           float cal[5];
           float prot[5];
           float carb[5];
           float fat[5];
           };
           
           struct database main_m[200];
           struct database side[200];
           struct database sweet[200];
           
           int i;
           
           
           int main()
           
           {
               
               for (i = 0; i < 4; i++)
               {
                   printf("\nEnter product name for main %d:", i);
                   scanf("%s", main_m[i].prod);
                   
                   printf("\nEnter amount of product in grams for %d:", i);
                   scanf("%f", main_m[i].amount);
                   
                   printf("Enter calorie amount for %d:", i);
                   scanf("%f", main_m[i].cal);
                   
                   printf("Enter protien value in grams for %d", i);
                   scanf("%f", main_m[i].prot);
                   
                   printf("Enter Carbohydrate value in grams for %d:", i);
                   scanf("%f", main_m[i].carb);
                   
                   printf("Enter fat content in grams for %d:", i);
                   scanf("%f", main_m[i].fat);
                   }
                   
                   FILE *fp = fopen("output.dat", "wb");
                   fwrite(main_m, 1, sizeof(struct database) * 200, fp);
                   
                   FILE *fpp = fopen("output.dat", "rb");
                   fread (main_m, 1, sizeof(struct database) * 200, fpp); 
    
                   for (i = 1; i < 5; i++) {
                    printf("%s\n", main_m[i].prod);
                    printf("%f\n", main_m[i].amount);
                    printf("%f\n", main_m[i].cal);
                    printf("%f\n", main_m[i].prot);
                    printf("%f\n", main_m[i].carb);
                    printf("%f\n", main_m[i].fat);
                    getch();
                    
                    }
        
                     
                     }
    Last edited by voodoo3182; 08-04-2005 at 03:56 PM. Reason: wrong title

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Spot some of the many differences:
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    struct database
    {
       char prod[30];
       float amount[5];
       float cal[5];
       float prot[5];
       float carb[5];
       float fat[5];
    };
    
    struct database main_m[200];
    
    int main()
    {
       FILE *fp;
       int i;
       for ( i = 0; i < 5; i++ )
       {
          printf("\nEnter product name for main %d:", i);
          scanf("%s", main_m[0].prod);
    
          printf("\nEnter amount of product in grams for %d:", i);
          scanf("%f", &main_m[0].amount[i]);
    
          printf("Enter calorie amount for %d:", i);
          scanf("%f", &main_m[0].cal[i]);
    
          printf("Enter protien value in grams for %d", i);
          scanf("%f", &main_m[0].prot[i]);
    
          printf("Enter Carbohydrate value in grams for %d:", i);
          scanf("%f", &main_m[0].carb[i]);
    
          printf("Enter fat content in grams for %d:", i);
          scanf("%f", &main_m[0].fat[i]);
       }
    
       fp = fopen("output.dat", "wb");
       if ( fp )
       {
          fwrite(main_m, sizeof *main_m, sizeof main_m / sizeof *main_m, fp);
          fclose(fp);
          fp = fopen("output.dat", "rb");
          if ( fp )
          {
             fread (main_m, sizeof *main_m, sizeof main_m / sizeof *main_m, fp);
             for ( i = 0; i < 5; i++ )
             {
                printf("%s\n", main_m[0].prod);
                printf("%f\n", main_m[0].amount[i]);
                printf("%f\n", main_m[0].cal[i]);
                printf("%f\n", main_m[0].prot[i]);
                printf("%f\n", main_m[0].carb[i]);
                printf("%f\n", main_m[0].fat[i]);
                getchar();
             }
             fclose(fp);
          }
       }
       return 0;
    }
    Even at that there's much more to do.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    Thankyou, I will look through it. By the way, Im just learning.

Popular pages Recent additions subscribe to a feed