Home > front end >  trying to update variables in a vector object, can anyone explain how to make it work?
trying to update variables in a vector object, can anyone explain how to make it work?

Time:02-03

I am new to programming. I am trying to make a program that allows me to input student details including their grades, and information. I have the students as objects stored in a vector.

I want to make it update the student grades, but for some reason what I have isn't working. When trying to edit the grades, it loops through and displays all of the students, even when I only select one student, then it asks me to edit them all and changes the values of different students selected. I'm trying to make it so when I select to edit the student grades, it will ask me which student to edit, and display a list of students, and I then select which one and it allows me to edit their grades.

I hope what I said makes sense. Can anyone help me?

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>

using namespace std;
// UPDATESTUDENTGRADES DISPLAYS ALL STUDENTS AND DOES NOT UPDATE PROPERLY
// fix the updategrade to display only the student selected
// Show a list of all students in grade order, highest to lowest

class Student
{  
private:
    string name;
    string email;
    string number;
    int grade1, grade2, grade3, grade4, totalGrade;
    int percentage1, percentage2, percentage3, percentage4, totalpercentage;

public:
    static void addstudent(vector<Student>& students)
    {
        cout << "\nEnter the number of students: ";
        int count{ };
        cin >> count;

        cout << '\n';

        for (int i{ }; i < count; i  )
        {
            cout << "Enter the details of student: " << i   1 << ":\n";
            students.resize(students.size()   1);
            students.back().inputStudentDetails();
            cout << '\n';
        }

        cout << '\n';
    }

    void inputStudentDetails()
    {
        cout << "Enter Student name: ";
        cin >> name;
        cout << "Enter student number: ";
        cin >> number;
        cout << "Enter student email: ";
        cin >> email;
        cout << "Enter grade for test1: ";
        cin >> grade1;
        cout << "Enter grade for test2: ";
        cin >> grade2;
        cout << "Enter grade for test3: ";
        cin >> grade3;
        cout << "Enter grade for test4: ";
        cin >> grade4;

        percentage1 = grade1 / 100 * 100;
        percentage2 = grade2 / 100 * 100;
        percentage3 = grade3 / 100 * 100;
        percentage4 = grade4 / 100 * 100;
        totalpercentage = grade1 * 10 / 100   grade2 * 20 / 100   grade3 * 30 / 100   grade4 * 40 / 100;
    }

    static void printallstudentsdetails(const vector<Student>& students)
    {
        int i{ };

        for (const Student& stud : students)
        {
            cout << "\nDetails of student #" <<   i << ":\n";
            stud.printstudentdetails();
            cout << setfill('-') << std::setw(28) << '\n';
        }
    }

    void printstudentdetails() const
    {
        cout << "Student Name: " << name << "\nStudent Number: " << number << "\nStudent email: " << email << "\ngrade1: " << grade1 << "\ngrade2: " << grade2 << "\ngrade3: " << grade3 << "\ngrade4: " << grade4 << "\ntotal " << totalpercentage << " out of 100\n";
    }


    static void displayactivestudents(const vector<Student>& students) {
        int i{ };

        for (const Student& stud : students)
        {
            cout << "\n student #" <<   i << ":\n";
            stud.printstudentdetails2();
            cout << setfill('-') << std::setw(28) << '\n';
        }
    }

    void printstudentdetails2() const
    {
        cout << "Student Name: " << name << "\ngrade1: " << grade1 << "\ngrade2: " << grade2 << "\ngrade3: " << grade3 << "\ngrade4: " << grade4 << "\ntotal " << totalpercentage << " out of 100\n";
    }

    static void updatestudentgrades(vector<Student>& students)  //not working properly need it to only print and edit whatever student is selected
    {                                                         
        displayactivestudents(students);
        cout << "select which student in array";
        int count{ };
        cin >> count;

        cout << '\n';

        for (int i{ }; i < count;   i)
        {
            cout << "Enter the new grades of the students: " << i   1 << ":\n";
            students.resize(students.size());
            students.back().inputstudentgrades();
            cout << '\n';
        }

        cout << '\n';
    }

    void inputstudentgrades() {
        cout << "Enter grade for test1: ";
        cin >> grade1;
        cout << "Enter grade for test2: ";
        cin >> grade2;
        cout << "Enter grade for test3: ";
        cin >> grade3;
        cout << "Enter grade for test4: ";
        cin >> grade4;
        totalpercentage = grade1 * 10 / 100   grade2 * 20 / 100   grade3 * 30 / 100   grade4 * 40 / 100;
        cout << "\ntotal " << totalpercentage << " out of 100\n";
    }

    static void gradepredictor() {
        int predictedgrade1, predictedgrade2, predictedgrade3, predictedgrade4, predictedtotalpercentage, predicted70total;

        cout << "Enter grade for test1: ";
        cin >> predictedgrade1;
        cout << "Enter grade for test2: ";
        cin >> predictedgrade2;
        cout << "Enter grade for test3: ";
        cin >> predictedgrade3;
        cout << "Enter grade for test4: ";
        cin >> predictedgrade4;

        predictedtotalpercentage = predictedgrade1 * 10 / 100   predictedgrade2 * 20 / 100   predictedgrade3 * 30 / 100   predictedgrade4 * 40 / 100;

        cout << "\npredicted total " << predictedtotalpercentage << "%" << " out of 100\n";

        if (predictedtotalpercentage < 70 && predictedgrade4 == 0) {
            predicted70total = 70 - predictedtotalpercentage;
            cout << "\nyou will need " << predicted70total << "%" << " to get 70%";
        }
    }

    static void save(vector<Student>& students) {
        ofstream outPutFile;
        outPutFile.open("studentmarks.txt");
        for (int i = 0; i < students.size(); i  )
        {
            outPutFile << students[i].getName() << endl;
            outPutFile << students[i].getNum() << endl;
            outPutFile << students[i].getEmail() << endl;
            outPutFile << students[i].getGrade1() << endl;
            outPutFile << students[i].getGrade2() << endl;
            outPutFile << students[i].getGrade3() << endl;
            outPutFile << students[i].getGrade4() << endl;
            outPutFile << students[i].getTotal() << endl;

            if (i != (students.size() - 1))
            {
                outPutFile << endl;
            }
        }
    }

    static void load() {
        string filestring;
        ifstream file_("studentmarks.txt");
        if (file_.is_open())
        {
            char mychar;
            while (file_) {
                mychar = file_.get();
                cout << mychar;
            }
        }
    }

    string getName() {
        return name;
    }

    string getEmail() {
        return email;
    }

    string getNum() {
        return number;
    }

    int getGrade1() {
        return grade1;
    }

    int getGrade2() {
        return grade2;
    }

    int getGrade3() {
        return grade3;
    }

    int getGrade4() {
        return grade4;
    }

    int getTotal() {
        return totalpercentage;
    }
};

int main()
{
    vector<Student> students;

    while (true)
    {
        cout << "\t\tMENU\n" << setfill('_') << setw(36) << "\n\n";
        cout << "1: Enter student details\n";
        cout << "2: Display all students' details and grades with current total mark\n";
        cout << "3: Update existing student grades\n";
        cout << "4: Enter grades for predicted total\n";
        cout << "5: save\n";
        cout << "6:Load\n";
        cout << "8: Exit the Application\n";
        char select{ };
        cin >> select;

        switch (select)
        {
        case '1': Student::addstudent(students);
            break;
        case '2': Student::printallstudentsdetails(students);
            break;
        case '3': Student::updatestudentgrades(students);
            break;
        case '4': Student::gradepredictor();
            break;
        case '5': Student::save(students);
            break;
        case '6': Student::load();
            break;
        case '8':
            return 0;
        default: cout << "Invalid Input\n";
        }
    }
}

CodePudding user response:

In updatestudentgrades() you are prompting for the index of a given student, but then you don't actually use that index correctly. Youare looping through every student up to that index, prompting for edits to each one. Get rid of the loop entirely, you don't need it. You can access just the specified student directly, eg:

static void updatestudentgrades(vector<Student>& students)
{                                                         
    displayactivestudents(students);
    cout << "select which student in array:";

    int index{ };
    cin >> index;

    cout << '\n';

    if ((index >= 0) && (index < students.size()))
    {
        cout << "Enter the new grades of student " << index   1 << ":\n";
        students[index].inputstudentgrades();
        cout << '\n';
    }
    else
        cout << "Invalid index\n";
}
  •  Tags:  
  • Related