Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
The men's long jump competition at the 2012 Summer Olympics in London, United Kingdom was held at the Olympic Stadium on 3–4 August.

The data of this competition are written to a file named jump.txt which is attached with the assignment.

This file contains the first names, last names, nationality and distances jumped by athletes in this competition.

Write a C program to do the following: (1) read all data from jump.txt; (2) keeps the data into an array of structure and (3) prints out the names, nationalities, and the distances of the jumpers who exceeded a distance given by the user.

Description of the data file jump.txt file

The first line in the file contains titles. Each other line in the file represents a data of a jumper so that white spaces separate the first name, last name, nationality, and the distance as shown below.

This is jump.txt
FirstName LastName Nationality Result

Aleksandr Menkov Russia 8.09
Aleksandr Petrov Russia 7.89
Alyn Camara Germany 7.72





A sample of the running the program



The data are loaded successfully.

Please enter the distance threshold: 8.10



The jumpers exceeded 8.10 m are:
Marquise Goodwin United States 8.11
Mauro Vinicius da Silva Brazil 8.11 2




C++
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
   char ch, file_name[25];     // HERE WE ARE READING THE FILE NAME AND SETTING THE STRING AMOUNT FOR THE FILE NAME
   FILE *aj;                   // WE ARE READING THE FILE NAME
 
   printf("Please Enter The Name Of The File You Wish To See \n"); // PRINTING THE FILE WHICH WE CALL
   gets(file_name);                                                // CALLS THE FILE
 
   aj = fopen(file_name,"r"); // WE ARE IN THE READING MODE
 
   if( aj == NULL )
   {
      perror("Error! Opening The File, Please Try Again :( \n");     // WE ARE WRITING THE ERROR MESSAGE FOR THE FILE IF NOT CORRECT FILE
      exit(EXIT_FAILURE);
   }
 
   printf("The Data From The %s File Is :\n", file_name);
 
   while( ( ch = fgetc(aj) ) != EOF )
      printf("%c",ch);
 
   fclose(aj);
   return 0;
}
Posted
Updated 2-Feb-16 4:02am
v3
Comments
Member 12301021 1-Feb-16 18:10pm    
BASICALLY THERE IS A JUMP.TXT FILE WITH THE DATA ABOVE

Aleksandr Menkov Russia 8.09
Aleksandr Petrov Russia 7.89
Alyn Camara Germany 7.72

SO WE ARE TRYING TO READ THE DATA
Dave Kreskowiak 1-Feb-16 18:47pm    
First delete the solution you posted to your own question, which isn't a solution at all.

Next, what's the question? You never said what you're having a problem with.

From just looking at your code and not knowing what your problem is, I would guess that you are having problems with the while loop:
C++
char ch;

while ((ch = fgetc(aj)) != EOF )
    printf ("%c",ch);

Note that fgets returns a value of type int and not char. That is so, in order to be able to return the EOF value. In your chase you are casting the return value to type char (the type of ch), which might cause the later comparison with EOF to fail.

Solution: Declare ch as an int.

After that you can continue working on the actual task.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Feb-16 19:02pm    
5ed.
—SA
nv3 2-Feb-16 4:57am    
Thank you, Sergey.
Do not read one character at a time, as it serves no purpose; your code is merely copying each character to the console. Use fgets to read each line of the file into a character array. Then split the line into its parts (use strtok), and create an array of the athletes (hint; use an arroy of structs, one for each athlete). Once you have your array of structs you can search for all the ones that meet your test criteria.
 
Share this answer
 
Comments
Member 12301021 2-Feb-16 8:44am    
SO THIS IS WHAT I HAVE SO FAR

I HAVE THE FILE CALLED WHICH JUST READS THE FILE

AFTER THAT I NEED TO CREATE A SEARCH FUNCTION WHICH A USER INPUTS FOR THE DISTANCE THRESHOLD

EXAMPLE 7.92

AND ALSO PRINT OUT DISTANCE THRESHOLD MORE THAN 7.92
C++
#include<stdio.h>     // contains defination of printf and scanf
#include<string.h>    // contains defenation of all string functions
#include <stdlib.h>

typedef struct {
	//define members (variables)
	
	char firstName[15];
	char lastName[15]; 
	char nationalities[20];
	float result; 

}Athletes_In_This_Competition;

char *gets(char*); // prototype for gets function 
// to avoid the compilier warning 

// define the function printCustomer
 void printInformation(Athletes_In_This_Competition c){
	printf("\n The distances of the jumpers who exceeded a distance given by the user %.2f m are: \n", c.result);
	printf("\n%s %s\n From: %s\n The distance threshold: %.2f\n", c.firstName,c.lastName,c.nationalities,c.result);
	
}

// define the search function 

 
int main(int argc, char* argv[])
{
   char ch, file_name[25];     // HERE WE ARE READING THE FILE NAME AND SETTING THE STRING AMOUNT FOR THE FILE NAME
   FILE *aj;                   // WE ARE READING THE FILE NAME
 
   printf("Please Enter The Name Of The File You Wish To See \n"); // PRINTING THE FILE WHICH WE CALL
   gets(file_name);                                                // CALLS THE FILE
 
   aj = fopen(file_name,"r"); // WE ARE IN THE READING MODE
 
   if( aj == NULL )
   {
      perror("Error! Opening The File, Please Try Again :( \n");     // WE ARE WRITING THE ERROR MESSAGE FOR THE FILE IF NOT CORRECT FILE
      exit(EXIT_FAILURE);
   }
 
   printf("The Data From The %s File Is :\n", file_name);
 
   while( ( ch = fgetc(aj) ) != EOF )
      printf("%c",ch);
 
   fclose(aj);
   return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 2-Feb-16 9:05am    
This is not a solution. Please delete it, edit your original question and put the additional information there.
Member 12301021 2-Feb-16 10:04am    
i think that might help explain my situation more and its my first time using c as i mentioned
ridoy 2-Feb-16 10:13am    
Is it your solution of your question?
Member 12301021 2-Feb-16 10:57am    
this is the solutin so far

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900