0

I created a struct named products that contains multiple data types:

struct products{
    int ID;
    string Name;
    double Price;
    int Quantity;
};

Then in the main function, I created an array named details which utilizes the struct products:

int main(){
    struct products details[5];

Then I gave each array element data.

details[0] = {1, "Apple Juice", 12, 240};
details[1] = {2,"Bread", 10, 100};
details[2] = {3, "Chocolate", 5, 500};
details[3] = {4, "Dates", 50, 150};
details[4] = {5, "Eggs", 30, 360};

finally, I tried to print the values of the element at index 2:

cout<<details[2]; 

it gave me this error:

"Invalid operands to binary expression ('std::ostream' (aka 'basic_ostream') and 'struct products')"

Here is a picture of the whole code

enter image description here

4
  • 2
    you get the same error when you try to print a products that is not in an array Commented May 6, 2022 at 11:44
  • 3
    How can the compiler know how to print your struct? You have to define appropriate operator<< yourself. Commented May 6, 2022 at 11:47
  • Try cout<<details[2].ID and similar constructs for the other things you want to print Commented May 6, 2022 at 11:48
  • The array doesn't contain multiple data types - no arrays do. It contains elements of the type products. The array is also completely irrelevant; you would see the same error with products p; std::cout << p; Commented May 6, 2022 at 12:47

2 Answers 2

2

There are several ways of doing what you need, here's 3 of them:

  1. Overload << operator:
std::ostream& operator<< (std::ostream& os, const products& pr)
{
    return os << pr.ID << " " << pr.Name << " " << pr.Price << " " << pr.Quantity;
}

std::cout << details[2]; should now work as expected.


  1. Print the struct members directly:

    std::cout << details[2].ID << " " << details[2].Name 
        << " " << details[2].Price << " " <<  details[2].Quantity;
    

  1. Add a to_string() member function:
struct products{
    
//...

    std::string to_string() const
    {
        std::ostringstream os; // #include <sstream>
        os << ID << " " << Name << " " << Price << " " << Quantity;
        return os.str();
    }
};

Usage:

std::cout << details[2].to_string();
Sign up to request clarification or add additional context in comments.

Comments

0

i write you like code.

#include <iostream>
using namespace std;

struct H{
    int n;
    int b;
};

int main() {
    H h[] = {10, 20};
    cout << h[0] << endl; // you can not print it becouse it is array!
    return 0;
}

in this code i try to print struct! but i can not do it!

it like:

#include <iostream>
using namespace std;

struct H{
    int n;
    int b;
};

int main() {
    H h = {10, 20};
    cout << h << endl;
    return 0;
}

just try:

#include <iostream>
using namespace std;

struct H{
    int n;
    int b;
};

int main() {
    H h[] = {10, 20};
    cout << h[0].n << endl;
    cout << h[0].b << endl;
    return 

0; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.