trouble opening my .txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Izzy123
    New Member
    • Apr 2010
    • 7

    trouble opening my .txt file

    Im having trouble opening the .txt file in dev-c++, I've entered the correct name for the .txt file which is saved under the same folder, but whenever I compile and run, it will just show :
    "
    Begin reading file.
    End reading file.

    --------Welcome-------
    Please choose:
    etc.
    "
    The content of the .txt file won't show.

    below is part of my header file "Example.h:
    Code:
      
     class Example
    {
          private:
                  char seats[4][5][11];
                  bool validate(char);
          public:
                 Example(){
                          readInformation();
                          }
                 int LVL, COL, ROW;
                 void readInformation();
                 void writeInformation();
    };
    void Example :: readInformation() 
    {
         int i, j,k;     
         ifstream fin;
         cout<< "Begin reading file.\n";
         
         fin.open("abc.txt");
         if (!fin)
         {
                   cout<<"Input file opening failed.\n";
                   exit(1);
         }
         while(!fin.eof())
         {
             char c;
             fin.get(c);
             if (validate(c)) 
             {
                   seats[i][j][k] =c;
                   if(j==COL-1)
                   {
                       j=0;
                       i++;
                   }
                   else
                   {
                       k+=1;
                       i+=1;
                   }
             }
         }
    
         fin.close();
         cout<< "End reading file\n" <<endl;
    }
    
    bool Example :: validate(char c)  
    { 
            if (c=='A' || c == 'B' || c == 'D' || c == 'E') 
                    return true; 
            else  
                    return false; 
    }
    
    //void Example :: writeInformation()
    and this is my cpp file:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    #include "Example.h"
    
    int main()
    {
        Example ex;
        cout<< "-----Welcome-----\n";
        int choice;
        char ch;
        do
        {
             cout<< "Please choose: \n";
             cout<< "1. Display info\n 2.Make reservation\n";
             cin>>choice;
             
             switch(choice)
             {
                 case 1:
                      ex.readInformation();
                      break;
                 case 2:
                      cout<<"make reservation\n";
                      break;
                 default:
                      cout<<"Invalid input! Enter again:\n";
                      cin>>choice;
             }
             cout<< "Continue? (Y/N)";
             cin>>ch;
             if(ch == 'N' || ch == 'n')
             {
                   cout<<"Thank you and Bye!\n";
             }
        }while (ch == 'Y' || ch == 'y');
        system ("pause");
        return 0;    
    }
    this is what I have in my abc.txt file:
    A B B A
    A B B A
    A B B A
    A B B A
    A B B A
    D B B D
    D B B D
    D B B D
    D B B D
    D B B D
    D B B D
    D B B D
    D B B D

    can anyone please help, thanks.
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    There is no code to 'show' something when you read from the file - it just reads and stores it in seats[] array. Why must it 'show' something?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      exit(1) ?

      Not in C++. You need to return and error code. There is no way the function that tries to open the file has any idea of what to do if it doesn't open. That may be OK or it may not depending upon the application.

      Consider this code is 125 levels deep in a program of 125,000 functions running on a server with 4000 users attached and you do exit(1). Boom! There's the C:>\ and the support staff says "Gee, I wonder where that happened".

      Alyways code error handlers. In this case, call the funciton in main(), check for the error code and take the action in main().

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Code:
        1. Display info:
        ...
        case 1: 
                          ex.readInformation();
        If you type in 1, you're telling it to read in information, but never display it. What does this seat information represent anyways? You need to choose a way to display it.


        On second look, the read function doesn't make much sense either. The incrementing is all wrong.
        ... side note: give the guy a break with the exit(1); it was probably written by a teacher or ta, and they're not taking this beyond the classroom.

        Comment

        Working...