When I compile and run this code I can enter values to the array, and then terminate the loop by pressing n, which is OK. It will then print of what I entered and exit the program. But when I run the program again and enter more data, and then break the loop, it will only print of to me the input from the last time I executed the program, and anything after that is just a load of unreadable garbage. So it is not appending the next structure in the array like i want it to. The idea is that each time data is added it just saves it to the next empty structure. I want that break in there aswell so the user can quit when he/she wants to.
Is it something to do with the break statement? And Quzah, The code has now been sorted out, so I would be grateful if you could help me.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct database {
char prod[60];
float cal;
float carb;
float prot;
float fat;
};
static float getnum(void);
int main(void)
{
enum {
MAX = 10
};
struct database mainm[MAX];
char *p;
int j;
FILE *fp;
for (j = 0; j < MAX; j++) {
printf("\nAnother? (Y/N) ");
if(getch() == 'n') break;
(void) system("cls");
printf("\nEnter the product: ");
(void) fflush(stdout);
(void) fgets(mainm[j].prod, 60, stdin);
if ((p = strchr(mainm[j].prod, '\n')) != NULL) {
*p = '\0';
}
(void) system("cls");
printf("\nNow Please Enter The Nutritional Information: ");
(void) fflush(stdout);
printf("\nEnter cal: ");
(void) fflush(stdout);
mainm[j].cal = getnum();
printf("\nEnter carb: ");
(void) fflush(stdout);
mainm[j].carb = getnum();
printf("\nEnter protein: ");
(void) fflush(stdout);
mainm[j].prot = getnum();
printf("\nEnter fat: ");
(void) fflush(stdout);
mainm[j].fat = getnum();
}
(void) system("cls");
fp = fopen("output.dat", "ab");
if (fp == NULL) {
perror("Unable to open file");
exit(EXIT_FAILURE);
}
fwrite(mainm, sizeof(struct database), MAX, fp);
if (fclose(fp) != 0) {
perror("fclose");
exit(EXIT_FAILURE);
}
fp = fopen("output.dat", "rb");
if (fp == NULL) {
perror("Unable to open file");
exit(EXIT_FAILURE);
}
fread(mainm, sizeof(struct database), MAX, fp);
if (fclose(fp) != 0) {
perror("fclose");
exit(EXIT_FAILURE);
}
for (j = 0; j < MAX; j++) {
(void) system("cls");
printf
("\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
printf
("\n\nThe nutritional values for item #%d (%s) are: \n\n",
j + 1, mainm[j].prod);
printf("CALORIES - %.2f\n", mainm[j].cal);
printf("CARBOHYDRATES - %.2f\n", mainm[j].carb);
printf("PROTEIN - %.2f\n", mainm[j].prot);
printf("FAT - %.2f\n\n", mainm[j].prot);
printf
("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
printf("\n\nPress return for next product...\n");
(void) getchar();
}
return 0;
}
static float getnum(void)
{
enum {
BUFMAX = 12
};
char *p;
char buf[BUFMAX] = { (char) 0 };
float num;
(void) fgets(buf, BUFMAX, stdin);
if ((p = strchr(buf, '\n')) != NULL) {
*p = '\0';
}
if ((sscanf(buf, "%f", &num)) != 1) {
(void) puts("Error: call to sscanf() failed.");
exit(EXIT_FAILURE);
} else {
return num;
}
}