Classes

This is a discussion on Classes within the C++ Programming forums, part of the General Programming Boards category; If I declare "int halt" in my private section of the header file, how can I use it in one ...

  1. #1
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65

    Classes

    If I declare "int halt" in my private section of the header file, how can I use it in one of the functions in the source file?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    post some code.

    I'm guessing you're talking about private in the class and member functions for that class? private is accessable to all member functions for that class. So if those member functions are in the source file as you have said, they should access the private stuff just fine.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    ya thats right so heres some examples:

    My Class In the Headerfile:
    Code:
    class ATM_Database
    {
    	int halt;
    
    	public:
    	//Loads member info from file into struct
    	void Load(struct &Users);
    	//Saves member info from struct to file
    	void Save(struct &Users);
    	//Creates a new user
    	void NewUser(struct &Users);
    	//Deletes a user
    	void DelUser(struct &Users);
    	//Randomizes Pin#
    	int PinNum();
    };
    My Funct Declaration in the source file:
    Code:
    ATM_Database::Load(struct &Users)
    {
    	ifstream fin;
    	fin.open("\\tipw2ks01\user_share\Lessons\AP Computer Science\Second Year Shared\ATM.txt");
    	for(int x=0; x<40; ++x)
    	{
    		if(fin.fail())
    		{
    			halt = x;	// <-- This should work fine, right?
    			return;
    		}
    		getline(fin, user[x].name);
    		fin<<user[x].pin;
    		fin<<user[x].checking;
    		fin<<user[x].savings;
    	}
    		fin.close();
    }
    [/code]

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    void ATM_Database::Load(struct &Users)

    Does that make a difference?

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Eh? Why does this thread have 1180 views? It was posted today...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    c--
    c-- is offline
    Registered User
    Join Date
    Dec 2001
    Posts
    43
    What you mean in :
    void Load(struct &Users);
    what is the name of the struct you defined???!
    I understand that you write aprototype of function that take aparametr in varable Users, but What is the type of this (Users) what's the name of struct you made?

    if the struct like this :
    struct Anything {
    int number;
    char letter;
    float real;
    };
    struct Anything Mystruct;

    then your Prototype should be:
    void Load(struct Mystruct* Users);
    Or
    void Load(struct Mystruct Users);

  7. #7
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Here is my struct:
    Code:
    	
    struct UserInfo;
    {
    	string name;
    	int pin;
    	double balance;
    }user[40];
    How do i call the stuct in a statemnt like this:
    void Load(struct Mystruct Users);

    is it like this:
    void Load(struct Userinfo user[40]);
    Last edited by GreenCherry; 11-26-2002 at 09:32 AM.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    There were several problems:

    >ATM_Database::Load(struct &Users)
    First, this function must match the declaration. So be sure to add the void return type. Second, struct &Users will give you problems, you must include the name of the struct to declare an instance of it:

    void ATM_Database::Load(UserInfo &Users)

    >fin.open("\\tipw2ks01\user_share\Lessons\AP Computer Science\Second Year Shared\ATM.txt");
    You may have problems if the compiler decides to treat \u, \L, \S, and \A as escape sequences. To fix this, double up the backslashes:

    fin.open("\\tipw2ks01\\user_share\\Lessons\\AP Computer Science\\Second Year Shared\\ATM.txt");

    >getline(fin, user[x].name);
    >fin>>user[x].pin;
    Why do you pass a reference to a UserInfo instance if you don't even work with it? This function accesses a global array.

    >fin>>user[x].checking;
    >fin>>user[x].savings;
    checking and savings aren't even members of UserInfo, it's an error to access them.
    Code:
    struct UserInfo
    {
      string name;
      int pin;
      double balance;
      double checking; // Added
      double savings; // Added
    }user[40];
    
    class ATM_Database
    {
      int halt;
      
    public:
      //Loads member info from file into struct
      void Load(UserInfo &Users);
      //Saves member info from struct to file
      void Save(UserInfo &Users);
      //Creates a new user
      void NewUser(UserInfo &Users);
      //Deletes a user
      void DelUser(UserInfo &Users);
      //Randomizes Pin#
      int PinNum();
    };
    
    void ATM_Database::Load(UserInfo &Users)
    {
      ifstream fin;
      fin.open("\\tipw2ks01\\user_share\\Lessons\\AP Computer Science\\Second Year Shared\\ATM.txt");
      for(int x=0; x<40; ++x)
      {
        if(fin.fail())
        {
          halt = x;
          return;
        }
        getline(fin, user[x].name);
        fin>>user[x].pin;
        fin>>user[x].checking;
        fin>>user[x].savings;
      }
      fin.close();
    }
    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Thank you so much prelude! I appreciate it. I changed all those things, i was wondering if you were willing to check my whole program, just a quick runthrough, I would appreciate it.

    Code:
    //ATM_Database.h
    
    #ifndef ATM_Database_H
    #define ATM_Database_H
    
    class ATM_Database
    {
    	int halt;
    
    	public:
    	//Loads member info from file into struct
    	void Load(struct &Users);
    	//Saves member info from struct to file
    	void Save(struct &Users);
    	//Creates a new user
    	void NewUser(struct &Users);
    	//Deletes a user
    	void DelUser(struct &Users);
    	//Randomizes Pin#
    	int PinNum();
    };
    
    #endif
    Code:
    //ATM_Database.cpp
    
    #include "apstring.h"
    #include <iostream>
    #include <fstream>
    #include <time.h>
    using namespace std;
    
    //Loads member info from file into struct.(struct constructor)
    void ATM_Database::Load(UserInfo &Users)
    {
      ifstream fin;
      fin.open("\\tipw2ks01\\user_share\\Lessons\\AP Computer Science\\Second Year Shared\\ATM.txt");
      for(int x=0; x<40; ++x)
      {
        if(fin.fail())
        {
          halt = x;
          return;
        }
        getline(fin, user[x].name);
        fin>>user[x].pin;
        fin>>user[x].checking;
        fin>>user[x].savings;
      }
      fin.close();
    }
    
    //Saves member info from struct to file
    void ATM_Database::Save(struct Users)
    {
    
    	ofstream fout;
    	fout.open("\\tipw2ks01\user_share\Lessons\AP Computer Science\Second Year Shared\ATM.txt");
    	for(x=0; x<=halt; ++x)
    	{
    		fout>>user[x].name;
    		
    		if(user[x].pin == 0)
    			fout>>user[x].pin;
    		
    		if(user[x].checking == 0)
    			fout>>user[x].checking;
    		
    		if(user[x].savings == 0)
    			fout>>user[x].savings;
    	}
    	fout.close();
    }
    //Creates a new user
    void ATM_Database::NewUser(struct &Users)
    {	
    	cout<<"Please enter the information requested in order to become a member."<<endl;
    	cout<<"Enter Name: ";
    	cin>>user[halt].name;
    	int randNum = PinNum();			//Random Pin Number
    	user[halt].pin = randNum;
    	user[halt].checking = 0;
    	user[halt].savings = 0;
    	cout<<endl<<"Member creation sucessful"<<endl;
    	cout<<"Your randomly generated pin number is: "<<randNum<<"."<<endl;
    	++halt;
    }
    //Deletes a user
    void ATM_Database::DelUser(struct &Users)
    {
    	int usernum;
    	cout<<"What is the member number of the one to be deleted? ";
    	cin>>usernum;
    	user[usernum].name = "";
    	user[usernum].pin = '\0';
    	user[usernum].checking = '\0';
    	user[usernum].savings = '\0';
    	cout<<endl<<"User deletion complete"<<endl;
    	--halt;
    }
    //Randomizes Pin#
    int ATM_Database::PinNum()
    {
    	int HIGH = 9999;
    	int LOW = 1111;
    	time_t seconds;
    	time(&seconds);
    	srand((unsigned int) seconds);
    	int dice = rand() % (HIGH - LOW + 1) + LOW;
    	return dice;
    }
    Last edited by GreenCherry; 12-03-2002 at 08:55 AM.

  10. #10
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    C'mon people, this isn't just limited to Prelude, andyone with knowledge can point out the simplest of my errors. If you do a run through and find nothing wrong just say so, its not very hard.

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    read the thread again. The answers u seek are already there. Apply them yourself
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    I did, I just would appreciate it if someone would look it over and make sure its correct. I'm not very good at classes. Also, just so you dont get confused, the struct is in the main cpp file and will be called into my class.
    I have a rabbit in my pants! Please be happy for me.

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    read thread and fix your header file first then we will look at the implementation.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #14
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65

    Cool

    Thanks for all your help, but I don't need anymore. Normally I would lock the post, but I am unable to do so, or too ignorant to figure it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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