I call upon ye' fellow programmers for help with this code.. which compiles but locks up at runtime. It's a simple program.. a program that will read in a list of menial travel information and displays it to screen.
Code:
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
//(Global) Function Prototype(s)
void clear_array(char array[], int size);
struct hotelbase
{
char ReservationNum[6];
char Origin[30];
char Destination[30];
char Price[15];
char HotelName[30];
};
int main()
{
//Create an array of "hotelbase" pointers
hotelbase* hotelinfo[7];
//temp array for strcpy()
char temp[30];
//Create object of type ifstream
ifstream infile;
infile.open("prototype.txt");
if(!infile.is_open())
{
cout<<"Error:Problem with opening the file 'prototype.txt'"<<endl;
}
else
{
for(int i=0;i<7;i++)
{
//Can't stream directly to a linked list attribute
//So I stream into an intermediate temp array and
//then I use the assignment operator "=" or in this
//case, I use strcpy() to handle array copying/assignment
infile >> temp;
strcpy(hotelinfo[i]->ReservationNum, temp);
clear_array(temp, 6);
infile >> temp;
strcpy(hotelinfo[i]->Origin, temp);
clear_array(temp, 30);
infile >> temp;
strcpy(hotelinfo[i]->Destination, temp);
clear_array(temp, 30);
infile >> temp;
strcpy(hotelinfo[i]->Price, temp);
clear_array(temp, 15);
infile >> temp;
strcpy(hotelinfo[i]->HotelName, temp);
clear_array(temp, 30);
}
}
if(!infile.eof())
{
cout<<"Error: There are still more data to be written";
}
else{
for(int i=0;i<7;i++)
{
//Be careful with "precedence" here (order of operations)
//Use parenthesis to properly dereferrence an array of pointers
// "*" has a lower precedence than "[]"
cout << "The Info of Plan" << " " << i << endl
<< *hotelinfo[i]->ReservationNum << endl
<< *hotelinfo[i]->Origin << endl
<< *hotelinfo[i]->Destination << endl
<< *hotelinfo[i]->Price << endl
<< *hotelinfo[i]->HotelName << endl
<< "____________________________" << endl;
}
//infile stream no-longer needed
infile.close();
}
return 0;
}
//Function Definition(s)
void clear_array(char array[], int size)
{
for(int i=0; i<size; i++)
array[i] = NULL;
}
//Copy/Paste this section.. and into a notepad file named, "prototype.txt"
/*
TA206 London Paris 3355.35 Ritz
GB503 London Rome 69.5 HolidayInn
KY349 Berlin London 103.56 Maryott
ME923 Edinburgh Madrid 635.2 Mercure
KO732 London LosAngeles 564.3 Renaissance
HT983 Moscow London 254.34 Hilton
*/