Hi there friends I have an inventory program that stores all the data entered by a user.
I am having trouble trying to output the infos entered. I tried to use for loop but no luck. Mine only outputs the last set of keyed in information. It will not output information inputted earlier. Please help me.

For Example:
How many products?
>>2
input product 1
>>shampoo
How many shampoo?
cin>>2
cin>>brand[1]
cin>>price
cin>>stock
cin>>sold

cin>>brand[2]
cin>>price
cin>>stock
cin>>sold

Now all of these inputted by a user will be outputted and must look something like this:

Shampoo[1] : rejoice
Price: Php71.25
Stock: 100
Sold: 26

Shampoo[2] : Palmolive
Price:Php70.50
Stock: 150
Sold: 35

Shampoo[3] : h&s
Price:Php99.75
Stock: 100
Sold: 50

PRODUCT[2] SOAP
How Many Soap? 2

Soap[1]: safeguard
Price:Php25.50
Stock: 250
Sold: 56

Soap[2]: bioderm
Price:Php18.50
Stock: 100
Sold: 10

But mine will only output this part:(which is the last set of inputted information)

cin>>brand[2]
cin>>price
cin>>stock
cin>>sold

=======================================================================================

Now here is my code:

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <windows.h>

using namespace std;

struct inventory
{
       string brand[10];
       string name[10];
       int numofprod;
       int price;
       int stock;
       int sold;
       int prodleft;
       int prodnum;
       int prod;   
}input;


      
int main()
{
  
    int ctr;
    int numofprod;
    cout<<"Enter Number of Products (1 to 10 only)"<<endl;
    cin>>input.numofprod;
    
    while  (input.numofprod>10)
    {
    cout<<"Invalid Input, Enter only numbers from 1 to 10"<<endl;
       
    cin>>input.numofprod;
  
    }
    
   for (int i=0;i<input.numofprod;i++)
   {
    for (int i=0;i<input.numofprod;i++)
    {
        cout<<"Enter Type of Product"<<endl;
        cin>>input.name[i];
        cout<<"How many "<<input.name[i]<<"?";
        cin>>input.prod;
        
        for(i=0;i<input.prod;i++)
        {
                                 
         cout<<input.name[i]<<" brand:"<<"["<<i+1<<"]"<<endl;
        cin>>input.brand[i];
        cout<<"Enter a price"<<endl;
        cin>>input.price;
        cout<<"Enter stock"<<endl;
        cin>>input.stock;
        cout<<"Enter sold"<<endl;
        cin>>input.sold;
        cout<<endl;
        }
        
        }
        
        }
       
cout<<"*********************INVENTORY SYSTEM C++.2***********************"<<endl;
                                      
  
  
  

    
 for (int i=0;i<input.numofprod;i++)
  {
    for (int i=0;i<input.numofprod;i++)
    {
        cout<<"Type of Product"<<endl;
        cout<<input.name[i];
        cout<<endl;
        cout<<"Num of Prod:"<<endl;
        cout<<input.prod;
        cout<<endl;
        
        
      for(i=0;i<input.prod;i++)
        {
                                 
        cout<<input.name[i]<<" brand:"<<"["<<i+1<<"]"<<endl;
        cout<<input.brand[i]<<endl;
        cout<<"price"<<endl<<endl;
        cout<<input.price<<endl;
        cout<<"stock"<<endl<<endl;
        cout<<input.stock<<endl;
        cout<<"sold"<<endl<<endl;
        cout<<input.sold<<endl;
        cout<<endl<<endl; 
        }
        }
        }   
       

    system ("pause");
    return 0;
    
}

Please help me guys...

Input is only a single structure, so every time you input new data, it's overwriting what was previously there.

You can remove the "input" after your struct declaration and create an array of inventories in main, then apply the input to the specific product number that is put in.

You don't have to use the letter 'i' for every for-loop variable. In fact, you shouldn't do that. You can pick any variable name you like, although it is idiomatic to use i,j,k,l.. letters, but that is just a convention. What you should not do is have several nested for-loops with the same variable name, this will be, at best, very confusing and, at worst, erroneous.

In any case, I see no reason to have nested loops here. That code makes no sense.

Also, your structure in which you hold the records is very weird. You have an array of names and brands for the products, but a single value for the rest. That makes no sense, at the end you will only have the last inputted set of values, because your inventory only stores one set of values. I would suggest having a structure for one item or product and then keeping an array of products. Maybe like so:

struct Product {
  string name;
  string brand;
  int stock;
  float price;
  //.. whatever else..
};

int main() {
  const size_t inventory_size = 10;
  Product inventory[inventory_size]; //keep an array of products as an inventory.

  //...
};

You don't have to use the letter 'i' for every for-loop variable. In fact, you shouldn't do that. You can pick any variable name you like, although it is idiomatic to use i,j,k,l.. letters, but that is just a convention. What you should not do is have several nested for-loops with the same variable name, this will be, at best, very confusing and, at worst, erroneous.

In any case, I see no reason to have nested loops here. That code makes no sense.

