473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

10
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?
Jul 21 '08 #1
5 1972
Laharl
849 Expert 512MB
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.
Jul 22 '08 #2
gpraghuram
1,275 Expert 1GB
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
Jul 22 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
holidayCopy is not an array. You defined as char holidayCopy[] but never allocated memory for the copy.
Jul 22 '08 #4
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
Jul 25 '08 #5
gpraghuram
1,275 Expert 1GB
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
Jul 25 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Farooq Khan | last post by:
why does Response.Write in a method of code-beind class when called from inpage code (i.e in <%---%>), after creating object of that class, fails when called while it works perfectly ok while...
10
by: Tomás Ó hÉilidhe | last post by:
I've been writing mostly C code for the last year or so (rather than C++), and in writing C code I always make it fully portable, allowing for 9-Bit bytes, allowing for padding within integer...
0
by: mk | last post by:
http://linux.byexamples.com/archives/365/python-convey-the-exception-traceba That's seriously weird. What's your Python version and platform? On my Windows and Linux machines, with more recent...
4
by: Newbie | last post by:
Hello I need to enter a string of the form abc (a string of characters followed by EOF) #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 100 int main(void) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.