Hi all,

I need program that has the following:

a) Create a structure that represents the product in the inventory.

b) Insert 5 different product details into the inventory.

c) Display the name of the most expensive product

and the main information is:
Product ID: 5 characters

Product Name: 50 characters

Price : float

Description: 80 characters.

Amount in stock: integer

Thank you very much

That's the spec of your application. But you didn't show the code you've made so far and tell what's not working for you.

I'm guessing you only asked for the answer, rather than for help with solving a problem. Giving the completed code is not going to advance your coding skills.

Thnak you for your reply, i have some problems ,, yes ,, and here is my code

#include <iostream>
#include <string>
using namespace std;

struct inventory

{
    int Product_ID;
    int Product_Name;
    float price;
    int Amount_in_stock;
};

int main()

 {
    inventory p1, p2, p3, p4, p5;
    p1.Product_Name = "Disply Screen";
    p1.Product_ID=00001;
    p1.price= 3000;
    p1.Amount_in_stock=70;


    return 0;
}

the problem shown in

p1.Product_Name = "Disply Screen";

and i dont know why its happend

Product_Name is defined as an int and you are trying to assign a string to it. My C is a little rusty but it seems to me that you want to declare Product_Name as a char array and use strcpy to copy a string value to it, or declare it as a pointer to a string and use the assignment as stated to assign the address of the literal string.

Product_Name is defined as an int and you are trying to assign a string to it. My C is a little rusty but it seems to me that you want to declare Product_Name as a char array and use strcpy to copy a string value to it, or declare it as a pointer to a string and use the assignment as stated to assign the address of the literal string.

The OP has it tagged as C++, not C, and is including the string library, so to fix the error, all the OP has to do is declare Product_Name as a string.

That solves the actual compilation error. However, according to the spec:

Product ID: 5 characters

Product Name: 50 characters

Price : float

Description: 80 characters.

Amount in stock: integer

It does appear that the OP is supposed to use C-Style strings, not C++-style strings, so Reverend Jim's idea of using a function from the cstring library may be more inline with the assignment. I would clarify with your teacher what he/she wants and go with that, which, assuming that the spec is what is actually what's intended, would be C-Strings (character arrays), not strings, which means you can get get rid of your #include <string> directive and include the cstring library instead. You will also need to change your structure beyond what has been mentioned, to match the spec. Your structure does not currently match the spec.

 struct inventory
{
    int Product_ID;
    int Product_Name;
    float price;
    int Amount_in_stock;
};

I would also google "C-String tutorial" and learn about C-strings, then come back to the assignment since it appears from this thread that you are new to them.

it seems to me that you want to declare Product_Name as a char array and use strcpy to copy a string value to it, or declare it as a pointer to a string and use the assignment as stated to assign the address of the literal string.

I would go with your first idea (declaring ProductName as a character array inside the structure and deep copying a C-string to that character array using strcpy or strncpy) over the second (declaring it as achar* in the structure and shallow copying to that pointer with an = assignment) because the second way is more advanced and has more potential pitfalls to a C/C++ beginner due to memory management/scoping issues. Defining the character buffer in the structure itself seems more in line with the spec and avoids the potential problem of the string literal going out of scope (and the memory accidentally being freed prematurely) before the inventory object does, among other things.

C++-style strings are nice because it handles all that for you under the hood.

int Product_Name;
The problem is in this code.You can't save a name into a Integer variable.
string Product_Name;
use this one..

**Hey .. Check out my lase code
**

#include <iostream>
#include <string>
using namespace std;
struct inventory
{
int Product_ID;
string Product_Name;
float price;
int Amount_in_stock;
string Description;
}p[5];
void sortt()
{
struct inventory EE;
for(int i=0;i<5;i++)
{
for(int j=0 ;j<5;j++)
if(p[i].price<p[j].price)
{
EE.price=p[j].price;
p[j].price=p[i].price;
p[i].price=EE.price;
}
}
}
int main()
{

p[0].Product_Name = "Disply Screen";
p[0].Product_ID=00001;
p[0].price= 3000;
p[0].Amount_in_stock=70;
p[0].Description= "This item made in KSA";
p[1].Product_Name = "Radio";
p[1].Product_ID=00002;
p[1].price= 750;
p[1].Amount_in_stock=170;
p[1].Description= "This item made in UAE";
p[2].Product_Name = "Laptop";
p[2].Product_ID=00003;
p[2].price= 4750;
p[2].Amount_in_stock=100;
p[2].Description= "This item made in USA";
p[3].Product_Name = "Mobile";
p[3].Product_ID=00004;
p[3].price= 2600;
p[3].Amount_in_stock=1000;
p[3].Description= "This item made in China";
p[4].Product_Name = "AC";
p[4].Product_ID=00005;
p[4].price= 2000;
p[4].Amount_in_stock=900;
p[4].Description= "This item made in Malaysia";
sortt();
cout << p[4].price;
return 0;
}

Is there any problem now??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.