Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Unusual Error, program works but there is a run time error

Question posted by: mwhit74 (Newbie) on July 21st, 2008 10:29 PM
here is the code:

// holidays.cpp
// Finds a holiday for a specified date from a list of holidays.

const int MAX_DATES = 60, // Max number of holidays in list
MAX_NAME_LEN = 81; // Max length of holiday name

#include <iostream>
#include <fstream>
using namespace::std;


// Definition of DayData type
struct DayData
{
int month, // Month / day of holiday
day;
char holiday[MAX_NAME_LEN]; // Name of holiday
};

// Function prototype
void findHoliday ( const DayData holidayList[], int listLength,
int month, int day, char holidayCopy[] );

void main ()
{
DayData holidayList[MAX_DATES]; // List of holidays
int count = 0, // Number of holidays in list
month, // Input month / day
day;
char holidayName[MAX_NAME_LEN]; // Name of selected holiday

// Open the designated file for input.
ifstream holidayFile("G:\\C++ Lab Hints\\Lab#12\\holidays.dat");
if (!holidayFile) {
cout << "Can NOT open file " << endl;
return;
}

// Read in the list of holidays.
while (holidayFile.good() && holidayFile >>
holidayList[count].month>> holidayList[count].day )
{
holidayFile.get(); // Remove blank after day from the stream
holidayFile.getline(holidayList[count].holiday,
MAX_NAME_LEN,'\n'); // Read holiday name
count++; // including spaces
}

// Close the file.
holidayFile.close();

// Prompt the user for the date of the desired hoilday.
cout << endl << "Enter the month and day for a holiday: ";
cin >> month >> day;

// Display the holiday (if any) for the requested date.
findHoliday(holidayList,count,month,day,holidayNam e);
if ( holidayName[0] != '\0' )
cout << holidayName << endl;
else
cout << "No holiday listed" << endl;

return;
}

//--------------------------------------------------------------------
// Insert your findHoliday() function here.
//--------------------------------------------------------------------
void findHoliday (const DayData holidayList[],int listLength,
int month,int day,char holidayCopy[])
{
int i;
int j;

for ( i = 0; i < listLength; i++ )
{
if ( holidayList[i].month == month && holidayList[i].day == day )
{
for ( j = 0; j < MAX_NAME_LEN ; j++ )
{
holidayCopy[j] = holidayList[i].holiday[j];
}
}
}
holidayCopy[j]= '\0';


here is the output:


Enter the month and day for a holiday: 1 11
Hostos Day (Puerto Rico)
Press any key to continue . . .

there is a .dat file that i get the input from linked to the program and the error is get is:

Run-Time Error Check # 2 stack around the variable 'holidayName' is corrupt

what do i need to do to correct this?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Laharl's Avatar
Laharl
Expert
706 Posts
July 22nd, 2008
01:10 AM
#2

Re: Unusual Error, program works but there is a run time error
Why are you not using std::string to do this? Were I to guess, one of your names is too long somewhere and your stack got corrupted.

Reply
gpraghuram's Avatar
gpraghuram
Expert
1,089 Posts
July 22nd, 2008
01:29 AM
#3

Re: Unusual Error, program works but there is a run time error
Check whether u are exceeding the size of the array
DayData holidayList[MAX_DATES];
Before pushing value to the array check the count and then push the element.

Raghu

Reply
weaknessforcats's Avatar
weaknessforcats
Moderator
4,615 Posts
July 22nd, 2008
04:07 PM
#4

Re: Unusual Error, program works but there is a run time error
holidayCopy is not an array. You defined as char holidayCopy[] but never allocated memory for the copy.

Reply
osama alqaise's Avatar
osama alqaise
Newbie
1 Posts
July 25th, 2008
01:26 AM
#5

Re: Unusual Error, program works but there is a run time error
Quote:
Originally Posted by weaknessforcats
holidayCopy is not an array. You defined as char holidayCopy[] but never allocated memory for the copy.

how i can under stand stack qeue linced list double linced list

Reply
gpraghuram's Avatar
gpraghuram
Expert
1,089 Posts
July 25th, 2008
04:04 AM
#6

Re: Unusual Error, program works but there is a run time error
Quote:
Originally Posted by osama alqaise
how i can under stand stack qeue linced list double linced list



Whats your question?
Better start a new thread instead of adding a new question to an already active thread.

raghu

Reply
Reply
Not the answer you were looking for? Post your question . . .
182,318 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top C / C++ Forum Contributors