samedi 27 novembre 2021

Program won't perform this if-branch [closed]

The objective of this program is to be able to change any of the following values using user input: department ID, course number, section, credits, instructor, days, time, max class enrollment, students and student IDs of said students enrolled, and registration status. You should be able to change any of those values and be able to print it all out in the end. Here is the part of my code with the issue:

cout << "Please input \"c\" if you want to make any changes." << endl;
    cin >> makeChanges;

    if (makeChanges == 'c') {
        cout << "Please input which change you would like to make: " << endl;
        cout << "Department ID" << endl;
        cout << "Course Number" << endl;
        cout << "Section" << endl;
        cout << "Credits" << endl;
        cout << "Instructor" << endl;
        cout << "Day(s)" << endl;
        cout << "Time" << endl;
        cout << "Campus" << endl;
        cout << "Building" << endl;
        cout << "Room Number" << endl;
        cout << "Max Class Enrollment" << endl;
        cout << "Students" << endl;
        cout << "Student IDs" << endl;
        cout << "Registration Status" << endl;
    }
    else {
        courseInfo.printInfo();
    }

    getline(cin, changeType);

    if (changeType == "Department ID") {
        cout << "Please input the department ID." << endl;
        getline(cin, department);
        courseInfo.setCourseID(department);
    }

    else if (changeType == "Course Number") {
        cout << "Please input the course number." << endl;
        cin >> classNum;
        courseInfo.setCourseNum(classNum);
    }

    else if (changeType == "Section") {
        cout << "Please input the section number." << endl;
        cin >> section;
        courseInfo.setSectNum(section);
    }

    else if (changeType == "Credits") {
        cout << "Please input the number of credits." << endl;
        cin >> credits;
        courseInfo.setNumCreds(credits);
    }

    else if (changeType == "Instructor") {
        cout << "Please input the instructor." << endl;
        getline(cin, professor);
        courseInfo.setInstructName(professor);
    }

    else if (changeType == "Day(s)") {
        cout << "Please input the day(s): M, T, W, Th, or F." << endl;
        getline(cin, days);
        courseInfo.setDays(days);
    }

    else if (changeType == "Time") {
        cout << "Please input the hour of the start time." << endl;
        cin >> hour1;
        courseInfo.setCourseHour1(hour1);

        cout << "Next please input the minutes of the start time (If it is 0, then put in 00)." << endl;
        cin >> min1;
        courseInfo.setCourseMinute1(min1);

        cout << "Please input the hour of the end time." << endl;
        cin >> hour2;
        courseInfo.setCourseHour2(hour2);

        cout << "Next please input the minutes of the end time (If it is 0, then put 00)." << endl;
        cin >> min2;
        courseInfo.setCourseMinute2(min2);
    }
    
    else if (changeType == "Campus") {
        cout << "Please input the campus your course is held on." << endl;
        getline(cin, campus);
        courseInfo.setCampus(campus);
    }

    else if (changeType == "Building") {
        cout << "Please input the building your course is held in." << endl;
        cin >> building;
        courseInfo.setClassroomBuilding(building);
    }

    else if (changeType == "Room Number") {
        cout << "Please input the classroom number." << endl;
        cin >> roomNum;
        courseInfo.setClassroomNum(roomNum);
    }

    else if (changeType == "Max Class Enrollment") {   
        cout << "Please input maximum students in the course." << endl;
        cin >> maxClass;
        courseInfo.setMaxEnroll(maxClass);
    }

    else if (changeType == "Students") {
        cout << "Please input the number of students enrolled." << endl;
        cin >> numStuds;
        courseInfo.setNumStuds(numStuds);
    }

    else if (changeType == "Student IDs") {
        cout << "Please input the IDs of each student enrolled." << endl;
        courseInfo.inputStudIDs(numStuds);
    }

    else if (changeType == "Registration Status") {
        cout << "Please input the registration status: \"o\" for open, \"c\" for close, or \"x\" for cancelled." << endl;
        cin >> status;
        courseInfo.setCourseStat(status);
    }
    courseInfo.printInfo();

When testing this by attempting to change any value that is more than one word (like Department ID), the program just outputs all of the initial values without following through with the if-branch and actually changing the value. However, this is not an issue for one-word values (like Section). What can I do to fix this error?

Edit: I already included directives and functions and their respective definitions, but they are too long to put here. It still compiles, but it just doesn't follow through with the if-branch.

