Have problem using size_t type...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: Have problem using size_t type...

 
0
  #11
May 11th, 2008
If I try to use this in my main:
  1. srand(static_cast<unsigned int>(time(NULL)));
  2. mAccountNumber = rand();
  3. cout << mAccountNumber

And remove that getAccountNumber function from this program, will it work ??
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: Have problem using size_t type...

 
0
  #12
May 11th, 2008
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."
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: Have problem using size_t type...

 
0
  #13
May 11th, 2008
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
Last edited by code12; May 11th, 2008 at 4:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Have problem using size_t type...

 
0
  #14
May 11th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Have problem using size_t type...

 
0
  #15
May 11th, 2008
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

  1. int main()
  2. {
  3. srand(static_cast<unsigned int>(time(NULL)));
  4.  
  5. BankAccount ba;
  6.  
  7. // given that the mAccountNumber were a public member variable,
  8. // the following assignment would work
  9. ba.mAccountNumber = rand();
  10. cout << ba.mAccountNumber;
  11.  
  12. return 0;
  13. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Have problem using size_t type...

 
0
  #16
May 11th, 2008
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

  1. // constructor
  2. BankAccount()
  3. {
  4. // set the number here
  5. mAccountNumber = rand();
  6. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: Have problem using size_t type...

 
0
  #17
May 11th, 2008
  1. #include<cstdlib>
  2. #include<string>
  3. #include<time.h>
  4. #include"Person.h"
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. typedef Person* PersonPtr;
  9. class BankAccount{
  10. public:
  11. //friend ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount);
  12. //friend istream& operator >> (istream& aInput, BankAccount& aBankAccount);
  13. //friend bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2);
  14. BankAccount();
  15. BankAccount(const Person& aPerson);
  16. /** double withdrawal(double aAmount);
  17. double deposit(double aAmount);*/
  18. size_t getAccountNumber()const;
  19. /**void setBalance(double aBalance);
  20. double getBalance() const;
  21. void setAccountOwner(const Person& aPerson);
  22. Person getAccountOwner() const;
  23. bool sufficientFundsToWithdrawal(double aAmount) const;*/
  24. private:
  25. PersonPtr mPerson;
  26. //mAmount;
  27. double mBalance;
  28. size_t mAccountNumber;
  29. };
  30. //#endif
  31.  
  32. int main() {
  33. BankAccount ba ;
  34. cout <<"running";
  35. //ba.getAccountNumber();
  36. srand(static_cast<unsigned int>(time(NULL)));
  37. //mAccountNumber = rand();
  38. //cout << mAccountNumber;
  39.  
  40.  
  41. //cin >> ba;
  42. //cout << ba;
  43.  
  44. return 0;
  45. }
  46.  
  47. /**
  48. ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount){
  49. cout << "A new account with account number";
  50. aOutput << aBankAccount.mAccountNumber << endl;
  51. cout << "has been created for";
  52. aOutput << aBankAccount.mPerson << endl;
  53. return aOutput;
  54. }
  55. istream& operator >> (istream &aInput, BankAccount& aBankAccount){
  56. cout << "Please type the intitial balance for account number: " << aBankAccount.mAccountNumber << endl;
  57. aInput >> aBankAccount.mBalance;
  58. aInput >> aBank.mPerson;
  59.  
  60. cout << "type the first name\n;
  61. aInput >> aBankAccount.mFName;
  62. cout << "type the last name\n;
  63. aInput >> aBankAccount.mLName;
  64. cout << "Type the address\n";
  65. aInput >> aBankAccount.mAddress;
  66. cout << "type the name of the state\n";
  67. aInput >> aBankAccount.mState;
  68. cout << "type the name of the city\n";
  69. aInput >> aBankAccount.mCity;
  70. cout << "type the zip code\n";
  71. aInput >> aBankAccount.mZip;
  72.  
  73. return aInput;
  74. }
  75. bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2){
  76. return(aBankAccount1.mPerson == aBankAccount2.mPerson &&
  77. aBankAccount1.mBalance == aBankAccount2.mBalance &&
  78. aBankAccount1.mAccountNumber == aBankAccount2.mAccountNumber);
  79. }*/
  80. BankAccount::BankAccount(){
  81. mPerson = new Person;
  82. mBalance = 5.00;
  83. mAccountNumber = rand();
  84. }
  85.  
  86. BankAccount::BankAccount(const Person &aPerson):mPerson( new Person), mBalance(5.00),mAccountNumber(rand()){
  87. }/**
  88. void BankAccount::setAccountOwner (const Person& aPerson){
  89. mPerson = new Person;
  90. }
  91. Person getAccountOwner () const{
  92. return mPerson;
  93. }
  94. double BankAccount:: withdrawal (double aAmount){
  95. double newBalance = Balance - aAmount;
  96. setBalance(newBalance);
  97. getBalance();
  98. //return aBalance;
  99.  
  100. }
  101.  
  102. double BankAccount::deposit (double aAmount){
  103. double newBalance = newBalance + aAmount;
  104. aBankAccount.setBalance(newBalance);
  105. aBankAccount.getBalance ();
  106.  
  107. }*/
  108. size_t BankAccount::getAccountNumber () const{
  109. //srand(static_cast<unsigned int>(time(NULL)));
  110. //mAccountNumber = rand();
  111. return mAccountNumber;
  112. }/**
  113. void BankAccount::setBalance (double aBalance) {
  114. mBalance = aBalance;
  115. }
  116. double BankAccount:: getBalance () const{
  117. return mBalance;
  118. }
  119. bool BankAccount::sufficientFundsToWithdrawal (double aAmount) const {
  120. if((aAmount - aBalance) > 5){
  121. return true;
  122. }
  123. else {
  124. return false;
  125. }
  126. }
  127. */



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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Have problem using size_t type...

 
0
  #18
May 11th, 2008
  1. int main()
  2. {
  3. // Before allocating any BankAccounts ... call srand() first
  4. srand(static_cast<unsigned int>(time(NULL)));
  5.  
  6. BankAccount ba1, ba2;
  7. // see what account numbers were given ...
  8. cout << "Account: " << ba1.getAccountNumber() << "\n";
  9. cout << "Account: " << ba2.getAccountNumber() << "\n";
  10.  
  11. return 0;
  12. }
Last edited by mitrmkar; May 11th, 2008 at 5:24 pm. Reason: added newlines
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: Have problem using size_t type...

 
0
  #19
May 11th, 2008
  1. #include<cstdlib>
  2. #include<string>
  3. #include<time.h>
  4. #include"Person.h"
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. typedef Person* PersonPtr;
  9. class BankAccount{
  10. public:
  11. //friend ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount);
  12. //friend istream& operator >> (istream& aInput, BankAccount& aBankAccount);
  13. //friend bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2);
  14. BankAccount();
  15. BankAccount(const Person& aPerson);
  16. /** double withdrawal(double aAmount);
  17. double deposit(double aAmount);*/
  18. size_t getAccountNumber()const;
  19. /**void setBalance(double aBalance);
  20. double getBalance() const;
  21. void setAccountOwner(const Person& aPerson);
  22. Person getAccountOwner() const;
  23. bool sufficientFundsToWithdrawal(double aAmount) const;*/
  24. private:
  25. PersonPtr mPerson;
  26. //mAmount;
  27. double mBalance;
  28. size_t mAccountNumber;
  29. };
  30. //#endif
  31.  
  32. int main() {
  33. srand(static_cast<unsigned int>(time(NULL)));
  34. BankAccount ba ;
  35. cout <<"running";
  36. //ba.getAccountNumber();
  37.  
  38. //mAccountNumber = rand();
  39. cout << ba.getAccountNumber() << endl;
  40.  
  41.  
  42. //cin >> ba;
  43. //cout << ba;
  44.  
  45. return 0;
  46. }
  47.  
  48. /**
  49. ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount){
  50. cout << "A new account with account number";
  51. aOutput << aBankAccount.mAccountNumber << endl;
  52. cout << "has been created for";
  53. aOutput << aBankAccount.mPerson << endl;
  54. return aOutput;
  55. }
  56. istream& operator >> (istream &aInput, BankAccount& aBankAccount){
  57. cout << "Please type the intitial balance for account number: " << aBankAccount.mAccountNumber << endl;
  58. aInput >> aBankAccount.mBalance;
  59. aInput >> aBank.mPerson;
  60.  
  61. cout << "type the first name\n;
  62. aInput >> aBankAccount.mFName;
  63. cout << "type the last name\n;
  64. aInput >> aBankAccount.mLName;
  65. cout << "Type the address\n";
  66. aInput >> aBankAccount.mAddress;
  67. cout << "type the name of the state\n";
  68. aInput >> aBankAccount.mState;
  69. cout << "type the name of the city\n";
  70. aInput >> aBankAccount.mCity;
  71. cout << "type the zip code\n";
  72. aInput >> aBankAccount.mZip;
  73.  
  74. return aInput;
  75. }
  76. bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2){
  77. return(aBankAccount1.mPerson == aBankAccount2.mPerson &&
  78. aBankAccount1.mBalance == aBankAccount2.mBalance &&
  79. aBankAccount1.mAccountNumber == aBankAccount2.mAccountNumber);
  80. }*/
  81. BankAccount::BankAccount(){
  82. mPerson = new Person;
  83. mBalance = 5.00;
  84. mAccountNumber = rand();
  85. }
  86.  
  87. BankAccount::BankAccount(const Person &aPerson):mPerson( new Person), mBalance(5.00),mAccountNumber(rand()){
  88. }/**
  89. void BankAccount::setAccountOwner (const Person& aPerson){
  90. mPerson = new Person;
  91. }
  92. Person getAccountOwner () const{
  93. return mPerson;
  94. }
  95. double BankAccount:: withdrawal (double aAmount){
  96. double newBalance = Balance - aAmount;
  97. setBalance(newBalance);
  98. getBalance();
  99. //return aBalance;
  100.  
  101. }
  102.  
  103. double BankAccount::deposit (double aAmount){
  104. double newBalance = newBalance + aAmount;
  105. aBankAccount.setBalance(newBalance);
  106. aBankAccount.getBalance ();
  107.  
  108. }*/
  109. size_t BankAccount::getAccountNumber () const{
  110. //srand(static_cast<unsigned int>(time(NULL)));
  111. //mAccountNumber = rand();
  112. return mAccountNumber;
  113. }/**
  114. void BankAccount::setBalance (double aBalance) {
  115. mBalance = aBalance;
  116. }
  117. double BankAccount:: getBalance () const{
  118. return mBalance;
  119. }
  120. bool BankAccount::sufficientFundsToWithdrawal (double aAmount) const {
  121. if((aAmount - aBalance) > 5){
  122. return true;
  123. }
  124. else {
  125. return false;
  126. }
  127. }
  128. */

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;
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: Have problem using size_t type...

 
0
  #20
May 11th, 2008
my mistake didn't remove // in front of declaration of GetAccountNumber();
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC