| | |
Have problem using size_t type...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
If I try to use this in my main:
And remove that getAccountNumber function from this program, will it work ??
C++ Syntax (Toggle Plain Text)
srand(static_cast<unsigned int>(time(NULL))); mAccountNumber = rand(); cout << mAccountNumber
And remove that getAccountNumber function from this program, will it work ??
This is how I'm told to use this function:
To set the account number to a unique number you should use the functions:
srand(static_cast<unsigned int>(time(NULL))) and rand() functions
srand(static_cast<unsigned int>(time(NULL)) sets the seed to the current time for the random number generation.
this is the number that is the starting point for the unique number rand() generates the random number. So to create your unique number, use:
srand(static_cast<unsigned int>(time(NULL)));
mAccountNumber = rand();
& this is what I'm told about set Function: told not to use it
"
Although we're implementing an ADT, we leave the setAccountNumber out because this is generated when the BankAccount is created and should not be changed once the BankAccount is created."
To set the account number to a unique number you should use the functions:
srand(static_cast<unsigned int>(time(NULL))) and rand() functions
srand(static_cast<unsigned int>(time(NULL)) sets the seed to the current time for the random number generation.
this is the number that is the starting point for the unique number rand() generates the random number. So to create your unique number, use:
srand(static_cast<unsigned int>(time(NULL)));
mAccountNumber = rand();
& this is what I'm told about set Function: told not to use it

"
Although we're implementing an ADT, we leave the setAccountNumber out because this is generated when the BankAccount is created and should not be changed once the BankAccount is created."
If I use that code for generating a unique # whithout having any getAccountNumber function then even i get these for these two lines in main:
int main() {
BankAccount ba ;
cout <<"running";
//ba.getAccountNumber();
srand(static_cast<unsigned int>(time(NULL)));
mAccountNumber = rand();
cout << mAccountNumber;
return 0;
}
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(39) : error C2065: 'mAccountNumber' : undeclared identifier
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(40) : error C2065: 'mAccountNumber' : undeclared identifier
int main() {
BankAccount ba ;
cout <<"running";
//ba.getAccountNumber();
srand(static_cast<unsigned int>(time(NULL)));
mAccountNumber = rand();
cout << mAccountNumber;
return 0;
}
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(39) : error C2065: 'mAccountNumber' : undeclared identifier
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(40) : error C2065: 'mAccountNumber' : undeclared identifier
Last edited by code12; May 11th, 2008 at 4:58 pm.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
A short example, the rand() function is directly used in the constructor, and the
getAccountNumber() can now be const.
Note that you need to call srand() only once.
getAccountNumber() can now be const.
Note that you need to call srand() only once.
class BankAccount
{
public:
// constructor
BankAccount()
{
// set the number here
mAccountNumber = rand();
}
size_t getAccountNumber() const { return mAccountNumber; }
private:
size_t mAccountNumber;
};
int main()
{
// seed the random number generator at the beginning of main()
srand(static_cast<unsigned int>(time(NULL)));
// rest of the program follows
} Last edited by mitrmkar; May 11th, 2008 at 5:03 pm.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
mAccountNumber is a member variable of BankAccount class, hence you need an instance of that class in the first place. So you might write something like
C++ Syntax (Toggle Plain Text)
int main() { srand(static_cast<unsigned int>(time(NULL))); BankAccount ba; // given that the mAccountNumber were a public member variable, // the following assignment would work ba.mAccountNumber = rand(); cout << ba.mAccountNumber; return 0; }
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
Actually one of your posts says that "we leave the setAccountNumber out because this is generated when the BankAccount is created" <-> essentially this is what I suggested about using rand() in the constructor, so I think you might very well do it like that, i.e. simply
C++ Syntax (Toggle Plain Text)
// constructor BankAccount() { // set the number here mAccountNumber = rand(); }
C++ Syntax (Toggle Plain Text)
#include<cstdlib> #include<string> #include<time.h> #include"Person.h" #include<iostream> using namespace std; typedef Person* PersonPtr; class BankAccount{ public: //friend ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount); //friend istream& operator >> (istream& aInput, BankAccount& aBankAccount); //friend bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2); BankAccount(); BankAccount(const Person& aPerson); /** double withdrawal(double aAmount); double deposit(double aAmount);*/ size_t getAccountNumber()const; /**void setBalance(double aBalance); double getBalance() const; void setAccountOwner(const Person& aPerson); Person getAccountOwner() const; bool sufficientFundsToWithdrawal(double aAmount) const;*/ private: PersonPtr mPerson; //mAmount; double mBalance; size_t mAccountNumber; }; //#endif int main() { BankAccount ba ; cout <<"running"; //ba.getAccountNumber(); srand(static_cast<unsigned int>(time(NULL))); //mAccountNumber = rand(); //cout << mAccountNumber; //cin >> ba; //cout << ba; return 0; } /** ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount){ cout << "A new account with account number"; aOutput << aBankAccount.mAccountNumber << endl; cout << "has been created for"; aOutput << aBankAccount.mPerson << endl; return aOutput; } istream& operator >> (istream &aInput, BankAccount& aBankAccount){ cout << "Please type the intitial balance for account number: " << aBankAccount.mAccountNumber << endl; aInput >> aBankAccount.mBalance; aInput >> aBank.mPerson; cout << "type the first name\n; aInput >> aBankAccount.mFName; cout << "type the last name\n; aInput >> aBankAccount.mLName; cout << "Type the address\n"; aInput >> aBankAccount.mAddress; cout << "type the name of the state\n"; aInput >> aBankAccount.mState; cout << "type the name of the city\n"; aInput >> aBankAccount.mCity; cout << "type the zip code\n"; aInput >> aBankAccount.mZip; return aInput; } bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2){ return(aBankAccount1.mPerson == aBankAccount2.mPerson && aBankAccount1.mBalance == aBankAccount2.mBalance && aBankAccount1.mAccountNumber == aBankAccount2.mAccountNumber); }*/ BankAccount::BankAccount(){ mPerson = new Person; mBalance = 5.00; mAccountNumber = rand(); } BankAccount::BankAccount(const Person &aPerson):mPerson( new Person), mBalance(5.00),mAccountNumber(rand()){ }/** void BankAccount::setAccountOwner (const Person& aPerson){ mPerson = new Person; } Person getAccountOwner () const{ return mPerson; } double BankAccount:: withdrawal (double aAmount){ double newBalance = Balance - aAmount; setBalance(newBalance); getBalance(); //return aBalance; } double BankAccount::deposit (double aAmount){ double newBalance = newBalance + aAmount; aBankAccount.setBalance(newBalance); aBankAccount.getBalance (); }*/ size_t BankAccount::getAccountNumber () const{ //srand(static_cast<unsigned int>(time(NULL))); //mAccountNumber = rand(); return mAccountNumber; }/** void BankAccount::setBalance (double aBalance) { mBalance = aBalance; } double BankAccount:: getBalance () const{ return mBalance; } bool BankAccount::sufficientFundsToWithdrawal (double aAmount) const { if((aAmount - aBalance) > 5){ return true; } else { return false; } } */
I have done as you told, now it doesnot give any errors but , how do I check which accountNumber is created...
I mean to say that I want to see th eaccountNumber created on the ouput screen after we run the program...
Last edited by code12; May 11th, 2008 at 5:20 pm.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
C++ Syntax (Toggle Plain Text)
int main() { // Before allocating any BankAccounts ... call srand() first srand(static_cast<unsigned int>(time(NULL))); BankAccount ba1, ba2; // see what account numbers were given ... cout << "Account: " << ba1.getAccountNumber() << "\n"; cout << "Account: " << ba2.getAccountNumber() << "\n"; return 0; }
Last edited by mitrmkar; May 11th, 2008 at 5:24 pm. Reason: added newlines
C++ Syntax (Toggle Plain Text)
#include<cstdlib> #include<string> #include<time.h> #include"Person.h" #include<iostream> using namespace std; typedef Person* PersonPtr; class BankAccount{ public: //friend ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount); //friend istream& operator >> (istream& aInput, BankAccount& aBankAccount); //friend bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2); BankAccount(); BankAccount(const Person& aPerson); /** double withdrawal(double aAmount); double deposit(double aAmount);*/ size_t getAccountNumber()const; /**void setBalance(double aBalance); double getBalance() const; void setAccountOwner(const Person& aPerson); Person getAccountOwner() const; bool sufficientFundsToWithdrawal(double aAmount) const;*/ private: PersonPtr mPerson; //mAmount; double mBalance; size_t mAccountNumber; }; //#endif int main() { srand(static_cast<unsigned int>(time(NULL))); BankAccount ba ; cout <<"running"; //ba.getAccountNumber(); //mAccountNumber = rand(); cout << ba.getAccountNumber() << endl; //cin >> ba; //cout << ba; return 0; } /** ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount){ cout << "A new account with account number"; aOutput << aBankAccount.mAccountNumber << endl; cout << "has been created for"; aOutput << aBankAccount.mPerson << endl; return aOutput; } istream& operator >> (istream &aInput, BankAccount& aBankAccount){ cout << "Please type the intitial balance for account number: " << aBankAccount.mAccountNumber << endl; aInput >> aBankAccount.mBalance; aInput >> aBank.mPerson; cout << "type the first name\n; aInput >> aBankAccount.mFName; cout << "type the last name\n; aInput >> aBankAccount.mLName; cout << "Type the address\n"; aInput >> aBankAccount.mAddress; cout << "type the name of the state\n"; aInput >> aBankAccount.mState; cout << "type the name of the city\n"; aInput >> aBankAccount.mCity; cout << "type the zip code\n"; aInput >> aBankAccount.mZip; return aInput; } bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2){ return(aBankAccount1.mPerson == aBankAccount2.mPerson && aBankAccount1.mBalance == aBankAccount2.mBalance && aBankAccount1.mAccountNumber == aBankAccount2.mAccountNumber); }*/ BankAccount::BankAccount(){ mPerson = new Person; mBalance = 5.00; mAccountNumber = rand(); } BankAccount::BankAccount(const Person &aPerson):mPerson( new Person), mBalance(5.00),mAccountNumber(rand()){ }/** void BankAccount::setAccountOwner (const Person& aPerson){ mPerson = new Person; } Person getAccountOwner () const{ return mPerson; } double BankAccount:: withdrawal (double aAmount){ double newBalance = Balance - aAmount; setBalance(newBalance); getBalance(); //return aBalance; } double BankAccount::deposit (double aAmount){ double newBalance = newBalance + aAmount; aBankAccount.setBalance(newBalance); aBankAccount.getBalance (); }*/ size_t BankAccount::getAccountNumber () const{ //srand(static_cast<unsigned int>(time(NULL))); //mAccountNumber = rand(); return mAccountNumber; }/** void BankAccount::setBalance (double aBalance) { mBalance = aBalance; } double BankAccount:: getBalance () const{ return mBalance; } bool BankAccount::sufficientFundsToWithdrawal (double aAmount) const { if((aAmount - aBalance) > 5){ return true; } else { return false; } } */
now it is giving these errors:
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(39) : error C2039: 'getAccountNumber' : is not a member of 'BankAccount'
1> c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(9) : see declaration of 'BankAccount'
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(109) : error C2039: 'getAccountNumber' : is not a member of 'BankAccount'
1> c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(9) : see declaration of 'BankAccount'
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(109) : error C2270: 'getAccountNumber' : modifiers not allowed on nonmember functions
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(109) : error C2365: 'getAccountNumber' : redefinition; previous definition was 'formerly unknown identifier'
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(112) : error C2065: 'mAccountNumber' : undeclared identifier
Last edited by code12; May 11th, 2008 at 5:47 pm. Reason: removed commneting sign"//" from front of line --size_t getAccountNumber()const;
![]() |
Similar Threads
- problem with size_t filesize (C)
- simple string assignment problem (C)
- Problem (C++)
- help vector problem (C++)
- problem instantiating a subclass of abstract class (C)
- Problem with memory allocation =( (C)
- can't seem to make a yes/no question (C)
- Problem of sorting words of each string using pointers (C++)
- IMF message filter SDK sample problem (C)
Other Threads in the C++ Forum
- Previous Thread: Delete Rows in dataGridView
- Next Thread: help with text editing
| Thread Tools | Search this Thread |
api application array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll email encryption error file format forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream image input int integer java lib linux loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct studio template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





