sum map problem

How do I let the program calculate the sum of value and retrieve the match person. If both of the individuals reach sum of total value, they are matched. Like if total value is 300. If person 1 has 100 and person 2 has 200, they match. Otherwise they are not. If match, the user can enter a name and output will display the matched people or else the other statement.
Last edited on
Please post real code instead of this uncompilable garbage.

members.cpp: In function ‘int main()’:
members.cpp:24:30: error: ‘criterion’ was not declared in this scope
     while (infile >> name >> criterion >> phone){
                              ^
members.cpp:33:5: error: expected ‘;’ before ‘getline’
     getline (cin, entername);
     ^
members.cpp:37:59: error: ‘struct members’ has no member named ‘criterion’
         cout << it-> second. name << "\t" << it -> second.criterion
                                                           ^
members.cpp:48:13: error: ‘currCriterion’ was not declared in this scope
             currCriterion = it-> second. crit;
             ^
members.cpp:49:13: error: ‘sumCriterion’ was not declared in this scope
             sumCriterion = totalCrit - currCrit;
             ^
.
Last edited on
In function 'int main()':
53:9: warning: 'sumCriterion' may be used uninitialized in this function [-Wmaybe-uninitialized]

A golden rule: Always initialise your variables. You could declare and initialise on line 50.

If you turn on compiler warnings to a suitable level, you will be able to see these warnings yourself :+)

Good Luck !!
Last edited on
Where is it2 being incremented in the while loop on line 47 ?

Consider using a range based for loop when doing an entire container, no need for iterators:

1
2
3
for (auto MapItem&&  : memMap) { // uses forward reference, the safest & best option 
  // do something with MapItem 
}


Use better variable names, that's what confused you earlier.

Don't have using namespace std; it will bite you in the ass one day. Google to see why and what to do about it.

Pedantically:

Don't declare more than 1 variable on 1 LOC

Could some of those ints be unsigned?
Thanks for the kind suggestions. How would I store the sum criterion so that when I enter a person it will get a match person?
As the code stands now, you just need to do this on line 50, and delete line 44

int sumCriterion = totalCriterion - currCriterion;

If you are going to use the range based for loop, MapItem is like an already derefrenced iterator, so you can use it with the dot operator.
Line 47 contained a typo that was meant for it2.

I'll take note of the using namespace std for future projects. Never was taught the dangers of that before.

What would be the positive for using unsigned ints?

I have redesigned the program so far and only the first individual is displaying.

Last edited on
.
Last edited on
can anyone help?
Hi,

What is it that doesn't work with the code? Saying something compiles but doesn't work isn't helpful.

With the forwarding reference in the for loop, I had 2 & , as in :

for (auto&& map_criterion : mem_map)

It won't make any difference in this example, 1 & means pass by reference, 2 means a forwarding reference which will work with lvalues, rvalues, const and non const. Google it if you are interested.
Thanks I have updated the code below.The program is only displaying the first individual when I try to display everyone. When a username is entered is this the right way to find person?
Should I store int current_criterion , int match_criterion = 500, int desired_criterion in vector?

Last edited on
The program is only displaying the first individual when I try to display everyone. When a username is entered is this the right way to find person?


Well you only search for one, there is no loop in lines 49 to 66.

Should I store int current_criterion , int match_criterion = 500, int desired_criterion in vector?


If you think there is an advantage in doing that. But they would be fine as variables in the main function, not in the member struct.
the lines 37 to 39 are only displaying the first individual

1
2
3
 for (auto && map_item : mem_map) {
        cout << map_item.second.name << "\t" << map_item.second.criterion << "\t" << map_item.second.phone;
      }


how to fix this? could you post a solution to this. i am struggling with this alot.
Well try to use the debugger. Hopefully there is a GUI one in your IDE. Set up a watch list of variables, step through the program line by line, deduce what goes wrong.
im using gdb to debug and its producing an [Inferior 1 (process 31434) exited normally]. don't know what that means.
Do you have Google? I have: https://www.cs.cmu.edu/~gilpin/tutorial/
where does the article explain about [Inferior 1 (process 31434) exited normally]? debugger is producing no stack with backtrace so i dont know where to narrow it down.
How are you compiling? Do you have debug information?

Did you try setting a breakpoint?
ty for the patience. i found the error and actually got the intended output. took a while but nothing like a cold beer after some hard work.
Last edited on
Topic archived. No new replies allowed.