#include<iostream>
    #include<conio.h>
    using namespace std;

    struct marks{
        int phy[10];
        int math[10];
        int chem[10];
    };
    int main(){

        struct marks m[20];
        int i,n;
        cout<<"Enter the number of students : " << endl;
        cin >>n;
        for(i=0;i<n;i++){
            cout<<"\nEnter Physics marks: " << endl;
            cin >> m[i].phy;  [error here]

            cout<<"\nEnter Maths marks : "<< endl;
            cin >> m[i].math;

            cout<<"\nEnter Chemistry marks : "<< endl;
            cin >> m[i].chem;
        }

        cout<<"\nStudents mark details : \n"<< endl;

        for(i=0;i<n;i++){

            cout<<"\nMarks of Student"<< i+1 <<" : "<< endl;
            cout<<"\nPhysics marks: "<< m[i].phy<< endl;
            cout<<"\nMaths marks: "<< m[i].math<< endl;
            cout<<"\nChemistry marks: "<< m[i].chem<< endl;
            cout<<"\n"<< endl;

        }
        return 0;
    }

//There is an error in the first cin >> in the for loop. it shows no match for 'operator >>' (operand type is 'std::istream{aka std::basic_istream<char> and 'int[10]')

Recommended Answers

All 2 Replies

I'll guess you really meant the following as you didn't indicate where to store in the arrays phy, math and chem.

Try this:

    #include<iostream>
    #include<conio.h>
    using namespace std;

    struct marks{
        int phy[10];
        int math[10];
        int chem[10];
    };
    int main(){

        struct marks m;
        int i,n;
        cout<<"Enter the number of students : " << endl;
        cin >>n;
        for(i=0;i<n;i++){
            cout<<"\nEnter Physics marks: " << endl;
            cin >> m.phy[i]; 

            cout<<"\nEnter Maths marks : "<< endl;
            cin >> m.math[i];

            cout<<"\nEnter Chemistry marks : "<< endl;
            cin >> m.chem[i];
        }

        cout<<"\nStudents mark details : \n"<< endl;

        for(i=0;i<n;i++){

            cout<<"\nMarks of Student"<< i+1 <<" : "<< endl;
            cout<<"\nPhysics marks: "<< m.phy[i]<< endl;
            cout<<"\nMaths marks: "<< m.math[i]<< endl;
            cout<<"\nChemistry marks: "<< m.chem[i]<< endl;
            cout<<"\n"<< endl;

        }
        return 0;
    }

To elaborate on RProffitt's answer: the error message is saying that you are trying to read the whole array phy rather than a specific (indexed) element of phy. The error is saying that there is no defined cin>> operator for reading an entire array of int. Since this clearly isn't what you were trying to do, the answer RProffitt gave of iterating over the data arrays rather than an array of the marks structure illustrates what you need.

An alternative approach would be to have individual values in the marks struct, and iterate over an array of marks.

#include<iostream>
using namespace std;

struct marks{
    int phy;
    int math;
    int chem;
};

int main(){
    struct marks m[20];
    int i,n;
    cout<<"Enter the number of students : " << endl;
    cin >>n;
    for(i=0;i<n;i++){
        cout<<"\nEnter Physics marks: " << endl;
        cin >> m[i].phy;

        cout<<"\nEnter Maths marks : "<< endl;
        cin >> m[i].math;

        cout<<"\nEnter Chemistry marks : "<< endl;
        cin >> m[i].chem;
    }

    cout<<"\nStudents mark details : \n"<< endl;

    for(i=0;i<n;i++){
         cout<<"\nMarks of Student"<< i+1 <<" : "<< endl;
         cout<<"\nPhysics marks: "<< m[i].phy<< endl;
         cout<<"\nMaths marks: "<< m[i].math<< endl;
         cout<<"\nChemistry marks: "<< m[i].chem<< endl;
         cout<<"\n"<< endl;
    }
    return 0;
}

These two approaches are more or less equivalent; however, you appear to tried to do both at the same time, and got it mixed up as a result.

As a side note: why are you #includeing the conio.h header, a non-standard header which you aren't actually using, and which won't even work under most C++ compilers? I know that there are ports of <conio.h> to GCC and Visual C++, but it isn't an actual standard library - it can't be, since unbuffered console I/O is system-dependent, so a library designed for MS-DOS won't necessarily be translatable to, say, Linux, without serious modifications. It was originally part of the Turbo C++ MS-DOS library, and no equivalent to it exists for most operating systems - even for Windows it is a bad shoehorning of an older style of prgramming. I would avoid conio.h in the future, even - or perhaps especially - if your professor is teaching you to use it.

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.