Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading