Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,212 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,264 people online right now. Registration is fast and FREE... Join Now!




.::STRUCTURES PROBLEM::.

 
Reply to this topicStart new topic

.::STRUCTURES PROBLEM::., The two programs provided are based in using structures but am having

dreambaff0
22 May, 2008 - 01:05 AM
Post #1

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 22

Hi there... I have being asked to prepare a program that will store and display relevant information about a local Rally race. The program below allows the user to add the details of 5 competitors and add, for each one, their 5 stage times.
CODE

//  RALLY'S DATA INSERTER

//This program will enable you to enter vital information
//to calculate various findings of a local rally competition

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
//Those are the required libraries

#define MAX_comps 5
//This line defines the maximum number of competitors
//and can be modified to suite the number required

struct comp_details
{
char comp_name[16];
char comp_surname[16];
char car_registration[9];
char car_make[11];
char car_model[11];
char car_enginesize[6];
int stagetime_min[6];
int stagetime_sec[6];
}comp_details[MAX_comps];
//Here the structure comp_details is defined
//comp_details contains all the information relevant to each competitor

FILE *fp;
int file_open(char *filename,char *mode)
{
    int retrieve = 0;

    if((fp = fopen(filename,mode))== NULL)
    {
        printf("The file %s could not be opened.  Please try again.");
    }
    else
    {
        retrieve = 1;
        printf("The file %s was succesfully opened.",filename);
    }
    return retrieve;
}
//This section is used to open the file

int input_details (void)
{
    int comp_number=0;
    char buff[20];
    int stageno=0;
    int i;

    printf("\n\nEnter competitors's details.\n");
    printf("____________________________________________________");
  
      do{
           printf("\n____________________________________________________");
          
           printf("\nEnter competitor number%2d's name.\n",comp_number+1);
           gets(comp_details[comp_number].comp_name);
    
             printf("\nEnter competitor number%2d's surname.\n",comp_number+1);
             gets(comp_details[comp_number].comp_surname);
    
           printf("\nEnter competitor number%2d's registration number.\n",comp_number+1);
           gets(comp_details[comp_number].car_registration);
    
           printf("\nEnter competitor number%2d's car's make.\n",comp_number+1);
           gets(comp_details[comp_number].car_make);
    
           printf("\nEnter competitor number%2d's car's model.\n",comp_number+1);
           gets(comp_details[comp_number].car_model);
            
           printf("\nEnter competitor number%2d's car's engine size.\n",comp_number+1);
           gets(comp_details[comp_number].car_enginesize);
          
             printf("\n____________________________________________________________________\n");
             comp_number++;
                       
           for(i=0;i<1;i++)
           {
              for(stageno=0;stageno<=4;stageno++)
              {     
              printf("\nEnter stage (%1d) time in minutes.",stageno+1);
              comp_details[comp_number].stagetime_min[stageno]=atoi(gets(buff));
          
              printf("\nEnter stage (%1d) time in seconds.",stageno+1);
              comp_details[comp_number].stagetime_sec[stageno]=atoi(gets(buff));
              
              };
           };
    }while(comp_number<MAX_comps);

return (comp_number);
}
//This section deals with inputting data in the file

void write_file (int m)
{
  int l;
  char buff[20];
  char filename [20];
  int open =0;
  
  printf("\n\nEnter the name that you want to appoint to the ");
  printf("\nfile that will contain all the relevant information.\n\n");

  gets(filename);
  open = file_open(filename,"wb");

  if (open)
  {
      printf("\n\nUpdating data content ....\n");
      fwrite(&m,sizeof(int),1,fp);

      printf("\n\nUpdating complete ....\n");
      
      for (l=0; l<=m; l++)
      {
         fwrite(&comp_details[l],sizeof(comp_details[l]),1,fp);
      }
      
  fclose(fp);
  printf("\n\nData stored and file %s closed\n" , filename);
  }
  else
  {
      printf("Could not save data !");
  }
}
//This function writes data in the file and closes it

void main (void)
{
    int i=0;
    int number; //This is the number of competitors
    
    printf("\nWelcome to RALLY'S DATA INSERTER!");
    printf("\nInsert disk in appropriate USB or Floppy Drive, slot");

    number = input_details();
    printf("\nData added succesfully\n\n.");

    write_file(number);
    printf("\nThanks for using RALLY'S DATA INSERTER!");
    printf("\nYou can now remove you disk or USB drive from the appropriate slot.");

    printf("\n\nPress any key to quit. To view result open RALLY'S DATA VIEWER.");
    getch();
}
//This is the main function


There is nothing wrong with this program but the problem is, that is to be used with another application which will display the information added in the file... The code of the other program is shown below:
CODE

//  Rally - Output program

//This program will enable you to display the data and information
//inserted in the previous program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
//Those are the required libraries

#define MAX_comps 5
//This line defines the maximum number of competitors
//and can be modified to suite the number required

FILE *fp;

//This section is used to open the file

struct comp_details
{
char comp_name[16];
char comp_surname[16];
char car_registration[9];
char car_make[11];
char car_model[11];
char car_enginesize[6];
int stagetime_min[6];
int stagetime_sec[6];
int totaltime[6];
}comp_details[MAX_comps];
//Here the structure comp_details is defined
//comp_details contains all the information relevant to each competitor


int file_open(char *filename,char *mode)
{
    int retrieve = 0;

    if((fp = fopen(filename,mode))== NULL)
    {
        printf("The file %s could not be opened.  Please try again.");
    }
    else
    {
        retrieve = 1;
        printf("The file %s was succesfully opened.",filename);
    }
    return retrieve;
}

int get_data(void)
{
    int open = 0;
    int i,m;
    char filename[20];
    
    printf("\nEnter the name appointed to the file.\n");
    gets(filename);
    open = file_open(filename,"rb");
    if(open)
    {
        fread(&m,sizeof(int),1,fp);
        for(i=0;i<m;i++)
            fread(&comp_details[i],sizeof(comp_details[i]),1,fp);
        fclose(fp);
    }
    printf("\nData retrieved succesfully...\n");
    getch();
    return(m);
}

void report(int m)
{
  int i;
  int stageno=0;
  
  /* Print Headings  */
  printf("\n\n\nCompetitor|Reg. No.|Stage 1|Stage 2|Stage 3|Stage 4|Stage 5|");
  printf("\n============================================================");
  for (i=0;i<m;i++)
  {        
          printf(" \n");
          printf("%-15s|",comp_details[i].comp_surname);
          printf("%-10s|",comp_details[i].car_registration);
  }
  
}


void main (void)
{
   int size;
   int i,m;
   int rep;

   printf("Insert the disk or USB drive, containing the file,");
   printf("\nin the appropriate slot.\n");
   m = get_data();

   report(m);
   printf("\n\n\n\n Thank-you,  Remove your data disk now\n");
   getch();
}

This program should be able to display the data inputted in teh previous program, but I can't display all the correct information because it will display it whether incorrectly or not completely any suggestion of what I need to change in the report function? Or did I miss something in the input program?

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 01:21PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month