Alright i am trying to read from a file so that it opens into the array temp_array. the idea is to have the data previously added to the file open into the array so i can search it and sort it this is giving me alot of stress it is an assignment. i have looked at other posts but they have not really helped me with this problem. my read from file displays all right now doesnt place it in the array.

# include <stdio.h>
# include <string.h>
# include <fstream>
# include <iostream>
# include <cstring>


using namespace std;

//This program was written by Rikaad 'K'rul' Laloo
//The baddest computer techy out there.
//It demonstrates the basic idea behind a DBMS using Arrays and 
//the struct definition for creating a composite data type.
//Data;

typedef struct {
        char hname[20];
        char rname[20];
        char IDNum[20];
        char race [20];
        float time;
} Horses;

const int num_of_Horses=5;              //number of  records required        

int Horses_num=0;                            //holds the current position in the Array

//*************************************************************
void writetofile(Horses temp_array[]) {
      ofstream HorsesFile ("HorsesFile.txt", ios::app|ios::out);
      if (HorsesFile.is_open()){
       
         HorsesFile << temp_array[Horses_num-1].hname << endl;
         HorsesFile << temp_array[Horses_num-1].rname << endl;
         HorsesFile << temp_array[Horses_num-1].IDNum << endl;  
         HorsesFile << temp_array[Horses_num-1].race << endl;    
         HorsesFile << temp_array[Horses_num-1].time << endl;
         HorsesFile.close();

     
         }
      else cout << "Error opening file.";
      }//writing to a file
//*************************************************
void addHorses(Horses temp_array[num_of_Horses]) {
       //array called Horses
     if (Horses_num<num_of_Horses){
         printf("\nAdd Horses %d",Horses_num+1,"\n");
         printf("\nEnter Horses name:");
         scanf("%s",temp_array[Horses_num].hname);
         printf("\nEnter Jockey:");
         scanf("%s",temp_array[Horses_num].rname);
         printf("\nEnter ID number:");
         scanf("%s",temp_array[Horses_num].IDNum);
         printf("\nEnter race:");
         scanf("%s",temp_array[Horses_num].race);
         printf("\nEnter time:");
         scanf("%f",&temp_array[Horses_num].time);
         Horses_num += 1;
         writetofile(temp_array);
         
     }
     else {
          printf("\nArray Full. No more  can be added at this time.");
          }
}//end of addHorsess     

//*************************************************
void find_horse (Horses temp_array[num_of_Horses])
{
        //array called Horses
     char searchhname[20];
     bool found;
     int diff;
     int i;
     
     printf("\n**********FIND Horses**********") ;
     printf("\nEnter Horse name:");
     scanf( "%s", searchhname);
     found = false;
     
     for(i=0; i<num_of_Horses; i++) {
              diff = strcmp(searchhname,temp_array[i].hname);
              if ( diff == 0) {
                   found = true;
                   printf("\nHorses: %s", temp_array[i].hname);
                   printf("\nJockey: %s", temp_array[i].rname);
                   printf("\nID Number: %s", temp_array[i].IDNum);
                   printf("\nrace: %s", temp_array[i].race);
                   printf("\ntime: %f", &temp_array[i].time);
              }
     }
     
     if (found == false) {
               printf("\nNo Matches Found.");
     }
     
}//end of find

//*************************************************

void displayAll (Horses temp_array[num_of_Horses]) {
     
     int i;
     printf("\n******DISPLAY ALL******");
     printf("\n");
     for(i=0; i<Horses_num; i++){
              printf("\nHorses: %s", temp_array[i].hname);
              printf("\nJockey  : %s", temp_array[i].rname);
              printf("\nID Number: %s", temp_array[i].IDNum);
              printf("\nrace: %s", temp_array[i].race);
              printf("\ntime: %f", &temp_array[i].time);
              printf("\n*******************");
     }
}//end of displayAll
//*************************************************
void bubble_sort(Horses temp_array[num_of_Horses]) {

int i, j, flag = 1; 
      char temphname[20];
      char temprname[20];
      char tempIDNum[20];
      char temprace [20];
      float temptime;             

      for(i = 1; (i <= num_of_Horses) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (num_of_Horses -1); j++)
         {
               if (temp_array[j+1].time > temp_array[j].time)      
              { 
                    strcpy(temphname, temp_array[j].hname);             
                    strcpy(temp_array[j].hname, temp_array[j+1].hname);
                    strcpy(temp_array[j+1].hname, temphname);
                    
                    strcpy(temprname, temp_array[j].rname);             
                    strcpy(temp_array[j].rname, temp_array[j+1].rname);
                    strcpy(temp_array[j+1].rname, temprname);
                    
                    strcpy(tempIDNum, temp_array[j].IDNum);             
                    strcpy(temp_array[j].IDNum, temp_array[j+1].IDNum);
                    strcpy(temp_array[j+1].IDNum, tempIDNum);
                    
                    strcpy(temprace, temp_array[j].race);             
                    strcpy(temp_array[j].race, temp_array[j+1].race);
                    strcpy(temp_array[j+1].race, temprace);
                    
                                      
                    temptime = temp_array[j].time;             
                    temp_array[j].time = temp_array[j+1].time;
                    temp_array[j+1].time = temptime;
                    flag = 1;              
               }
          }
     }
     return;   
}

//********************************************
void initialize_file(Horses temp_array[]) {
 string line;
        

 ifstream HorsesFile ("HorsesFile.txt");

 

 if (HorsesFile.is_open())

 {

 while (! HorsesFile.eof() )

 {

 getline (HorsesFile,line);

         
         cout<<line<<endl;
 
 
 }

 HorsesFile.close();

 }


 else cout << "Unable to open file"; 

 


}


//*************************************************

