Problem getting data stored in header file via a function...

This is a discussion on Problem getting data stored in header file via a function... within the C++ Programming forums, part of the General Programming Boards category; Hello folks, I am working on an assignment for school and I would greatly appreciate some help with my code. ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    20

    Problem getting data stored in header file via a function...

    Hello folks,

    I am working on an assignment for school and I would greatly appreciate some help with my code. I would like to retrieve some data from my header via a function in my main. However, when I call the function in my main the text shows up, but not the data. If I attempt to retrieve the data straight in the main without the use of a function, I get the right results, it is when I use a function that I have the problem.

    Some help would be greatly appreciate it.

    here is my header:
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    
    #include <iostream>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    class Account
    {
        public:
    
    
        char first_name[20];
        char last_name[20];
        float sin_number;
        char account_type[10];
        int number_of_transactions;
    
    
    void set_data(char fname[20],char lname[20],float sinnum, int acctype, int numtrans)
    {
        strcpy (first_name,fname);
        strcpy (last_name,lname);
        sin_number = sinnum;
        if (acctype = 1)
        {
            strcpy(account_type, "Chequing");
        }
        if (acctype = 2)
        {
            strcpy(account_type,"Savings");
        }
        number_of_transactions = numtrans;
    }
    
    void get_data()
    {
        cout<<"the first name is : "<<first_name<<endl;
        cout<<"the last name is : "<<last_name<<endl;
        cout<<"the sin number is : "<<sin_number<<endl;
        cout<<"the account type is : "<<account_type<<endl;
        cout<<"the number of transactions is : "<<number_of_transactions<<endl;
    }
    
    };
    #endif
    and here is my main:

    Code:
    #include <iostream>
    #include "Account.h"
    using namespace std;
    
    class functions : public Account
    {
    
        public:
    
    void PrintStatement(void)
    {
        Account acc;
        acc.get_data();
    }
    
    int main()
    {
        Account acc;
        functions func;
    
    
        char fname[20];
        char lname[20];
        float sinnum;
        int acctype;
        int numtrans;
    
        cout<<"Please enter the client's first name"<<endl;
        cin>>fname;
        cout<<"Please enter the client's last name"<<endl;
        cin>>lname;
        cout<<"Please enter the client's sin number"<<endl;
        cin>> sinnum;
        cout<<"Please enter the 1: for chequing or 2: for savings"<<endl;
        cin>>acctype;
        if (acctype !=1 && acctype !=2)
        {
            cout<<"Invalid account type, Please enter the 1: for chequing or 2: for savings"<<endl;
            cin>>acctype;
        }
        cout<<"Please enter the client's transaction number"<<endl;
        cin>>numtrans;
    
        acc.set_data(fname, lname, sinnum, acctype, numtrans);
    
        acc.get_data(); //it works here
    
        func.PrintStatement(); //here it doesnt work
    
    
        return (0);
    }
    Thank you very much for your help. I am very new to this.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    In set_data(), look in the "if" statements. Single equal sign (=) is assignment, two of them (==) is comparison.

    The Account object in PrintStatement() is a different object from the one in main(), and is not initialised with any data (set_data() is never called for it) which would explain your symptoms.

    There is the incidental problem I can tell, visually, that your code, as posted, will not compile, but I assume that is an error in your copy and paste.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    It would be a good idea to replace your char arrays with std::string and strcpy with normal assignment (=).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User sirama's Avatar
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    7
    Code:
    void PrintStatement(void)
    {
        Account acc;
        acc.get_data();
    }


    Change the above function to as you are already inheriting Account class:
    Code:
    void PrintStatement(void)
    {
        get_data();
    }

    There is the incidental problem I can tell, visually, that your code, as posted, will not compile, but I assume that is an error in your copy and paste.
    Agree with you. Semicolon is missing before main I guess...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    This is wrong:
    Code:
    class functions : public Account
    Your Account class is not designed to be a base class, so you should not be publicly inheriting from it. Rather, PrintStatement should be a non-member function that takes an Account as an argument.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A doubt about how data is stored in a file
    By avi2886 in forum C Programming
    Replies: 7
    Last Post: 04-16-2009, 02:37 AM
  2. Changing header data in binary file
    By neruocomp in forum C Programming
    Replies: 8
    Last Post: 11-14-2008, 06:30 PM
  3. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  4. How is data stored? (or something like that)
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 12-14-2002, 05:18 PM
  5. Write and use data stored in data files...
    By alex6852 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 12:45 PM

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