SamGoat
SamGoat

Reputation: 13

comparison between mapvalue and iterator c++

I want to know if there is a way to compare two values to see if they are equal. I have a map where the key is int and the type is a class. And I want to find a specific number to see if it exists in the map and if it does a certain action should be done.

I.e: I have a bankaccount that I am storing in my map container. And i want to change the value of the balance for that account.

the int in my mapdeclaration is accountnumber.

map<int, class>::iterator it;
map<int, class>::myMap;



void Deposit(){
int banknbr;
int amount;
cout << "What is your acc numb?" << endl;
cin >> bankNbr;
for(it = myMap.begin(); it != myMap.end(); it++){
   if(it->first==myMap[bankNbr]){
     cout << "How much money do you wish to deposit?" << endl;
     cin >> amount;
     deposit(); // balance = balance + amount;

or anything like this perhaps. If I store two accounts i overwrite the balance of the first account. So if the first account has the balance 100 and the second 200 and I want to deposit 100 money in the first account, my value becomes 300 which is incorrect.

My code now is like this:

void deposit(int number){
int amount;
for(it = myMap.begin(); it != myMap.end(); it++){
    myMap.find(number);
    if(it != myMap.end(){
        cout << "How much money do you wish to deposit?"<<endl;
        cin >> amount;
            acc.deposit(amount); //acc is my own implemented class
            cout << "Your balance is: " << acc.getBal(number) << endl; //get bal is return balance
}
            }



}

Upvotes: 1

Views: 87

Answers (2)

Jonathan Mee
Jonathan Mee

Reputation: 38919

Here is your problem:

for(it = myMap.begin(); it != myMap.end(); it++){ // it will iterate through all acounts
    myMap.find(number); // find returns an iterator BUT YOU DON'T ASSIGN IT
    if(it != myMap.end(){ // For each iteration this will be true, cause it is only changed in the for statement

find is the right way to handle this:

void Deposit(){
    int banknbr;

    cout << "What is your acc numb?" << endl;
    cin >> bankNbr;

    auto it = myMap.find(bankNbr);

    if(it != myMap.end()){
        int amount;

        cout << "How much money do you wish to deposit?" << endl;
        cin >> amount;
        it->second.deposit(amount);
        cout << "Your balance is: " << it->second.getBal() << endl; //get bal is return balance
    }
}

It looks like you had some misconceptions about how a map works so let me know if this is unclear.

Upvotes: 0

Ankit
Ankit

Reputation: 1105

You can use the find method of map to find whether the key exsists or not.

The find method returns an iterator if a key exists otherwise map::end

it=mymap.find(number);
if(it!=myMap.end()){
   //your code 
}

You can find more details of this method here http://www.cplusplus.com/reference/map/map/find/

 //for(it = myMap.begin(); it != myMap.end(); it++){
it = myMap.find(number);   //assign "it" 
if(it != myMap.end(){
    cout << "How much money do you wish to deposit?"<<endl;
    cin >> amount;
        acc.deposit(amount); //acc is my own implemented class
        cout << "Your balance is: " << acc.getBal(number) << endl; //get bal is return    balance
  }
       // }

Upvotes: 2

Related Questions