Debug Help w/ <fstream>

This is a discussion on Debug Help w/ <fstream> within the C++ Programming forums, part of the General Programming Boards category; I call upon ye' fellow programmers for help with this code.. which compiles but locks up at runtime. It's a ...

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    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
    
             */
    Last edited by The Brain; 02-11-2005 at 06:18 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    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.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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..??
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.
    Last edited by 7stud; 02-13-2005 at 12:09 PM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 05:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 03:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 12:08 PM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-25-2002, 12:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21