Originally Posted by
Salem
Mmm, is this giving it away?
No this is fine, I've just modified it a bit cause I want the user to enter in a number for both the category and item instead of having to type in everything word for word. I'm also trying to get my program to get the total price of item after its gotten all of the prices together. I'd rather not have a function that takes "x" amount of parameters,since I have so many prices, so I've decided to have my function, getTotal, take no parameters and get the total from adding private variables. The issue I'm having is after the price is found in ,getPrice, I want the price to be stored in another function, storeItemPrice, so that my getTotal function can work correctly. I get this error:
Code:
"main.cpp:103:29: error: cannot convert 'std::map<int, double>' to 'double' in initialization"
when I initialize this in getItemPrice:
Code:
double itemprice = it->second;
storeItemPrice(category,itemprice);
Here's my code:
Code:
#include<iostream>
#include<string>
#include<limits>
#include<map>
using namespace std;
class CPUCatalog{
private:
double cpu_price,RAM_price,CDD_price,
HD_price,modem_price,router_price,
nwcard_price,lapsize_price;
public:
//create constructor
map<int,map<int,double> > Items;
CPUCatalog(){
//Stores the CPU Processors
Items[1][1] =36.00;//AMD Sempron 140
Items[1][2] = 75.00;//Intel Pentium G2100T
//Stores the memory
Items[2][1] = 40.00;//2GB
Items[2][2] = 120.00;//6GB
//Hardrive sizes
Items[3][1] = 8.00;//40GB
Items[3][2] = 100.00; //1TB
//Stores a CD Drive
Items[4][1] = 20.00;//CD-ROM
Items[4][2] = 35.00;//CD-RW
Items[4][3]= 50.00;//DVD
//modems
Items[5][1] = 85.00;//Linksys 3.0
Items[5][2] = 115.00;//Netgear N600
//network adapter cards
Items[6][1] = 10.00;//Intellinet 150N
Items[6][2]=14.00;//Wintec PCI
//wireless routers
Items[7][1] = 155.00;//Asus N66U
Items[7][2] = 70.00;//D Link 655
//laptop sizes
Items[8][1]= 40.00; //14'
Items[8][2]= 70.00;//16'
}
void showCategories(){
cout << "CPU item categories:" <<"\n";
cout << "(1) CPU" << "\n";
cout << "(2) RAM" << "\n";
cout << "(3) CD drive" << "\n";
cout << "(4) Hard drive" << "\n";
cout << "(5) Modem" << "\n";
cout << "(6) Network card" << "\n";
cout << "(7) Wireless router" << "\n";
cout << "(8) Laptop size" << "\n";
}
/*
Stores the prices for each item found in getItemPrice.
*/
void storeItemPrice(int category,int item){
if(category == 1){
cpu_price =item;
}else if (category == 2){
RAM_price = item;
} else if(category == 3){
CDD_price = item;
}else if (category == 4){
HD_price = item;
}else if (category == 5){
modem_price = item;
}else if (category ==6){
router_price = item;
}else if (category == 7){
nwcard_price = item;
}else if(category == 8){
lapsize_price = item;
}
}
//create getPrice
void getItemPrice(int category,int item){
map<int,map<int,double> >::iterator it;
it = Items.find(category);
if ( it != Items.end() ) {
map<int,double>::iterator it2;
it2 = it->second.find(item);
if ( it2 != it->second.end() ) {
cout << "Item costs: " << it2->second << endl;//double
double itemprice = it->second;
storeItemPrice(category,itemprice);
} else {
cout << "Item " << " doesn't exist" << endl;
}
} else {
cout << "category " << " doesn't exist" << endl;
}
}
/*
Uses private variables initialized from the "getPrice" function
* to get the total.
*/
void getTotal(){
double total =
cpu_price + RAM_price +
CDD_price + HD_price + modem_price +
router_price + nwcard_price;
cout << "total cost: $" << total << endl;
}
};
int main()
{
CPUCatalog c;
int item;
int category;
cout << "Enter in CPU category number: ";
cin >> category;
cout << "Choose an item: (1) AMD Sempron 140 or "
<< "(2) Intel Pentium G2100T: " << endl;
cin >> item;
c.getItemPrice(category,item);
cout << "Enter in RAM category number: ";
cin >> category;
cout << "Choose an item: (1) 2GB or (2) 6GB: " << endl;
cin >> item;
c.getItemPrice(category,item);
cout << "Enter in hard drive category number: ";
cin >> category;
cout << "Choose an item: (1) 40GB or (2)160GB" << endl;
cin >> item;
c.getItemPrice(category,item);
cout << "Enter in CD Drive category number: ";
cin >> category;
cout << "Choose an item: (1) CD-Rom (2) CD-RW, or (3) DVD" << endl;
cin >> item;
c.getItemPrice(category,item);
cout << "Enter in modem category number: ";
cin >> category;
cout << "Choose an item: (1) Linksys 3.0 or (2)Netgear N600" << endl;
cin >> item;
c.getItemPrice(category,item);
cout << "Enter in network card category number: ";
cin >> category;
cout << "Choose an item: (1) Intellinet 150N or (2) Wintec PCI" << endl;
cin >> item;
c.getItemPrice(category,item);
cout << "Enter in router category number: ";
cin >> category;
cout << "Choose an item: (1) Asus N66U or (2) D Link 655" << endl;
cin >> item;
c.getItemPrice(category,item);
cout << "Enter in laptop size category number: ";
cin >> category;
cout << "Choose an item: (1) 14' or (2) 16'" << endl;
cin >> item;
c.getItemPrice(category,item);