Reading integers from a file into classes.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolcheelol
    New Member
    • Feb 2010
    • 25

    #1

    Reading integers from a file into classes.

    I'm fairly new to classes and I know how to read integers from a file into the main, but I'm really getting confused on how to do it in classes.

    for example...

    Code:
    ifstream in1("nums.dat");
        int ct=0;
        int *p=new int[1300];
        int forever=1;
    
       while(forever){      
        in1 >> *(p+ct); 
        
       if(*(p+ct) == -1) break;
        ct++; 
                } // WHILE LOOP
    I know that you would declare int p in private, but I have no idea where you would do the rest of the stuff. I need to use a class instance to read the files, and then print them.

    I know the basics on how to do classes, but I just started it so any help is appreciated. Thanks!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Write a main() that shows how you would want your classes used. Then write your classes to support that main().

    A common design approach is to define the user interface first and then the classes to support the interface.

    Comment

    • lolcheelol
      New Member
      • Feb 2010
      • 25

      #3
      well that is the main() and all the codes really. all there really else is the cout << part. so do you think you can help me adjust these into a class?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        First, pretend you have a class.
        Second, create an object of that class.
        Third, use a method of that class to fetch an integer.
        Fourth, write the class.
        Fifth, write the method to return an int. Just hard code to return say a 10.
        Sixth, verify your main() works.
        Seventh, now write the method to read from a file. Just hard code the file name.
        Eighth, verify your main() still works.
        Ninth, write a method to accept a file name.
        Tenth, use a data variable to hold the file name
        Eleventh, use the data variable in your read method.
        Twelfth, add an ifstream data member
        Thirteenth, use the ifstream data member in your read function.

        As you go throught these steps, make sure main() is always working. If it doesn't the problem will probably be with the last change you made.

        Comment

        • lolcheelol
          New Member
          • Feb 2010
          • 25

          #5
          Code:
          class Class1{
          private:
          int ct;
          int *p;
          int forever;
          public:
          void parray();
          void rarray();
          
          };
          
          void Class1::rarray(){
          ifstream in1("nums.dat");
          ct=0;
          p=new int[1300];
          forever=1;
          while(forever){
          in1 >> *(p+ct);
          
          if(*(p+ct) == -1) break;
          ct++;
          } // WHILE LOOP
          }
          
          void Class1::parray(){
          for(int i=0; i<ct; i++){
          cout << " " << *(p+i);
          if((i%15==0)&&(i>0)) cout << "\n";
          if((i%150==0)&&(i>0)){
          cout << "\n\n\tEnter to continue...\n";
          getch();
          } // if
          } // FOR LOOP
          }
          
          
          int main(){
          clrscr();
          
          Class1 c1;
          c1.parray();
          c1.rarray();
          This is the code I came up with. The program just crashes when I run it. I didn't post the includes and the end of main because I thought it was irrelevant. Any help is appreciated again.

          Code:
          int main(){
             clrscr();
              ifstream in1("nums.dat");
              int ct=0;
              int *p=new int[1300];
              int forever=1;
          
             while(forever){      
              in1 >> *(p+ct); 
              
             if(*(p+ct) == -1) break;
              ct++; 
                      } // WHILE LOOP
             
             for(int i=0; i<ct; i++){
                cout << "  " << *(p+i);
                 if((i%15==0)&&(i>0)) cout << "\n";
                 if((i%150==0)&&(i>0)){
                     cout << "\n\n\tEnter to continue...\n";
                     getch();
                          } // if
                         } // FOR LOOP
                              
              cout << "\n\n\tTotal data = " << ct;
              delete p; 
              in1.close();
          This is the original program I'm trying to convert to a class.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            It looks like you are trying to print your array before you read it. That is, the parray method is called before the rarray method.

            However, do not fix that. This has exposed a weakness in your class: No constructors. You need to write a constructor so that your ct an p variables are set to zero. That should allow the parray method to execute without doing anything.

            Comment

            • lolcheelol
              New Member
              • Feb 2010
              • 25

              #7
              okay great. thanks for your help. if i'm trying to read multiple files from a text file, how would it look like. this is what i have so far.

              Code:
              typedef struct record1{
                 int n1;
                 string name1;
                 char c1;
                 double d1;
              } set1;
              
              class Class1{
                 private:
                  int ct;
                  set1 *s1;
                 public:
                  void rarray();
                  void parray();   
              };
              
              void Class1::rarray(){
                 ifstream in1("names.dat");
                  set1 *s1 = new set1[100];
              
                 for(ct=0;!in1.eof();++ct){
                  in1>>(s1+ct)->n1;
                  in1>>(s1+ct)->name1;
                  in1>>(s1+ct)->c1;
                  in1>>(s1+ct)->d1;
                 
                                          } // FOR
              thanks for all your help so far. i really appreciate it. btw there is no constructor+des tructor on here yet because i want to try and figure out if the code part is right first.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Why is set1 a struct? This should be a class. You class1 class shoujld work with set1 objects.

                The class1::rarray has a hard-coded 100. Why? Hard-coding is big no-no. The class1 constructor should have an argument for the number of elements in the array. The constructor should do the allocation of the correct size and set up the other class variables.


                Try to avoid a class/struct A messing with the members of class/struct B. When you do this the two become welded together. If one changes, then the other may cause a crash. This is the dreaded "spaghetti code". Instead, write classes to protect those members. You need to make sure that iof the members get screwed up that it will be the member functions that did not and not some one or more of the 50,000 functions in your code.


                As to arrays of files, consider a private class1 method to GetNextFileName that has a string& argument and returns a bool. It true is returned the file name is in the reference string. If false, there is no next file file-- i.e. end of the array.

                Lastly, this is C++. Use a vector rather than an array. A vector is required to be implemented as an array. The vector code manages the array saving you the trouble of writing (and debugging) your same array code over and over and over.

                Code:
                string str;
                if (!GetNextFileName(str))
                {
                     return;   .//no more file names
                }
                //open the file using str.c_str().
                //and read it
                
                return

                Comment

                Working...