C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-03-2009, 08:56 PM   #1
K12
Registered User
 
Join Date: Oct 2009
Posts: 2
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)
K12 is offline   Reply With Quote
Old 10-03-2009, 11:49 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,260
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 10-03-2009, 11:51 PM   #3
K12
Registered User
 
Join Date: Oct 2009
Posts: 2
How do I enable warnings in Dev C++?
K12 is offline   Reply With Quote
Reply

Tags
data, file, marks, students, text

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue with program that's calling a function and has a loop tigerfansince84 C++ Programming 9 11-12-2008 01:38 PM
Need help with a program, theres something in it for you engstudent363 C Programming 1 02-29-2008 01:41 PM
This is a simple program.. Help me please I think it has an error.. lesrhac03 C Programming 4 02-21-2008 10:39 AM
My program, anyhelp @licomb C Programming 14 08-14-2001 10:04 PM


All times are GMT -6. The time now is 01:47 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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