Also, your structure in which you hold the records is very weird. You have an array of names and brands for the products, but a single value for the rest. That makes no sense, at the end you will only have the last inputted set of values, because your inventory only stores one set of values. I would suggest having a structure for one item or product and then keeping an array of products. Maybe like so:

struct Product {
  string name;
  string brand;
  int stock;
  float price;
  //.. whatever else..
};

int main() {
  const size_t inventory_size = 10;
  Product inventory[inventory_size]; //keep an array of products as an inventory.

  //...
};

sir how will i do the output with these things located at the main?

Input is only a single structure, so every time you input new data, it's overwriting what was previously there.

You can remove the "input" after your struct declaration and create an array of inventories in main, then apply the input to the specific product number that is put in.

sir will you be able to show me how to do it? Like for example I'll make another structure and how I can make use of that struct to output the items in my inventory. Thank you.

You have mistaken at many places as Mike mentioned. The thing you can do is create array of struct inventory to store all products so that none overwrite other.

struct inventory
{string brand[10];
 string name; //no need of array
 int numofprod;
//and so on
};

and in main() you can write:

inventory input[10];

//take input numofprod

for(int i=0;i<numofprod;i++)
 for(int j=0;j<10;j++)
 {
  cout<<"Enter Type of Product"<<endl;
  cin>>input[i].name;
  cout<<"How many "<<input[i].name<<"?";
  cin>>input[i].prod;
 
        for(k=0;k<input[i].prod;i++)
        {
 
         cout<<input[i].name<<" brand:"<<"["<<i+1<<"]"<<endl;
        cin>>input[i].brand[i];
        }
 }

Similarly do changes for output display.

You have mistaken at many places as Mike mentioned. The thing you can do is create array of struct inventory to store all products so that none overwrite other.

struct inventory
{string brand[10];
 string name; //no need of array
 int numofprod;
//and so on
};

and in main() you can write:

inventory input[10];

//take input numofprod

for(int i=0;i<numofprod;i++)
 for(int j=0;j<10;j++)
 {
  cout<<"Enter Type of Product"<<endl;
  cin>>input[i].name;
  cout<<"How many "<<input[i].name<<"?";
  cin>>input[i].prod;
 
        for(k=0;k<input[i].prod;i++)
        {
 
         cout<<input[i].name<<" brand:"<<"["<<i+1<<"]"<<endl;
        cin>>input[i].brand[i];
        }
 }

Similarly do changes for output display.

sir can you demonstrate it to me by putting these codes on the code that I have made?

Re-reading your code, I'm thinking you want multiple structures, but correct if me I'm wrong.

You have products which have stock, price, sold... actual data.
You have a product type, such as shampoo, soap, tortillas, etc..., which have multiple products.

In the most basic sense, I think you could do something like:

struct product
{
    string brand;
    int stock;
    int sold;
    float price;
    // Other items that pertain to a product
}

struct productType
{
    product items[10];
    string name;
    int numOfProducts;
}

int main()
{
    productTypes inventory[10];
    int numOfTypes;
    cin >> numOfTypes;
    for(int i = 0; i < numOfTypes; i++)
    {
        cin >> inventory[i].numOfProducts;
        for(int j = 0; j < inventory[i].numOfProducts; j++)
        {
            //Take input for specific items
            cin >> inventory[i].items[j].brand;
            cin >> inventory[i].items[j].stock;
            ...
        }
    }
}

You should be able to figure out how to do the input using this method.

I think this is what you aimed to accomplish, but forgive me if I'm wrong.

Re-reading your code, I'm thinking you want multiple structures, but correct if me I'm wrong.

You have products which have stock, price, sold... actual data.
You have a product type, such as shampoo, soap, tortillas, etc..., which have multiple products.

In the most basic sense, I think you could do something like:

struct product
{
    string brand;
    int stock;
    int sold;
    float price;
    // Other items that pertain to a product
}

struct productType
{
    product items[10];
    string name;
    int numOfProducts;
}

int main()
{
    productTypes inventory[10];
    int numOfTypes;
    cin >> numOfTypes;
    for(int i = 0; i < numOfTypes; i++)
    {
        cin >> inventory[i].numOfProducts;
        for(int j = 0; j < inventory[i].numOfProducts; j++)
        {
            //Take input for specific items
            cin >> inventory[i].items[j].brand;
            cin >> inventory[i].items[j].stock;
            ...
        }
    }
}

You should be able to figure out how to do the input using this method.

I think this is what you aimed to accomplish, but forgive me if I'm wrong.

sir what was the purpose of

product items[10]

? product is not a data type right? But why was it placed there? help please

Correction to the code posted by sodabread. There should be ; in line 8 and line 15.

? product is not a data type right? But why was it placed there? help please

product inventory[10];

It's a way to declare objects for a structure.

struct product
{
    string brand;
    int stock;
    int sold;
    float price;
    // Other items that pertain to a product
}inventory [10];// Another way to declare objects for a structure.
commented: Thanks for covering me. Don't check much on weekends =) +7
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.