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