Boolean Trouble (Simple)

I'm writing a program using a map that only allows keys/values to be used once.I've tried to set up an iterator that reads through the values in my map to check if a value passed through the function is already present. However, I keep changing stuff and it always returns "false" and it should be "true".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 bool BiMap::insert(string key, string value){
    
    bool found_value = false;

    for(it_type iterator = data_pairs.begin(); iterator != data_pairs.end(); iterator++){ //Checks if value is not already in map
        
        found_value = (iterator->second == value);//Think this is the problem
        
        if(found_value){ //Here too
            
            if(data_pairs.find(key) == data_pairs.end()){ //Checks for key 
                                               
               data_pairs[key] = value; //adds the new pair to map
               return true; //this piece by itself works properly.
            }
        }
        
        else{
            return false;
        }   
    }
}


I'm guessing it's something really simple I'm screwing up?

*Currently, the map is blank.
Last edited on
What you are doing is if it doesn't find it after the first element it returns false There should be no else statement and instead put the return false outside of the for loop pretty much means you loop through all of them then if none of them have been found return false.

*another example: Just like finding a letter in a word you must check each letter before jumping to conclusion that it is not in the word.
Last edited on
Putting return false outside of the for loop is just going to overwrite what happened inside the for loop isn't it?

I just got rid of the else statement like you suggested and it's still not going through and "making it" to return true. I guess I should have also specified in the OP that the function returns true if both the key and value are "new" and false if either of the 2 are already present somewhere in the map.

I'm still thinking there is either something wrong with how I set the for loop up or how I'm using the bool found_value inside the function.
Last edited on
Oh yeah I misread sorry. also shouldn't line 9 be !found_value.
Since you want to add it if and only if the key and value are not found.


You could also combine to one if statement

if(iterator->second != value && data_pairs.find(key) == data_pairs.end() )
Changed to:
1
2
3
4
5
6
7
8
9
10
11
bool BiMap::insert(string key, string value){

    for(it_type iterator = data_pairs.begin(); iterator != data_pairs.end(); iterator++){ //Checks if value is not already in map

       if(iterator->second != value && data_pairs.find(key) == data_pairs.end()){ //Checks if key already exists in map
      
         data_pairs[key] = value;
         return true;
       }
    }
}


Also tried this just because:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 bool found_value = false;

    for(it_type iterator = data_pairs.begin(); iterator != data_pairs.end(); iterator++){ 
        
        found_value = (iterator->second == value);
        
        if(!found_value){
            
            if(data_pairs.find(key) == data_pairs.end() && iterator->second != value){ 
        
               data_pairs[key] = value;
               return true;
            }
        }
    }     
}


Both of these are still just returning false and not picking up true. :\
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <map>

bool insert( const std::string &key , const int value , std::map<std::string , int> &m );

int main()
{
	// your code goes here
	std::map<std::string,int> m;
	m["apple"] = 1;
	m["grape"] = 2;
	
	insert( "orange" , 1 , m ); //value already found should not be added
		
	insert( "apple" , 3 , m ); //key already found should not be added
		
	insert( "orange" , 3 , m ); //key is not found and value is not found should be added
		
	for( const auto &it : m )
		std::cout << it.first << " = " << it.second << std::endl;
	return 0;
}

bool insert( const std::string &key , const int value , std::map<std::string , int> &m )
{
	bool found = false;
	if( m.find(key) != m.end() )
	{
		found = true;
	}
	else
	{
		for( const auto &it : m ) //for( std::map<std::string,int>::iterator it = m.begin() , it != m.end(); ++it )
		{
			if( it.second == value )
			{
				found = true;
			}
		}
	}
	
	if( !found )
	{
		m[key] = value;
		std::cout << "The item was added" << std::endl;
	}
	else
	{
		std::cout << "The item was not added" << std::endl;
	}
		
	return( found );
}


http://ideone.com/si2gXt
You win!

Much appreciated.
Last edited on
Topic archived. No new replies allowed.