在 C++ 中询问条件信息
Asking for conditional information in C++
不确定我应该如何制定标题。
我正在编写一个程序,要求输入学生姓名,如果名称不是“stop”,那么它会要求输入课程名称。如果课程名称不是“stop”,则要求成绩,然后returns要求其他课程,直到课程名称为“stop”。然后它要求另一个学生并再次做整个事情,直到学生名字是“停止”。然后打印所有学生的课程和成绩。
这是我的代码:
#include <iostream>
#include <vector>
using namespace std;
struct student{
public:
string name;
string course;
int grade;
}student_t;
void input(){
cout << "Please type the name of a student: " << endl;
getline(cin, student_t.name[i]);
if (student_t.name.compare("stop")){
break;
}
else {
cout << "Please type a course name: " << endl;
getline(cin, student_t.course);
if (student_t.course.compare("stop")) {
break;
}
else {
cout << "Please type the grade: " << endl;
cin >> student_t.grade;
}
}
}
int main() {
int i;
vector<student> input(i);
for (i = 0; i < 20; ++i){
student[i].input();
}
cout << student_t.name << " - " << student_t.course << " - " << student_t.grade << endl;
return 0;
}
根本不起作用..
我是 C++ 的新手,所以我真的不知道为什么。
有什么想法吗?
我可以给一两个建议。
- 与其检查每一步是否错误(即等于“停止”),不如关注正确的部分(即接下来要阅读的内容)。从心理的角度来看,我发现思考什么必须是正确的才能让程序按预期进行要容易得多,而不是“这里会出什么问题?”步步。你的 input() 然后可以是(伪代码):
if (student_t.name is not "stop")
{
Read_course_name();
if (student_t.course is not "stop")
{
Read_grades();
// Done
return(StudentInfo);
}
}
// Ending up here means "stop" was typed so deal with that
// in a meaningful way
...
- 看看 https://en.wikipedia.org/wiki/Separation_of_concerns 然后问问自己,-input() 函数真的是一个输入吗?或者是否有多个问题可以在一个单独的函数中分解?
干杯
/N
不确定我应该如何制定标题。
我正在编写一个程序,要求输入学生姓名,如果名称不是“stop”,那么它会要求输入课程名称。如果课程名称不是“stop”,则要求成绩,然后returns要求其他课程,直到课程名称为“stop”。然后它要求另一个学生并再次做整个事情,直到学生名字是“停止”。然后打印所有学生的课程和成绩。
这是我的代码:
#include <iostream>
#include <vector>
using namespace std;
struct student{
public:
string name;
string course;
int grade;
}student_t;
void input(){
cout << "Please type the name of a student: " << endl;
getline(cin, student_t.name[i]);
if (student_t.name.compare("stop")){
break;
}
else {
cout << "Please type a course name: " << endl;
getline(cin, student_t.course);
if (student_t.course.compare("stop")) {
break;
}
else {
cout << "Please type the grade: " << endl;
cin >> student_t.grade;
}
}
}
int main() {
int i;
vector<student> input(i);
for (i = 0; i < 20; ++i){
student[i].input();
}
cout << student_t.name << " - " << student_t.course << " - " << student_t.grade << endl;
return 0;
}
根本不起作用..
我是 C++ 的新手,所以我真的不知道为什么。
有什么想法吗?
我可以给一两个建议。
- 与其检查每一步是否错误(即等于“停止”),不如关注正确的部分(即接下来要阅读的内容)。从心理的角度来看,我发现思考什么必须是正确的才能让程序按预期进行要容易得多,而不是“这里会出什么问题?”步步。你的 input() 然后可以是(伪代码):
if (student_t.name is not "stop")
{
Read_course_name();
if (student_t.course is not "stop")
{
Read_grades();
// Done
return(StudentInfo);
}
}
// Ending up here means "stop" was typed so deal with that
// in a meaningful way
...
- 看看 https://en.wikipedia.org/wiki/Separation_of_concerns 然后问问自己,-input() 函数真的是一个输入吗?或者是否有多个问题可以在一个单独的函数中分解?
干杯 /N