1

I have to create a banking system program which adds, deletes, looks up, saves to file etc. For this I am required to use a Vector matching the private data member below and it is suggested to use the push_back() function to add new accounts.

The only way I could figure out to get data from the prompt to the vector (without a compilation error) was to create a constructor for Account which holds int accountID, int passcode, string firstname, string lastname, double balance. This constructor then initializes all the private data members in account with the same names listed using set() functions.

The problem is when I use the accounts_.size() function to see if there was a change in the vector, it always outputs 1.

How can I get the vector to input every additional value to the vector?

Not to mention I always end up with compilation errors when trying any vector functions...

Without writing out the actual code can you all point me in the right direction? thanks!

----edit - added main.cpp below and a sample of a cout loop which posts a compilation error

The problem I'm having is no matter how many times I input data, the size stays the same.. so while it compiles, it doesn't seem to be inputing data into the vector.

Also, when I try to see what's inside the vector I'll get a compilation error:

bankingsys.cpp(36): warning C4018: '<' : signed/unsigned mismatch
bankingsys.cpp(37): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Account' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(726): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(764): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(811): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(937): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(944): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(951): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(958): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(968): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>,Account>(std::basic_ostream<_Elem,_Traits> &&,_Ty)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ty=Account
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1085): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(186): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(192): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(199): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(206): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(226): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(260): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(280): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(305): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned int)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(325): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(345): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(366): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(386): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(407): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(427): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(447): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(467): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(487): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          while trying to match the argument list '(std::ostream, Account)'
1>
1>Build FAILED.
  • I don't see anything wrong with the code you posted. Where exactly is the error occuring? Also, what kinds of compilation errors are you getting? – Antimony Aug 09 '12 at 01:06
  • Can you show you main function? Is there have many BankingSystem object in your code? – laifjei Aug 09 '12 at 01:08
  • You need to separate your model from your IO. The model contains the data. Your IO, the conversation with the user, should be separate code. – walrii Aug 09 '12 at 01:51

2 Answers2

1

The problem is in your case statements ...

  switch ( inputcode )
  {     
  case 1:// Add Account
      {
      BankingSystem add;
      add.accAdd();
      }
      break;

  case 2:// Delete Account
      {
      BankingSystem del;
      del.accDelete();
      }
      break;

  case 3:// Account Inquiry
      {
      BankingSystem inquire;
      inquire.accInquiry();
      }
      break;        

  case 4: // Save Accounts to File
      {
      BankingSystem save;
      save.accSave();
      }
      break;

  case 5: // Load Accounts from File
      {
      BankingSystem load;
      load.accLoad();
      }
      break;       

You are creating and using a new instance of your BankingSystem class in each case statement, rather than using the one you created at the beginning of your main() function. That is why you always get a size() of 1. You want to use your myBankingSystem pointer instead ...

  switch ( inputcode )
  {     
  case 1:// Add Account
      myBankingSystem->accAdd();
      break;

  case 2:// Delete Account
      myBankingSystem->accDelete();
      break;

  case 3:// Account Inquiry
      myBankingSystem->accInquiry();
      break;        

  case 4: // Save Accounts to File
      myBankingSystem->accSave();
      break;

  case 5: // Load Accounts from File
      myBankingSystem->accLoad();
      break;       
Will
  • 3,500
  • 4
  • 30
  • 38
  • 1
    AAAAAAAAAAAHHHHHHHHHHH! Thank you all, jees, I was trying to recreate the wheel over here in the meantime...thanks Dinaiz, Will and everyone that responded. –  Aug 09 '12 at 02:08
  • @S.Moore you're most welcome; sometimes when you've been looking at a problem for so long, you miss the real source of the problem even when it's staring you right in the face. Happens to me all the time :) – Will Aug 09 '12 at 02:11
1

I don't know how you can have a problem at run time if your code doesn't compiles :) From what you posted, the line

cout << accounts_[i];

doesn't work because the operator << doesn't know who to handle Account type.

When you write cout << something , the compiler actually calls ostream& operator<<(something) . 'something' must be either a basic type (int, float, and so on ...) either a type for which you've defined an overload of ostream& operator<< .

So, without getting too much into details, you have 2 easy solutions here : - write a free function ostream& operator<< (ostream& out,const Account& val); which takes care of outputing the account, field by field.

ostream& operator<< (ostream& out,const Account& val);
{
 out << val.getID() << endl;
 out << val.getPass() << endl;
 out << val.getLast() << endl;
 out << val.getFirst() << endl;
 out << val.getBalance() << endl;
}

- write a method std::string Account::toString() which serializes an account into a string, and then, call

out << accounts_[i].toString();

That would solve your compilation error, and allow you to display the contents of an Account instance. For the reason the vector count is always 1, see Will's answer bellow.

HTH

Dinaiz
  • 2,213
  • 4
  • 22
  • 32
  • Now that the add function is at least responding I'll try this out next, also, I need to start using iterators (at least from the documentation) in order to delete elements from the vector using erase(). Is there a good reference for converting variables to iterators (keeping google busy, ha)? –  Aug 09 '12 at 02:16
  • Have a look at the algorithm find : http://www.cplusplus.com/reference/algorithm/find/ Hoever you must know that vectors are not the best for what you want to do, i.e random access. They're good when you need sequential access in a contiguous memory block. Have a look here http://www.cplusplus.com/reference/stl/ to see what other otpions are available. You could use a Set instead for example. For more details, you should post a new question in stack overflow because that's a totally separate topic. HTH Why did you chose to use a vector in the first place ? – Dinaiz Aug 10 '12 at 10:41