程序在 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;
}
不确定我应该如何制定标题!
我正在尝试编写一个程序,要求用户提供学生的姓名。然后将该名称存储在一个结构中,并询问课程名称和该课程的成绩。它会一直这样做,直到课程名称为 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;
}