Some help with a program.

This is a discussion on Some help with a program. within the C Programming forums, part of the General Programming Boards category; Hi there, I need some help with a program I am writing: Write a program that will allow the user ...

  1. #1
    K12
    K12 is offline
    Registered User
    Join Date
    Oct 2009
    Posts
    8

    Some help with a program.

    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:

    Code:
    Matric No.            Name		      Marks
    And this is what I've done so far:
    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;    
    }
    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.

    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)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try compiling with warnings on. That'd point you in the right direction.
    1. fflush wasn't designed to work on input streams.
    2. name is only a single character.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    K12
    K12 is offline
    Registered User
    Join Date
    Oct 2009
    Posts
    8
    How do I enable warnings in Dev C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21