displaying the name of the most expensive product

Can you help me in displaying the name of the most expensive product ?



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
55
56
57
58
#include <iostream>
#include <string>
using namespace std;

struct inventory

{
	int ID; //Product ID
	string Name; //Product Name
	float price ;    
	int Amount;   //Amount in stock
	string Desc;  //Description
};

int main()

 {
 	inventory pro1, pro2, pro3, pro4, pro5;
 	pro1.Name = "pen";
	pro1.ID=00001;
	pro1.price= 1.5;
	pro1.Amount=1000;
	pro1.Desc= "an instrument for writing with ink";
	
	pro2.Name = "eraser";
	pro2.ID=00002;
	pro2.price= 1;
	pro2.Amount=1000;
	pro2.Desc= " a piece of soft rubber";
	
	pro3.Name = "pencil";
	pro3.ID=00003;
	pro3.price= 2;
	pro3.Amount=1500;
	pro3.Desc= "an instrument for writing or drawing";
	
	pro4.Name = "toy";
	pro4.ID=00004;
	pro4.price= 4;
	pro4.Amount=500;
	pro4.Desc= "an object for a child to play ";
	
	pro5.Name = "comb";
	pro5.ID=00005;
	pro5.price= 1.75;
	pro5.Amount=300;
	pro5.Desc= "a strip of plastic, metal, or wood .";
	
	
	 cout <<   	pro1.Name << "\t" << pro1.ID << "\t" << pro1.price << "\t" <<	pro1.Amount <<"\t" << pro1.Desc <<"\n";
	 cout <<   	pro2.Name << "\t" << pro2.ID << "\t" << pro2.price << "\t" <<	pro2.Amount <<"\t" << pro2.Desc <<"\n";
	 cout <<   	pro3.Name << "\t" << pro3.ID << "\t" << pro3.price << "\t" <<	pro3.Amount <<"\t" << pro3.Desc <<"\n";
	 cout <<   	pro4.Name << "\t" << pro4.ID << "\t" << pro4.price << "\t" <<	pro4.Amount <<"\t" << pro4.Desc <<"\n";
	 cout <<   	pro5.Name << "\t" << pro5.ID << "\t" << pro5.price << "\t" <<	pro5.Amount <<"\t" << pro5.Desc <<"\n";
	
 		 
			return 0;
}
Last edited on
Have you worked with arrays or vectors?

Put your products in an array or vector, then it's a simple matter to iterate through the array or vector to find the most expensive product.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
I'd say doing it with arrays would be best as suggested above, or just use a lot "if" functions :D
I have never worked with vectors and I have a problem in understanding and using arrays , and Can I use if" with 5 numbers ?
Can I use if" with 5 numbers ?

You mean: "Can one make a compound boolean expression?"
Yes, but you don't want to.

1. Lets assume that the pro1 is expensive.
2. If pro2 is more expensive, then pro2 is the pricey one.
3. If pro3 is more expensive, then pro3 is the pricey one.
4. If pro4 is more expensive, then pro4 is the pricey one.
5. If pro5 is more expensive, then pro5 is the pricey one.
6. You have the most expensive item.

Or:
http://www.cplusplus.com/reference/algorithm/max_element/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
using namespace std;

struct inventory

{
	int ID; //Product ID
	string Name; //Product Name
	float price ;    
	int Amount;   //Amount in stock
	string Desc;  //Description

};

