Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What are the best ways to use cin with class using vector.push_back function?
I know if it's normal vector I can do it like this and then use a sentinel code to prompt the end
C#
int myint;
vector<int> y;
cout << "vector integer" << endl;
while ((cin >> myint) && myint != 9999)
    y.push_back(myint);
for (int i = 0; i < y.size(); i++)
    cout << "y = " << y[i] << endl;*

Here's my code:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

class Account{
protected:
    int accno;
    double balance;
public:
    Account(int accno, double balance) : accno(accno), balance(balance){};
    double getBalance() {return balance;}
    void setBalance(double balance){ this->balance=balance; }
    int getAccno() {return accno;}
    void setAccno(int accno){ this->accno=accno; }
    void deposit(double amount){
        balance = balance + amount;
    }
};

class Saving : public Account{
public:
    Saving () : Account(accno,balance){};
    Saving (int accno, double balance) : Account(accno,balance){};
    bool withdraw (double amount){
        if (balance < amount){
            cout << "Insufficient funds";
            return false;
        }
        else{
            balance = balance - amount;
            return true;
        }
    }
    void compute_interest(){
        balance = balance * 1.05;
    }
    void print(){
        cout << "ID: " << accno << "     balance: " << balance << endl;
    }

    friend istream& operator >>(istream& input, Saving& account){
        cout << "ID: ";
        input >> account.accno;
        cout << "balance: ";
        input >> account.balance;
        return input;
    }
    friend ostream& operator <<(ostream& output, const Saving& account){
        output << "ID: " << account.accno << "     balance: " << account.balance << endl;
    }
    //friend bool operator !=(Saving a, int b)

};

int main()
{
    Saving saving[5];
    cout << "saving array" << endl;
    for (int i = 0; i < 5; i++){
        cin >> saving[i];
        saving[i].print();
        cout << endl;
    }

    Saving myvec;
    vector<Saving> savingV;
    cout << "saving vector" << endl;

    while ((cin >> myvec) && (myvec.getAccno() != 9999)){
        int i = 0;
        savingV.push_back(myvec);
        savingV[i].print();
        cout << endl;
        i++;
    }

    return 0;
}

As you can see in the main function i tested first using an array.
Basically what I really want to do using cin with class(vector not array) is to keep inserting value until a sentinel value or character or string is prompted.

& I'm still having trouble designing operator overloading functions (like the !=)
This is what I thought of doing by overloading the "!=" operator
while ((cin >> myvec) && myvec != 9999);

But idk whether it can be done.

What I have tried:

do{
int i = 0;
cin >> myvec;
savingV.push_back(myvec);
savingV[i].print();
cout << endl;
i++;
} while (cin >> myvec && ((myvec.getAccno() != 9999) || (myvec.getBalance() != 9999)));
Posted
Updated 7-Aug-16 0:36am

1 solution

You basically have the correct idea. You can either write a "canonical" operator != as:

C++
bool operator !=(Saving const& lhs, Saving const& rhs)
{
    // details omitted
}

and then declare a sentinel value (e.g. Saving sentinel(9999, -1.0)), or you can write an operator that compares Saving to int (or any other type):
C++
bool operator !=(Saving const& lhs, int rhs)
{
    // details omitted
}


Either method should work.

Note that if you declare operator !=, it is a good practice to also declare operator ==
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900