0

Here is the code as well as the output, it only takes the student name once.

#include <iostream>
#include<string>

using namespace std;
struct record
{
    string name;
    int marks[6];
    int total=0;
};
int index = 5;
record recorder[5];
void adder(record a[], int index)
{

    for (int i = 0; i < index; i++)
    {
        
        cout << "Enter the Student Name:\n";
            cin.clear();
        getline(cin, a[i].name);
        for (int j = 0; i < 6; i++)
        {
            cout << "Enter the " << j << " Subject Marks";
            cin >> a[i].marks[j];
        }

        for (int j = 0; i < 6; i++)
        {

            a[i].total += a[i].marks[j];
        }
    }
}
int comparer(record a[],int index){
    int i=0;
    int totalfn =0;
    while (i<index)
    {
        if (a[i].total>totalfn)
        {
            totalfn= a[i].total;
        }
        index++;
    }
    return totalfn;
}


void toppercalc(record a[] , int index ,int topper){
    for (int i = 0; i < index; i++)
    {
        if (topper == a[i].total)
        {
            cout<<a[i].name;
            return;
        }
        
    }
    
}
int main()
{
adder(recorder,index);
int topper = comparer(recorder,index);
toppercalc(recorder ,index , topper);
    return 0;
}

Screenshot

Enter the Student Name:
mary
Enter the 0 Subject Marks
33
Enter the 0 Subject Marks
4
Enter the 0 Subject Marks
3
Enter the 0 Subject Marks
2
Enter the 0 Subject Marks
3
Enter the 0 Subject Marks
4

Enter the Student Name:
alina
Enter the 1 Subject Marks
33
Enter the 1 Subject Marks
4
Enter the 1 Subject Marks
3
Enter the 1 Subject Marks
2
Enter the 1 Subject Marks
3
Enter the 1 Subject Marks
4
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • no it just clears the m from mary – Umar Ahmed Jan 05 '23 at 07:45
  • @DavidRanieri Why? According to `int marks[6];`, it should be correct. Though, with these [bad magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants), it's easy to confuse things... – Scheff's Cat Jan 05 '23 at 07:46
  • @Scheff'sCat right, sorry – David Ranieri Jan 05 '23 at 07:47
  • Your two inner loops init `j` but increment `i`: `for (int j = 0; i < 6; i++)` – Frodyne Jan 05 '23 at 07:48
  • Thanks to all the contributors , the query stands closed. silly it was ;) – Umar Ahmed Jan 05 '23 at 07:50
  • 1
    The time to use `ignore` is not before `getline` when you can't generally know what input is waiting, it's after you do `>>` when you do know that there is input to be skipped. – john Jan 05 '23 at 07:52
  • Sorry, I confused `std::istream::clear()` with `std::istream::ignore()`... ...and I somehow feel like you intended to use the latter... – Scheff's Cat Jan 05 '23 at 07:52

0 Answers0