Edit: Here are the functions and function definitions:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class UMDearborn {
    public:
        UMDearborn();
        void setCourseNum(int courseNum);
        
        void setCourseID(string courseID);
        
        void setSectNum(int sectNum);
        
        void setNumCreds(int numCreds);
        
        void setDays(string courseDays);
        
        void setCourseHour1(int hour1);
        
        void setCourseMinute1(int min1);

        void setCourseHour2(int hour2);
        
        void setCourseMinute2(int min2);

        void setClassroomBuilding(string buildingID);
        
        void setClassroomNum(int roomNum);
        
        void setMaxEnroll(int maxNum);
        
        void setCampus(string campusName);
        
        void setInstructName(string profName);
        
        void setNumStuds(int numStuds);
        
        void inputStudIDs(int numStuds);
        void setStudIDs(int studID);
        int getStudIDs() const;
        void printStudIDs();

        void setCourseStat(char courseStat);

        void printInfo();
    private:
        int classNum;
        string department;
        int section;
        int credits;
        string days;
        int courseHr1;
        string courseMin1;
        int courseHr2;
        string courseMin2;
        string building;
        int locationNum;
        int maximum;
        string campus;
        string professor;
        int students;
        int id;
        char status;
        vector<UMDearborn> idNums;
};

UMDearborn::UMDearborn() {
    classNum = 150;
    department = "CIS";
    section = 4;
    credits = 4;
    days = "M, W";
    courseHr1 = 2;
    courseMin1 = "00";
    courseHr2 = 3;
    courseMin2 = "15";
    building = "SSB";
    locationNum = 2195;
    maximum = 60;
    campus = "Dearborn";
    professor = "Unknown";
    students = -1;
    id = 10101010;
    status = 'n';
}

void UMDearborn::setCourseNum(int courseNum) {
    classNum = courseNum;
}

void UMDearborn::setCourseID(string courseID) {
    department = courseID;
}

void UMDearborn::setSectNum(int sectNum) {
    section = sectNum;
}

void UMDearborn::setNumCreds(int numCreds) {
    credits = numCreds;
}

void UMDearborn::setDays(string courseDays) {
    days = courseDays;
}

void UMDearborn::setCourseHour1(int hour1) {
    courseHr1 = hour1;
}

void UMDearborn::setCourseMinute1(int min1) {
    courseMin1 = min1;
}

void UMDearborn::setCourseHour2(int hour2) {
    courseHr2 = hour2;
}

void UMDearborn::setCourseMinute2(int min2) {
    courseMin2 = min2;
}

void UMDearborn::setClassroomBuilding(string buildingID) {
    building = buildingID;
}

void UMDearborn::setClassroomNum(int roomNum) {
    locationNum = roomNum;
}

void UMDearborn::setMaxEnroll(int maxNum) {
    maximum = maxNum;
}

void UMDearborn::setCampus(string campusName) {
    campus = campusName;
}

void UMDearborn::setInstructName(string profName) {
    professor = profName;
}

void UMDearborn::setNumStuds(int numStuds) {
    students = numStuds;
}

void UMDearborn::setStudIDs(int studID) {
    id = studID;
}

void UMDearborn::setCourseStat(char courseStat) {
    status = courseStat;
}

void UMDearborn::inputStudIDs(int numStuds) {
    UMDearborn currID;
    int i;
    int id;

    for (i = 0; i < idNums.size(); ++i) {
        cout << "ID: ";
        cin >> id;
        currID.setStudIDs(id);
        idNums.push_back(currID);
    }

}

void UMDearborn::printStudIDs() {
    UMDearborn currID;
    
    for (int i = 0; i < (idNums.size() - 1); ++i) {
        currID = idNums.at(i);
        cout << currID.getStudIDs() << ", ";
    }

    currID = idNums.back();
    cout << currID.getStudIDs();
}

void UMDearborn::printInfo() {
    UMDearborn courseInfo;
    cout << "Department ID: " << department << endl;
    cout << "Course Number: " << classNum << endl;
    cout << "Section: " << section << endl;
    cout << "Credits: " << credits << endl;
    cout << "Instructor: " << professor << endl;
    cout << "Day(s): " << days << endl;
    cout << "Time: " << courseHr1 << ':' << courseMin1 << " - " << courseHr2 << ':' << courseMin2 << endl;
    cout << "Campus: " << campus << endl;
    cout << "Building ID: " << building << endl;
    cout << "Room Number: " << locationNum << endl;
    cout << "Max Class Enrollment: " << maximum << endl;
    cout << "Students: " << students << endl;
    cout << "Student IDs: " << courseInfo.printStudIDs() << endl; // Get back to later
    cout << "Status: " << status << endl;
}

Aucun commentaire:

Enregistrer un commentaire