Thread: Need help with outputting data from a class

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Need help with outputting data from a class

    Hi I am stuck on how to output data that I got from a text file.

    The text file looks like this:
    1 5
    2 6
    3 7
    4 8
    5 9

    6 10
    7 11
    8 12
    9 13
    10 14

    Since I want to have an array of a class, in my main.cpp I have this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include "Check.h"
    using namespace std;
    int main()
    {
    Check Checks[2];
    
    cout << Checks[0].id[0] << Checks[0].num[0] << endl;
    cout << Checks[1].id[0] << Checks[1].num[0] << endl;
    
    return 0;
    }
    And in my header file I have this:
    Code:
    #ifndef CHECK
    #define CHECK
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    class Check
    {
    public:
    int id[5];
    int num[5];
    Check();
    void readCheck(ifstream&);
    };
    
    Check::Check( )
    {
    for ( int i = 0; i < 5; i++)
    {
    id[i] = 0;
    num[i] = 0;
    }
    }
    
    void Check::readCheck(ifstream&)
    {
    ifstream File;
    File.open("data.txt");
    Check Checks[4];
    
    for(int i = 0; i < 2; i++)
    {
    for(int j = 0; j < 5; j++)
    {
    File >> Checks[i].id[j]; >> Checks[i].num[j];
    }
    }
    File.close();
    }
    
    #endif
    So with that, I want to see the results to be:
    1 5
    6 10
    However I would just keep the output of
    0 0
    0 0

    Please see what I did wrong. Thank you so much!
    Last edited by chickenlittle; 09-04-2011 at 04:46 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you have code in your header file, you've already done it wrong.

    And the main you have posted doesn't read anything, and only prints two numbers, so (a) there's no reason for you to think it would get numbers from the file and (b) wouldn't give you four zeroes, only two.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    I'm supposed to have this in my class inside the header file:
    void readCheck(ifstream&);

    Where would I use that function? Inside my main or header?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You call the function whenever you want to do what that function does (i.e. read from a file).

    Remember: there should be no code in a header file.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    This is a demo of what my teacher gave me on classes:
    main.cpp:
    Code:
    #include "Demo.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	DateType a, b(5,4,2010), c(32,-1,2023);
    	cout<<"a: "<<a.getMonth()<<"/"<<a.getDay()<<"/"<<a.getYear()<<endl;
    	cout<<"b: "<<b.getMonth()<<"/"<<b.getDay()<<"/"<<b.getYear()<<endl;
    	cout<<"c: "<<c.getMonth()<<"/"<<c.getDay()<<"/"<<c.getYear()<<endl;
    }
    Demo.h file:
    Code:
    #include <iostream>
    using namespace std;
    
    /*
       The starting form of the class from the text book
     
    class DateType
    {
    public:
    	void initialize(int newMonth, int newDay, int newYear);
    	int getYear() const;
    	int getMonth() const;
    	int getDay() const;
    private:
    	int year;
    	int month;
    	int day;
    };
    
    */
    
    class DateType
    {
    public:
    	DateType(int = 1, int = 1, int = 2000);	//constructor with values
    	DateType(const DateType &);				//copy constructor
    	~DateType();							//destructor
    	void initialize(int newMonth, int newDay, int newYear);
    	int getYear() const;
    	int getMonth() const;
    	int getDay() const;
    private:
    	int year;
    	int month;
    	int day;
    	static int count;
    };
    
    int DateType::count = 0;
    
    DateType::DateType(int newM, int newD, int newY)
    {
    	initialize(newM, newD, newY);
    	count++;
    }
    DateType::DateType(const DateType &original)
    {
    	year = original.year;
    	month = original.month;
    	day = original.day;
    	//  or simply 
    	//     *this = original;
    	//  for memberwise copy
    	count++;
    }
    DateType::~DateType()
    {
    	cout << "destructor is running.  Count = " << --count << endl;
    }
    void DateType::initialize(int m, int d, int y)
    {
    	month = (m>=1 && m<=12)?m:1;
    	// note that the following is an insufficient check on legal day
    	day = (d>=1 && d<=31)?d:1;
    	year = (y>1900 && y<2012)?y:2000;
    }
    int DateType::getYear() const
    {
    	return year;
    }
    int DateType::getMonth() const
    {
    	return month;
    }
    int DateType::getDay() const
    {
    	return day;
    }

  6. #6
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Make it a method in the public area of your class. The implementation part shouldn't be too hard if you have had a class on reading files using ifstream.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Also, your implementation really shouldn't be in your header file (.h).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Outputting Data to Excel Sheet - MFC Application
    By strokebow in forum C++ Programming
    Replies: 3
    Last Post: 06-27-2011, 09:25 AM
  2. question regarding outputting data to adobe flash
    By hoax464 in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2008, 01:08 PM
  3. Replies: 1
    Last Post: 10-22-2005, 05:28 AM
  4. Replies: 1
    Last Post: 11-23-2003, 08:51 AM
  5. Outputting data to ports
    By FwyWice in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2002, 08:42 AM