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?
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?
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
ya thats right so heres some examples:
My Class In the Headerfile:
My Funct Declaration in the source file: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(); };
[/code]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(); }
void ATM_Database::Load(struct &Users)
Does that make a difference?
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.
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);
Here is my struct:
How do i call the stuct in a statemnt like this:Code:struct UserInfo; { string name; int pin; double balance; }user[40];
void Load(struct Mystruct Users);
is it like this:
void Load(struct Userinfo user[40]);
Last edited by GreenCherry; 11-26-2002 at 10:32 AM.
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.
-PreludeCode: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(); }
My best code is written with the delete key.
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(); }; #endifCode://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 09:55 AM.
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.
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
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.
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
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.