int main()

 {
 	inventory pro1, pro2, pro3, pro4, pro5;
 	pro1.Name = "pen";
	pro1.ID=00001;
	pro1.price= 1.5;
	pro1.Amount=1000;
	pro1.Desc= "an instrument for writing with ink";
	
	pro2.Name = "eraser";
	pro2.ID=00002;
	pro2.price= 1;
	pro2.Amount=1000;
	pro2.Desc= " a piece of soft rubber";
	
	pro3.Name = "pencil";
	pro3.ID=00003;
	pro3.price= 2;
	pro3.Amount=1500;
	pro3.Desc= "an instrument for writing or drawing";
	
	pro4.Name = "toy";
	pro4.ID=00004;
	pro4.price= 4;
	pro4.Amount=500;
	pro4.Desc= "an object for a child to play ";
	
	pro5.Name = "comb";
	pro5.ID=00005;
	pro5.price= 1.75;
	pro5.Amount=300;
	pro5.Desc= "a strip of plastic, metal, or wood .";
	
	
	 cout <<   	pro1.Name << "\t" << pro1.ID << "\t" << pro1.price << "\t" <<	pro1.Amount <<"\t" << pro1.Desc <<"\n";
	 cout <<   	pro2.Name << "\t" << pro2.ID << "\t" << pro2.price << "\t" <<	pro2.Amount <<"\t" << pro2.Desc <<"\n";
	 cout <<   	pro3.Name << "\t" << pro3.ID << "\t" << pro3.price << "\t" <<	pro3.Amount <<"\t" << pro3.Desc <<"\n";
	 cout <<   	pro4.Name << "\t" << pro4.ID << "\t" << pro4.price << "\t" <<	pro4.Amount <<"\t" << pro4.Desc <<"\n";
	 cout <<   	pro5.Name << "\t" << pro5.ID << "\t" << pro5.price << "\t" <<	pro5.Amount <<"\t" << pro5.Desc <<"\n";
	 
	 {
	 if       ( (pro1.price > pro2.price)  && (pro1.price > pro3.price) && (pro1.price > pro4.price) && (pro1.price > pro5.price) )

	 	 cout << "the name of the most expensive product" <<"\n" <<"pen";
	 else if  ( (pro2.price > pro1.price) &&  (pro2.price > pro3.price) && (pro2.price > pro4.price) && (pro2.price > pro5.price) )
	 
	  	 cout << "the name of the most expensive product" <<"\n" <<"eraser";
	  	 
	  else if  ( (pro3.price > pro1.price) &&  ( pro3.price > pro2.price) && (pro3.price > pro4.price) && (pro3.price > pro5.price) )
	 
	  	 cout << "the name of the most expensive product" <<"\n" <<"pencil";
	  else if  ( (pro4.price > pro1.price ) && (pro4.price > pro2.price) && (pro4.price > pro3.price ) && (pro4.price > pro5.price) )

	  	 cout << "the name of the most expensive product" <<"\n" <<"toy";
	  else if  ( (pro5.price > pro1.price )   && (pro5.price > pro2.price) && (pro5.price > pro3.price)  && (pro5.price > pro4.price )  )
	  
	  	 cout << "the name of the most expensive product" <<"\n" <<"comb";
 		 }
			return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Product products[5];
product[0] = prod1; // First component of array
product[1] = prod2;
product[2] = prod3;
product[3] = prod4;
product[4] = prod5;

Product mostExpensiveProduct;
for (int i=0; i < 5; i++)
{
Product currentProduct = products[i];
if (currentProduct.price > mostExpensiveProduct.price)
{
mostExpensiveProduct = currentProduct;
}
}
}
@moonman239:
You are using uninitialized variable.


1
2
3
4
5
6
7
8
Product mostExpensiveProduct = pro1; // Lets assume that the pro1 is expensive.
if ( mostExpensiveProduct.price < pro2.price ) { // If pro2 is more expensive
  mostExpensiveProduct = pro2;       // then pro2 is the pricey one
}
...

std::cout << "the name of the most expensive product\n"
std::cout << mostExpensiveProduct.Name << '\n';


Now, lets pretend that your shop has million products in its inventory.
Do you really want to copy-paste-edit an if-clause for each?
What if you get three more products? Will you again edit the code and recompile the program?

No. You want a generic and dynamic program. The std::vector, std::max_element & co helps you do that, so learn to use them
Topic archived. No new replies allowed.