1

So, I am beginner. I have this code and a few problems. For better understanding, you will need this code:

struct student
{
    double marks;
    char name[50];
}stud[100],t;

int main()
{
    int i,j,n;
    cout<<"Enter the number of students: ";
    cin>>n;
    cout<<"Enter student info as name , marks\n";
    for(i=0;i<n;i++)
    {
        cin>>stud[i].name;
        cin>>stud[i].marks;
    }

The problem is, instead of this part:

struct student
{
    double marks;
    char name[50];
}stud[100],t;

There should be this part:

struct student
{
    double marks[];
    string name[];
}stud[100],t;

But then I don't know how to enter that data into the program because then the cin >> doesn't work. Task says that when the user enters ' ' (ENTER), the program should finish and show the students print in order.

  • 1
    How many marks by student you need? You need another loop inside you `for` – Katsu Apr 18 '20 at 20:11
  • 1
    Are you sure that the structure shoud be like that? Other thing, (assuming this is a school project) could you use vectors? – Eduardo Pascual Aseff Apr 18 '20 at 20:13
  • you read *one* mark per student, so *double marks;* is ok (and plurial is useless) – bruno Apr 18 '20 at 20:13
  • 1
    Arrays can't be written that way (except of old dirty C-ish tricks), you need to specify length. Also `string` is a string already (while `char` is a single character), no need to make an array of it. – numzero Apr 18 '20 at 20:14
  • There is another problem. The program says that when the user enters ' ' (ENTER), the program should finish and show the students print in order. – za stacka2345 Apr 18 '20 at 20:16
  • Are you allowed to use `std::string`? Are you allowed to use `std::vector`? They are part of the C++ standard library and you should use them if you're able. Not using them is like driving a car but you don't have a seat or a steering wheel and you have to invent them yourself. – JohnFilleau Apr 18 '20 at 20:25

3 Answers3

0

You'll need a second loop. I added constants -- this is good practice so you don't have "magic numbers" in your code. I added a display function to demonstrate that it works!

#include <iostream>

using namespace std;

const int MAX_MARKS = 25;
const int MAX_STR = 50;
const int MAX_STUDENTS = 100;

struct student
{
    double marks[MAX_MARKS];
    char name[MAX_STR];
}stud[MAX_STUDENTS],t;

int main()
{
    int i,j,n;
    bool complete = false;
    cout<<"Enter the number of students: ";
    cin>>n;
    for(i=0; i < n && i < MAX_STUDENTS; ++i)
    {
        complete = false;
        cout<<"Enter student info as name , marks\n";
        cin>>stud[i].name;
        for (j = 0; j < MAX_MARKS; ++j)
        {
            if (!complete)
            {
                cout << "Enter mark #" << j+1 << ": ";
                if (!(cin >> stud[i].marks[j]))
                {
                    complete = true;
                    stud[i].marks[j] = -1.0;
                    cin.clear();
                    cin.ignore(100, '\n');
                }
            }
            else
                stud[i].marks[j] = -1.0; //0.0 is a valid grade so need a different value
        }
    }

    //Added a block for displaying the students
    for (i = 0; i < MAX_STUDENTS && i < n; ++i)
    {
        complete = false;
        cout << "Student #" << i+1 << ": " << stud[i].name << endl;
        for (j = 0; j < MAX_MARKS && !complete; ++j)
        {
            if (stud[i].marks[j] == -1.0)
                complete = true;
            else
                cout << "\tMark #" << j+1 << ": " << stud[i].marks[j] << endl;
        }
    }
}
lmcdo
  • 45
  • 8
  • I edited it to meet your criteria -- hope this works! – lmcdo Apr 18 '20 at 21:04
  • Thanks, this code works. But we did not get along well. Instead of character, we mean empty space. The user can enter as many students as he wants. When a user enters a blank space instead of a name, that is, when he or she presses enter, the list of previously entered students should be printed without searching for information about another student. Also, only 1 grade per student is entered. – za stacka2345 Apr 18 '20 at 21:19
  • I'm confused -- why do you ask for the number of students then? Also why should you have an array of doubles, as you state in the question? – lmcdo Apr 18 '20 at 21:23
  • My mistake, sorry. The number of students is surplus. The goal of the program is to print students ranked from one with the highest to the one with the lowest grade, the grade is entered once, the students' names are entered until the user squeezes ENTER. Hope I clarified. – za stacka2345 Apr 18 '20 at 21:31
  • Ok this is a substantial departure from what you originally stated and and requires sorting. Have you written a sort before? – lmcdo Apr 18 '20 at 21:33
  • Also, have you learned linear linked lists? That would be much easier to use than an array for sorting. There's no way to do the problem you described without sorting. – lmcdo Apr 18 '20 at 21:35
  • I'm a beginner, so this is the first time I encounter this problem. I have not used sort before, although this is required in this program. – za stacka2345 Apr 18 '20 at 21:43
  • I don't think this program is solvable with the tools you've shown in your sample code. Please update the problem text with more details about the assignments and some things you've learned. This seems like a great time to either use strcpy or linear linked lists, and I'd also like to know what sorting algorithm you were taught or shown. – lmcdo Apr 18 '20 at 21:50
0

The marks and name array are dynamic and they are called flexible array members and they are not supported in cpp u can refer to this link Are flexible array members valid in C++? and moreover they are supported in c and u can have atmost one flexible array member and it should be at end https://www.geeksforgeeks.org/flexible-array-members-structure-c/

0

I believe this is close to what you want:

//10 marks by students
int m = 10;

struct student
{
    double marks[m];
    string name;
}stud[100],t;

int main()
{
    int i,j,n;
    cout<<"Enter the number of students: ";
    cin>>n;
    for(i=0;i<n;i++)
    {
        cout<<"Enter student info as name\n";
        cin>>stud[i].name;
        for(int j=0; j<m; ++j)
        {
            cout<<"Enter student marks "<<j+1<<endl; 
            cin>>stud[i].marks[j];
        }
    }
}
Katsu
  • 1,868
  • 4
  • 19
  • 28
  • Thanks but task says that when the user enters ' ' (ENTER), the program should finish and show the students print in order. So the number of marks is unlimited, the user enters the marks until he enters ' '. – za stacka2345 Apr 18 '20 at 20:26