Program breaks before it is supposed to in C++

Not sure how I was supposed to formulate title!

I’m trying to write a program that asks the user for the name of a student. Then it stores that name in a struct and asks for a course name and the grade for that course. It does this until the course name is stop. Then it asks for another student until the name given is stop.

Here is my code:

#include <iostream>

using namespace std;

struct student{

public:
    string name;
    string course;
    int grade;

}student_t;

int main() {


    while (student_t.name != "stop"){
        cout << "Please type the name of a student: " << endl;
        getline(cin, student_t.name);

        if (student_t.name.compare("stop")){
            break;
        }
        else{
            if (student_t.course.compare("stop")){
                break;
            }
            else {
                cout << "Please type a course name: " << endl;
                getline(cin, student_t.course);
                cout << "Please type the grade: " << endl;
                cin >> student_t.grade;
            }
        }
    }

    cout << student_t.name << " - " << student_t.course << " - " << student_t.grade << endl;


    return 0;
}

The problem is, it exits no matter what name I type.

Any ideas?

>Solution :

It should be:

if (student_t.name.compare("stop") == 0){
    break;
}

Leave a Reply