moon moon - 1 month ago 8
C++ Question

How to display one result in for loop

I've this program that is dealing with registration on seminar.

I'v managed to do up the struct class, and also able to list out the seminar.
Currently, I have an issue with the registration.
The problem goes like this:
The user would input the seminar that he would like to register for.
Once the user registered the seminar, it will result that the seminar is either registered successfully (if there's slot), or unsuccessfully (if all slot are taken).

The code that I have is able to print out the result, however it display all the result that it loop through. Is there a way for me to print the result just for that particular seminar that the user had input?

Below is my code.
I understand that I did a for loop.
But I'm not too sure how I can display a single result, without looping it.

Struct:

struct Seminar
{
string Title;
int Capacity;
};
Seminar theSeminar[4];


function:

void registerSem(string sem){

for (int i = 0; i < 4; i++)
{
if( theSeminar[i].Title == sem){
if (theSeminar[i].Capacity > 0)
{
theSeminar[i].Capacity = theSeminar[i].Capacity - 1;
cout << "Successful registered!" << endl;

}
else{
cout << "Unsuccessful registration" << endl;
}
}
else{
cout << "Not Found" << endl;
}
}
}

Answer Source

You need to move the printing out of the loop. You can do that by adding a found variable that you can check afterwards:

void registerSem(string sem){
    bool found = false;
    for (int i = 0; i < 4; i++)
    {
       if( theSeminar[i].Title == sem){
            found = true;
            if (theSeminar[i].Capacity > 0)
            {
                theSeminar[i].Capacity = theSeminar[i].Capacity - 1;
                cout << "Successful registered!" << endl;    
            }
            else{
                cout << "Unsuccessful registration" << endl;
            }
            break;
       }
    }
    if (!found)
    {
        cout << "Not Found" << endl;
    }
}

You could also streamline the code by returning from the whole function once a seminar is found:

void registerSem(string sem) {
    for (int i = 0; i < 4; i++) {
        if (theSeminar[i].Title == sem) {
            if (theSeminar[i].Capacity > 0) {
                theSeminar[i].Capacity -= 1;
                cout << "Successfully registered!\n";
            }
            else {
                cout << "Unsuccessful registration\n";
            }
            return;
        }
    }
    cout << "Not found\n";
}

Alternatively, you could separate the concerns of finding the right seminar and doing something with it:

void registerSem(string sem) {
    int found_at = -1;
    for (int i = 0; i < 4; i++) {
        if (theSeminar[i].Title == sem) {
            found_at = i;
            break;
        }
    }

    if (found_at == -1) {
        cout << "Not found\n";
        return;
    }

    if (theSeminar[found_at].Capacity <= 0) {
        cout << "Unsuccessful registration\n";
        return;
    }

    theSeminar[found_at].Capacity -= 1;
    cout << "Successfully registered!\n";
}