-
Debug Help w/ <fstream>
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
*/
-
Code:
hotelbase* hotelinfo[7];
You aren't initializing the array before you use it, causing you to access memory that's not yours and Windows is slapping you across the head for it.
-
I've tried a couple of methods for array initialization.. Based on this post, the first thing I tried was using a constructor:
Code:
struct hotelbase
{
hotelbase(void);
char ReservationNum[6];
char Origin[30];
char Destination[30];
char Price[15];
char HotelName[30];
};
Code:
hotelbase::hotelbase(void)
{
for(int i=0; i<6; i++)
ReservationNum[i] = '0';
for(int i=0; i<15; i++)
Price[i] = '0';
for(int i=0; i<30; i++)
{
Origin[i] = '0';
Destination[i] = '0';
HotelName[i] = '0';
}
}
The only other way I can think of to initialize hotelbase* hotelinfo[7] would be to use a ton of for loops... which would initialize each attribute.. of each element (the overhead == enormous)
Code:
for(int i=0; i<7; i++)
{
for(int j=0; j<6; j++)
hotelinfo[i]->ReservationNum[j] = '0';
for(int j=0; j<15; j++)
hotelinfo[i]->Price[j] = '0';
for(int j=0; j<30; j++)
{
hotelinfo[i]->Origin[j] = '0';
hotelinfo[i]->Destination[j] = '0';
hotelinfo[i]->HotelName [j] = '0';
}
}
Using either method, the program still compiles.. but locks up at runtime.. what am i missing/overlooking..??
-
Hi,
In this line:
strcpy(hotelinfo[i]->ReservationNum, temp);
the part:
hotelinfo[i]-->ReservationNum
says to take the pointer on the left and get the hotelbase object that it points to. However, the pointers in hotelinfo[] don't point to any hotelinfo objects. You might want to consider creating some hotelinfo objects and assigning their addresses to the pointers.
That is what jverkoey meant when he said you weren't initializing the array.
-
Your constructor method would work fine on its own if you were dealing with an array of hotelbase objects but you're not, you're using an array of pointers to type hotelbase. You need to allocate some memory with new to each array member, i.e.:
Code:
//Create an array of "hotelbase" pointers
hotelbase* hotelinfo[7];
// Allocate memory for those pointers
for( int loop = 0; loop < 7; ++loop )
hotelinfo[loop] = new hotelbase;
-
cool technique.. other than declaring new nodes for a linked list.. I have never had to explicitly allocate memory like that :) (is this C or C++ !! :P )
learning == good