Coder Profile - Show off your skills, get a coder profile.
 
 
 
  
Posted: 3.2 Years Ago 

Yuripro84
United States
Contrib Level: 10
Total Posts: 1,132
Hey guys, so I'm totally lost here. I'm almost done w/ an assignment, and have just one last thing to do. He had a working script but wanted us to modify it so instead of writing to the screen we write to a file, and instead using the predefined libraries, we only use the stdio library (he used <iostream> to read/write which is a bit different, I can tell). Anyways, it all seems to be working good, I can read and write what I need, But, I am having problems passing the stream through a function. Here's the original code, followed my my code (a Work in Progress). The error is on line 64. All I need to know, if someone could help, is how to properly pass the stream through the function, and then how to setup the function parameters to allow it. I appreciate all help, this has been bugging me for a long time now and it's due today lol. And I know my code is a bit messy, I'm sorry, I'm in a rush to help some people out in 40mins, still need to get ready and drive there, but thought I'd post this before I left, thanks guys!

-Chris

My Code
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. #include <fstream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. const int MaxHolidays = 25;
  6. const int MaxDescLength = 40;
  7.  
  8. struct holidayStruct {
  9.     int holidayId;
  10.     char description[MaxDescLength+1];
  11.     int month;
  12.     int day;
  13.     double cost;
  14. };
  15.  
  16. holidayStruct *
  17. readHoliday(istream &holidayFile)
  18. {
  19.     int id;
  20.     holidayStruct *holidayPtr;
  21.  
  22.     holidayFile >> id;
  23.     if (!holidayFile.eof()) {
  24.         holidayPtr = new holidayStruct;
  25.         if (!holidayPtr) {
  26. // cerr << "Critical Error: Failure to allocate memory in "
  27. // << "readHoliday() function.\n"
  28. // << "Contact your holiday management program team to resolve\n"
  29. // << "this problem." << endl;
  30.             exit(1);
  31.         }
  32.         holidayPtr->holidayId = id;
  33.         holidayFile >> holidayPtr->month >> holidayPtr->day >> holidayPtr->cost;
  34.         holidayFile.ignore();
  35.         holidayFile.getline(holidayPtr->description, MaxDescLength+1);
  36.         return (holidayPtr);
  37.     } else return NULL;
  38. }
  39.  
  40. int
  41. main()
  42. {
  43.     holidayStruct *items[MaxHolidays];
  44.     holidayStruct *itemPtr = NULL;
  45.  
  46.     int i; // general loop counter
  47.     int itemCount; // current number of items
  48.  
  49.   char str[100];
  50.   FILE * holidays;
  51.   holidays = fopen ("holidays.txt","r");
  52.   FILE * partsOut;
  53.   partsOut = fopen ("partsOut.txt","w");
  54.  
  55.   if(holidays==NULL)
  56.     printf("%s","Could Not Open File");
  57.  
  58.  
  59.  
  60.     //reading from the file
  61.     itemCount = 0;
  62.   // fscanf(holidays,"%s",&str);
  63.     //printf("%s",str);
  64.     while (itemCount < MaxHolidays && (items[itemCount] = readHoliday(&holidays)) )
  65.         ++itemCount;
  66. // cout << itemCount << " holidays read in.\n";
  67.  
  68. // cout << setiosflags(ios::fixed);
  69. // cout.precision(2);
  70.  
  71.     //if (itemCount)
  72.       fprintf(partsOut,"%*s %*s %*s %*s %s",
  73.                        10, "id",
  74.                        10,"Month",
  75.                        10,"Day",
  76.                        12,"Cost",
  77.                        "Description\n");
  78. // cout << setw(10) << "id" << setw(10) << "Month"
  79. // << setw(10) << "Day" << setw(12) << "Cost"
  80. // << " Description\n\n";
  81.  
  82.     for(i=0; i<itemCount; i++)
  83.  
  84. // cout << setw(10) << items[i]->holidayId
  85. // << setw(10) << items[i]->month
  86. // << setw(10) << items[i]->day
  87. // << setw(12) << items[i]->cost
  88. // << " " << items[i]->description << endl;
  89.     gets(str);
  90.     return 0;
  91. }
Original Code
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. const int MaxHolidays = 25;
  7. const int MaxDescLength = 40;
  8. const char HOLIDAY_FILE[] = "holidays.txt";
  9.  
  10. struct holidayStruct {
  11.     int holidayId;
  12.     char description[MaxDescLength+1];
  13.     int month;
  14.     int day;
  15.     double cost;
  16. };
  17.  
  18. holidayStruct *
  19. readHoliday(istream &holidayFile)
  20. {
  21.     int id;
  22.     holidayStruct *holidayPtr;
  23.  
  24.     holidayFile >> id;
  25.     if (!holidayFile.eof()) {
  26.         holidayPtr = new holidayStruct;
  27.         if (!holidayPtr) {
  28.             cerr << "Critical Error: Failure to allocate memory in "
  29.                  << "readHoliday() function.\n"
  30.                  << "Contact your holiday management program team to resolve\n"
  31.                  << "this problem." << endl;
  32.             exit(1);
  33.         }
  34.         holidayPtr->holidayId = id;
  35.         holidayFile >> holidayPtr->month >> holidayPtr->day >> holidayPtr->cost;
  36.         holidayFile.ignore();
  37.         holidayFile.getline(holidayPtr->description, MaxDescLength+1);
  38.         return (holidayPtr);
  39.     } else return NULL;
  40. }
  41.  
  42. int
  43. main()
  44. {
  45.     holidayStruct *items[MaxHolidays];
  46.     holidayStruct *itemPtr = NULL;
  47.  
  48.     int i; // general loop counter
  49.     int itemCount; // current number of items
  50.     ifstream inputFile;
  51.  
  52.     inputFile.open(HOLIDAY_FILE, ios::in);
  53.     if (inputFile.fail())
  54.     {
  55.         cerr << "Holiday file " << HOLIDAY_FILE
  56.              << " could not be opened." << endl;
  57.         cin.get();
  58.         exit(1);
  59.     }
  60.  
  61.     //reading from the file
  62.     itemCount = 0;
  63.     while (itemCount < MaxHolidays
  64.         && (items[itemCount] = readHoliday(inputFile)) )
  65.         ++itemCount;
  66.     cout << itemCount << " holidays read in.\n";
  67.  
  68.     cout << setiosflags(ios::fixed);
  69.     cout.precision(2);
  70.  
  71.     if (itemCount)
  72.         cout << setw(10) << "id" << setw(10) << "Month"
  73.              << setw(10) << "Day" << setw(12) << "Cost"
  74.              << " Description\n\n";
  75.  
  76.     for(i=0; i<itemCount; i++)
  77.         cout << setw(10) << items[i]->holidayId
  78.              << setw(10) << items[i]->month
  79.              << setw(10) << items[i]->day
  80.              << setw(12) << items[i]->cost
  81.              << " " << items[i]->description << endl;
  82.  
  83.     return 0;
  84. }
Page 1 of 1
 
 
Latest News About Coder Profile
Coder Profile Poll
Why do you get bored with programming?

Not enough time to do something productive
I run out of ideas
Too hard to show people my creations
Everything i do has too many errors, and it's too hard
I don't get bored!!!


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.49 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved