Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

can someone tell me what is my mistake on my code? ( i've bold the part)

+5 votes
asked Apr 21 by Afiqers (210 points)

there's 3 choices of college. but when i entered the college number, the output are same which is 5. can you guys help me?

#include <iostream>
using namespace std;
const int MAX_NAME_LENGTH = 50;
const int MAX_STUDENTS = 1000;

struct Student {
    char name[MAX_NAME_LENGTH];
    int id;
    string fee;
    int college;
};

void printStudents(const Student students[], int numStudents) {
    for (int i = 0; i < numStudents; i++) {
        cout << "Name: " << students[i].name << ", ID: " << students[i].id << ", College number: " << students[i].college << ", Fee: " << students[i].fee << endl;
    }
}

int main() {
    Student students[MAX_STUDENTS];
    int numStudents = 0;

    while (true) {
        cout << "1. Add student" << endl;
        cout << "2. Delete student" << endl;
        cout << "3. Print all students" << endl;
        cout << "4. Quit" << endl;
        cout << "Enter choice: ";

        int choice;
        cin >> choice;

        if (choice == 1) {
            if (numStudents < MAX_STUDENTS) {
                cout << "Enter student name: ";
                cin >> students[numStudents].name;

                cout << "Enter student ID: ";
                cin >> students[numStudents].id;

                cout << "Enter student college (5/11/12): ";
                cin >> students[numStudents].college;
                if (students[numStudents].college =  5){
                    students[numStudents].fee = "$80.00";
                }
                else if (students[numStudents].college =  11){
                    students[numStudents].fee = "$90.00";
                }
                else if(students[numStudents].college =  12){
                    students[numStudents].fee = "$100.00";
                }
                else {
                    cout<<"Wrong college"<<endl;
                }

                numStudents++;
            }
            else {
                cout << "Maximum number of students reached." << endl;
            }
        }
        else if (choice == 2) {
            cout << "Enter student ID to delete: ";
            int id;
            cin >> id;

            int index = -1;
            for (int i = 0; i < numStudents; i++) {
                if (students[i].id == id) {
                    index = i;
                    break;
                }
            }

            if (index != -1) {
                for (int i = index; i < numStudents - 1; i++) {
                    // Shift all elements after the deleted element one position to the left
                    for (int j = 0; j < MAX_NAME_LENGTH && students[i+1].name[j] != '\0'; j++) {
                        students[i].name[j] = students[i+1].name[j];
                    }
                    students[i].id = students[i+1].id;
                    students[i].college = students[i+1].college;
                }
                numStudents--;
            }
            else {
                cout << "Student not found." << endl;
            }
        }
        else if (choice == 3) {
            printStudents(students, numStudents);
        }
        else if (choice == 4) {
            break;
        }
    }

    return 0;
}

1 Answer

+3 votes
answered Apr 23 by Peter Minarik (77,930 points)
selected Apr 25 by Afiqers
 
Best answer
cout << "Enter student college (5/11/12): ";
cin >> students[numStudents].college;
if (students[numStudents].college == 5){
    students[numStudents].fee = "$80.00";
}
else if (students[numStudents].college == 11){
    students[numStudents].fee = "$90.00";
}
else if(students[numStudents].college == 12){
    students[numStudents].fee = "$100.00";
}
else {
    cout<<"Wrong college"<<endl;
}

The equal sign (=) is the assignment operator, the left hand side takes the value of the right hand side.

The double equal sign (==) is the equality comparison operator.

You assigned a value in your code instead of comparing values. With the fix proposed in yellow above, your code should work better now.

commented Apr 25 by Afiqers (210 points)
woahhh thankyou so much!! i dont remember that
commented Apr 26 by Peter Minarik (77,930 points)
No worries. Good luck!
commented May 5 by Alastor Insane (120 points)
Thanks! That was need me!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...