Thread: Fscanning Results

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    32

    Fscanning Results

    Im writiing a program that will load in a outside txt file of exams results for candidates in the form
    Name rslt1 rslt2 reslt 3 rslt 4
    with there being up to 40 candidates and a min of 4.
    program then must calculate mean score for exm standard deviation for each exm , and each candidates avrg, eg
    Exm1 exm2 ex3 Exm4 Avrg
    Name rslt rslt rslt rslt avrg rslt
    name " " " " "
    Mean eavrg eavrg eavrg eavrg
    S.D Esd esd esd esd.
    im thinking of creating a structure for putting each candidates reuslts in and then making a array of structures. But im not sure how to make this work for a variable number of candidates and how to also display the above table. im guessing by making my arary of structs bigger to add on the avrgs and assging a value for the avrgs to part of the array?
    Any suggestions would be greatly apprciated

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    To make it work for a variable number of candidates do you mean like a random number? If so you could just dynamically allocate memory for structures. If you use an array of say 10 structures that limits the amount of candidates. Like have 1 structure, and once your done reading into it, allocate memory for another with malloc and so on. To print them I would make a function that does just that, prints the values of the variables in the structure. Goodluck.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    Just did a first attempt at coding the reading in and storing in the array, but when i attempt to display the array i just a number or if i ask for [0]'s value which should be the first name i also get back a number , i tried changing the %d in the printf at the end to %c but then i just get back one letter.
    heres the code
    Code:
    #include <stdio.h>
    
    typedef struct
    	{
    	 char Name[20];
    	 int Exm1;
    	 int Exm2;
    	 int Exm3;
    	 int Exm4;
           } data;
    
    
    
    
    int main (void)
    {     FILE *my_in, *my_out;
    	int i;
    	data Dat[41];
    
    	if ((my_in = fopen ("datafile.txt","rt")) == NULL)
    	{
    		printf("\nError opening input file, program exiting\n");
    		return(1);
    	}
    
    	printf("\nProgram reading data from file\n");
    	for(i=0; i<41; i++){
    	while (fscanf(my_in, "%c %d %d %d %d",&Dat[i].Name,&Dat[i].Exm1,&Dat[i].Exm2,&Dat[i].Exm3,&Dat[i].Exm4) !=EOF)
    	{
    	printf(".");
    	}
    	}
    	printf("\n%d",Dat [0]);
    
    	return(0);
    
    	}
    Last edited by mr_spanky202; 04-10-2003 at 08:53 AM.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>fscanf(my_in, "%c %d %d %d %d",&Dat[i].Name
    .Name is a string, not a char as you're trying to get it, change it to %s and remove the & operator from Dat[i].Name. And check out the returf value of the fscanf() function, instead EOF, you should pass the number of parameters you're trying to get, in this case 5.
    Last edited by Vber; 04-10-2003 at 09:05 AM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    it doesnt seem to store things propely, changing to all for the string allows it a print a name but only the last name its read and the int values read in for the exm results are wrong

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    you don't need to change all the things, change just the name, from %c to %s. Post your last code here.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    something like this?
    Code:
    #include <stdio.h>
    
    typedef struct
    	{
    	 char Name[20];
    	 int Exm1;
    	 int Exm2;
    	 int Exm3;
    	 int Exm4;
           } data;
    
    
    
    
    int main (void)
    {     FILE *my_in, *my_out;
    	int i, j;
    	data Dat[41];
    
    	if ((my_in = fopen ("datafile.txt","rt")) == NULL)
    	{
    		printf("\nError opening input file, program exiting\n");
    		return(1);
    	}
    
    	printf("\nProgram reading data from file\n");
    	for ( i = 0 ; i < 41 ; i++ ) {
      if ( fscanf(my_in, "%s %d %d %d %d", Dat[i].Name, &Dat[i].Exm1, &Dat[i].Exm2, &Dat[i].Exm3, &Dat[i].Exm4) == EOF) break;
    }
    
    	{
    	printf(".");
    	}
    
    	printf("\n%d",Dat);
    
    
    	return(0);
    
    	}

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    32

    Angry

    Can anyone suggest how to do this? I cant get it to scan the results in and display them correctly and thats just the start! i can prob figure how to calc the means etc when they are stored in the array, but how can i input these calc values back into the array so i can display everything in one array so it displays as a nice table.sorry to sound desperate

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    Thanks a lot, now to do the calcs, decided to do the figures being displayed using printf above and below the array. ok basically to caculate the avrg of a canddates score i have no idea but to caculate the mean mark in each exam am i right in thinking
    Code:
    sum += Exm1;
    count = i;
    avrg = sum/count
    would this be ok ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incorrect results from fmod()?
    By karlthetruth in forum C Programming
    Replies: 4
    Last Post: 04-11-2008, 09:12 AM
  2. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  3. Results of March Monthly Contest
    By PJYelton in forum Contests Board
    Replies: 23
    Last Post: 04-17-2005, 09:46 AM
  4. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM
  5. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM