• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Map and accessing?

#include <map>
#include <iostream>
#include <string>

class sizer {
     public:
          sizer(){
            std::map <std::string, std::string> price;
            std::map <std::string, std::string> size;
            std::string part1 = "1450A";
            std::string price1 = "40.65";
            std::string dim1 = "7,34 1/2,3 1/2,";

            price[part1]=price1;
            size[part1]=dim1;
            };
          void accessPrice(std::string part){
            std::cout << price[part]; // I want this to print out the price for the part passed to it but it doesn't work.
            // What do I need to do to make this work?
               return
          }
};
0
turn123
Asked:
turn123
  • 4
  • 3
  • 2
  • +1
1 Solution
 
AlexFMCommented:
std::cout << price[part1];
0
 
jkrCommented:
Use

std::cout << price[part].second;
0
 
jkrCommented:
Oh, BTW, you should better use 'find()' in order to be able to handle the case that the key you want to look up does not exist in the map, e.g.

void accessPrice(std::string part){

 std::map <std::string, std::string>::iterator i = price.find(part);
 if ( i != price.end()) std::cout << i->second << std::endl;
   else
    std::cout << part << " could not be found" << std::endl;
}
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
AxterCommented:
Hi turn123,
> >std::cout << price[part]; // I want this to print out the price for the
> >part passed to it but it doesn't work.

Where is accessPrice getting price variable from.
In the code you posted, price is not declared within the scope that can be accessed by accessPrice function.

From looking at your class, you should either have price be a member variable, or you should modifiy the input arguments to  accessPrice, so that it includes price variable.

David Maisonave :-)
Cheers!
0
 
turn123Author Commented:
David,

Thats the problem is I'm unsure how to declare it so accessPrice can see it.  Can you post code on how to do that?

Thanks,
Turn123
0
 
jkrCommented:
>>Thats the problem is I'm unsure how to declare it so accessPrice can see it

Make it a member of your class:

class sizer {
    public:
         sizer(){

         std::string part1 = "1450A";
         std::string price1 = "40.65";
         std::string dim1 = "7,34 1/2,3 1/2,";

         price[part1]=price1;
         size[part1]=dim1;
         };
void accessPrice(std::string part){

std::map <std::string, std::string>::iterator i = price.find(part);
if ( i != price.end()) std::cout << i->second << std::endl;
  else
   std::cout << part << " could not be found" << std::endl;
}

protected:

         std::map <std::string, std::string> price;
         std::map <std::string, std::string> size;
};

0
 
turn123Author Commented:
Thanks for the tip jkr.  I'll use it that way.
0
 
turn123Author Commented:
Thank you jkr!
0
 
AxterCommented:
turn123,
????
Didn't I give that as the answer?
0
 
turn123Author Commented:
Axter,

What I needed was to know how to make it work not what was wrong.  I understood that price was not in scope for accessPrice but I didn't know how to make it so it would be in scope.  jkr provided the answer I needed for this question.

Looking back knowing how to do it I see that you gave me the correct answer first but I didn't understand what you were saying :-(.

Thank you for helping me though.  I really appreciate it.

Turn123
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now