int main (void){
    Horses Horses_array[num_of_Horses];
    char choice;
    
         
    printf("\n");
    printf("\n******MAIN MENU******");
    printf("\n Choose One of the Following:");
    printf("\n (1) Add Horses");
    printf("\n (2) Find Horses");
    printf("\n (3) Display All");
    printf("\n (4) Sort");
    printf("\n (5) Initialize Files");
    printf("\n (6) Exit\n");
    
    scanf("%c", &choice);
    
    if (choice=='6'){
       exit(0);
    }
    
   
    while(choice != '6') {
                 switch (choice) {
                        
                 case '1':
                          addHorses (Horses_array);
                          break;
                 case '2':
                          find_horse (Horses_array);
                          break;
                 case '3':
                          displayAll (Horses_array);
                          break;
                 case '4':
                          bubble_sort (Horses_array);
                          break;
                 case '5':
                          initialize_file(Horses_array);
                          break;
                 case '6':
                          exit (0);
                          break;
                 }
                 printf("\n******MAIN MENU******");
                 printf("\n Choose One of the Following:");
                 printf("\n (1) Add Horses");
                 printf("\n (2) Find Horses");
                 printf("\n (3) Display All");
                 printf("\n (4) Sort");
                 printf("\n (5) Initialize Files");
                 printf("\n (6) Exit\n");
                 cin >> choice;
                 
   
    }


   


   
   
   
   
   
    return 0;
    
}//end of main

Recommended Answers

All 18 Replies

Why did you put all that C code inside a C++ program? I realize C++ supports FILE* and associated functions, but if you want to write c++ then do it the right way using fstreams and other c++ classes.

well the person teaching me gave me those codes im not familiar with c++

I don't understand your problem.

Doesn't the 500 line code work?

Is it a design question?

In a general context in c++,
you never have to write a method that does the sorting.
It's a good exercise though.

well my read from file isnt working the way i want it i dont know how to get it to read the contents of the file into the array so that i can display and search for files right now it just reads the file

void initialize_file(Horses temp_array[]) {
 string line;
 ifstream HorsesFile ("HorsesFile.txt");
 if (HorsesFile.is_open()) {
   while (! HorsesFile.eof() ) {
     getline (HorsesFile,line);
     cout<<line<<endl;
 }
 HorsesFile.close();
 }
 else cout << "Unable to open file"; 
}

What is the format of your input file,
I cant see how the file is structured.
Maybe you should tell us.

Like monkey_king said, we have to know the structure of your file to help you with this. For example,

Your Horses structure:

typedef struct {
        char hname[20];
        char rname[20];
        char IDNum[20];
        char race [20];
        float time;
} Horses;

Sample File Structure (in HorsesFile.txt ):

My Horse,My R Name,My Horse ID,My Horse Race,20.54
My Second Horse,My Second R Name,My Horse ID two,My Horse Race,34.22

The above is a CSV file (Comma Separated Values file, where the comma is the delimiter)

So, to read the information in the file and parse the values into the structure, we would need to customize the code, based on the file structure.

the structur is

Horse Name
Jockey Name
id number
race
time


yeh guys im new to this sorry bout all that

please post some of the actual contents of the file

Hennesy
Emile.Ramsammy
002
Kentucky
35
Maggygirl
Jak.Ury
003
Kentuky
32
SilverFang
Joey.Stevens
001
Kentucky
37

lines 173-180 of the code you posted are wrong and incomplete. Here is one way to read the file:

int i = 0;
while( HorsesFile >> temp_array[i].hname >> temp_array[i].rname >>
    temp_array[i].IDNum >> temp_array[i].race >> temp_array[i].time)
{
         ++i;
}

THINKING

void getInput(ifstream& fin, int year[], int month[], int rain_in_month[])
{
int i = 0;
fin >> year >> month >> rain_in_month;
i++;
cout << year << month << rain_in_month << endl;

return;

THAT MIGHT HELP....I'M NOT SURE....

commented: NO NEED TO SHOUT. -2

thx ill try it

THINKING

THAT MIGHT HELP....I'M NOT SURE....

Huh?? The code you posted as nothing to do with this thread. Maybe you meant to post in another thread?

well it compile without a prblem but it i dont think it loads to the array cuase when i choose display option it doesnt display previous data

post code -- we can't see your monitor.

this is the read from file

void initialize_file(Horses temp_array[]) {

string line;

ifstream HorsesFile ("HorsesFile.txt");

if (HorsesFile.is_open())

{

int i = 0;
while( HorsesFile >> temp_array[i].hname >> temp_array[i].rname >>
    temp_array[i].IDNum >> temp_array[i].race >> temp_array[i].time)
{
         ++i;
}



HorsesFile.close();

}

else cout << "Unable to open file";

}

That part of your program works fine. Now the problem is in the function displayAll () shown below. The value of Horses_num in the loop is 0, so the loop doesn't to anything. What is that variable supposed to be? The number of horses read from the file? If yes, then you need to set it before initialize_file() returns.

void displayAll (Horses temp_array[num_of_Horses]) {
     
     int i;
     printf("\n******DISPLAY ALL******");
     printf("\n");
     for(i=0; i<Horses_num; i++){
void readfromfile (horses temp_array[]) {
    
     ifstream infile ("filename.txt");
     

     
     if (!infile){
                        
                        cout << "Error ! File did not open!\n";
                        }
     else{
          for ( int q = 0 ; q < num_of_horses; q++)
          {
              infile >>  temp_array[q].fieldname;
              infile >> temp_array[q].fieldname;
              infile >> temp_array[q].fieldname;
              infile >> temp_array[q].fieldname;
              infile >> temp_array[q].fieldname;  
              }
              }
         
        
         infile.close();
         }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.