簡體   English   中英

程序在 C++ 中應該在它之前中斷

[英]Program breaks before it is supposed to in C++

不知道我應該如何制定標題!

我正在嘗試編寫一個程序,要求用戶輸入學生的姓名。 然后它將該名稱存儲在一個結構體中,並要求提供該課程的課程名稱和成績。 它這樣做直到課程名稱是stop 然后它要求另一個學生,直到給出的名字是stop

這是我的代碼:

#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;
}

問題是,無論我輸入什么名稱,它都會退出。

有任何想法嗎?

它應該是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM