my program wont read my text files in??

This is a discussion on my program wont read my text files in?? within the C++ Programming forums, part of the General Programming Boards category; When my program runs and i press 1 or 2 from the menu the function member_list or book_list runs but ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    15

    my program wont read my text files in??

    When my program runs and i press 1 or 2 from the menu the function member_list or book_list runs but doesnt read any data from the text file it just prints out the cout statements a number of times. What i need it to do it display the records from the text file im reading into the program. Can anyone solve this problem ?

    I believe i have opened them correctly in main() could it be that my functions to read them are wrong ?

    please help??


    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    #include <fstream.h>
    
    const int maxmembers = 1000;
    const int maxbooks = 2000;
    
    struct cust
    {
      string surname;
      string house_no;
      string road;
      string  ID;
      int books_borrowed;
    };
    
    struct books
    {
      string author;
      string title;
      string category;
      string ISBN;
      int loaned;
    };
    
    void member_list(cust c[])
    {
     for(int i=0; i< maxbooks;i++)
        {
      cout<< "Surname\t\tHouse No\t\tRoad\t\tID" << endl;
      cout << c[i].surname << endl;
      cout << c[i].house_no << endl;
      cout << c[i].road << endl;
      cout << c[i].ID << endl;
        }
    }
    
    void book_list(books b[])
    {
     for(int i=0; i< maxbooks;i++)
        {
      cout<< "Author\t\tTitle\t\tCategory\t\tISBN" << endl;
      cout << b[i].author << endl;
      cout << b[i].title << endl;
      cout << b[i].category << endl;
      cout << b[i].ISBN << endl;
        }
    }
    
    main()
    {
      char uTest[100];
      int cRead = 0;
      int nTest[100];
    
    //variables for type people and books
      books book[maxbooks];
      cust user[maxmembers];
    
      ifstream user_data;
      user_data.open("userdata.txt",ios::in);
    
    do
      {
        user_data.getline(uTest,100,';');
        user[cRead].surname = uTest;
        user_data.getline(uTest,100,';');
        user[cRead].house_no = uTest;
        user_data.getline(uTest,100,';');
        user[cRead].road = uTest;
        user_data.getline(uTest,100,'\n');
        user[cRead].ID = uTest;
    
        if (user_data)
          {
    	cRead++;
          }
    
      }while (user_data && cRead <= maxmembers);
    
      user_data.close();
    
      ifstream book_data;
      book_data.open("bookdata.txt",ios::in);
    
    do
      {
        book_data.getline(uTest,100,';');
        book[cRead].author = uTest;
        book_data.getline(uTest,100,';');
        book[cRead].title = uTest;
        book_data.getline(uTest,100,';');
        book[cRead].category = uTest;
        book_data.getline(uTest,100,'\n');
        book[cRead].ISBN = uTest;
    
        if (book_data)
          {
    	cRead++;
          }
    
      }while (book_data && cRead <= maxbooks);
    
      book_data.close();
    
      int choice;
      cout <<"Library Menu" << endl << endl ;
      cout <<"[1] List Library Members and Member Information" << endl;
      cout <<"[2] List Library Books and Book Information" << endl ;
      cout <<"[3] Issue Library Book" << endl ;
      cout <<"[4] Return Library Book" << endl ;
      cout <<"[5] Exit Library System" << endl ;
      cout <<"Enter Your Required Choice" << endl;
      cin >> choice;
      
      switch(choice)
        {
        case 1:
          member_list(user);
          break;
        case 2:
          book_list(book);
          break;
        case 5:
          exit(1);
          break;
        default:
          cout << "This is not an option" << endl;
          break;
        }
    
     }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    i found a couple things in looking through your code. I didn't copy and paste because I don't have the files to get it to work and I'm too lazy to make them. In any event:

    for sure, this isn't the cause of your problem, but use int main() not void
    Then the line return 0 at the very end of main() is traditional, but not mandatory, apparently.

    This might be part of the problem, if you truly have a files that are maxnumber and maxbooks long. cRead needs to be limited to one less than maxnumber and maxbooks. If cRead equals maxnumber or maxbooks then you overwrite the end of array and if you are lucky your program crashes.

    Also, cRead needs to be reset back to zero between the two do/while loops.

    If your files don't have exactly maxnumber and maxbooks in them what happens? This is a weakness in the design of the program and should be corrected, IMO.

    the values of maxnumber and maxbooks is high enough you may be overloading the stack. You might want to try this using dynamic memory for book and user.

    if I were to look further I would probably change maxnumber to and maxbooks to 2 and place a few cout statements in the do/while loops to be sure that the file opened and you are able to read in the data as you expedted.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Data gets read, and printed out... but you loop 1000 times, the data gets pushed off the screen before you read it. Like elad said, reduce maxbooks and maxusers or whatever.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Builder C++ large files support for simple C program
    By Murfury in forum C Programming
    Replies: 3
    Last Post: 11-23-2007, 02:47 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 11:45 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM

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