Hi there,
I need some help with a program I am writing:
Write a program that will allow the user to enter the results of TEST1 for (Computing Systems and Programming) subject and store them in a data file named TEST1.txt. The data file “TEST1.txt” should have the following format:
And this is what I've done so far:Code:Matric No. Name Marks
I have been trying to fix the code so you may or may not need the commented lines. What I would like to know is what's wrong and why it can't produce the required output.Code:#include <stdio.h> #include <stdlib.h> //#include <cstring.h> #define FILENAME "data.txt" int main (void) { int num; printf ("How many students would you like to place record for?\n"); scanf ("%d", &num); char name; int matric; double marks; FILE *datatab; datatab = fopen (FILENAME,"a"); if (datatab == NULL) printf ("Error opening File\n"); else { printf ("num = %d\n",num); int i = 0; while (i<num) { printf ("i = %d\n",i); printf ("Please Enter name(Less than 50 Characters:\n"); fflush(stdin); scanf("%s", &name); fflush(stdin); printf ("Please Enter Matric Number:\n"); scanf("%d", &matric); fflush(stdin); printf ("Please Enter Marks Obtained (via Cheating or otherwise):\n"); scanf("%f", &marks); fflush(stdin); fprintf (datatab,"%d %s %.2f\n", matric,name,marks); // fflush(stdin); i++; } fclose(datatab); } system("pause"); return 0; }
Matric number ex: 0987674 (I want the zero to be displayed)
Name ex: Anything (Just the first name would be enough).
Marks ex: 67.89 (Maximum of 2 digit decimal)



LinkBack URL
About LinkBacks


