在接收进一步输入之前继续循环迭代

Loop iteration continues before receiving further input

提问人:VX00 提问时间:9/29/2023 最后编辑:Marwan_TamerVX00 更新时间:10/1/2023 访问量:100

问:

我对使用C++很陌生,所以我遇到了一些问题——>

我敢肯定,我可以用另一种方式来做到这一点,只需使用一个函数来检查老师是否要添加另一个学生。但是,我相信这种方式应该有效。我遇到了一个错误。循环继续迭代,打印“您想添加另一个学生:y/n?我认为这可能是由于剩余的输入通过 cin >> instructorChoice 传递的。

但如果是这样的话,它不应该停在其他 cin(s) 上吗?因为它会在某一时刻再次遇到他们。

我还有另一个问题,我不确定如何打印对象属性(我相信这就是你引用它们的方式?),例如 person.name()。虽然我敢肯定我以后会弄清楚的,但没什么大问题。 -->头文件中的打印功能。

我有一个主文件和一个班级的头文件。我不确定这是否是最好的方法。我知道这是一个小程序,但我想尝试在编程时养成更好的文件管理习惯。

我将不胜感激。提前致谢。

/*
    Simple "Class" Project 1:

    This program allows a teacher to add a student to a list of student,
    once she/he is done. The program prints all the students in the list
    along with their grades. I'm testing the use of header files by defining
    my class and its methods there.

    - Me
*/

#include <vector>
#include  "student.h" //include files for student class
                      //includes <iostream>, <string>, namespace


int main() {
    //var declarations
    string studentName;
    int studentGrade;
    char instructorChoice;
    int i;

    //delcare student vector
    vector<Student> studentList;

    //take the initial input
    cout << "Would you like to add a new student: y/n?" << endl;
    cin >> instructorChoice;

    //run until no more students need to be addded
    while (instructorChoice == 'y') { 
        
        Student student; //create new student info

        //prompt for user info
        cout << "Student Name: ";
        cin >> studentName;

        cout << "Student Grade: ";
        cin >> studentGrade;

        student.SetName(studentName); //setName
        student.SetGrade(studentGrade); //setGrade
        studentList.push_back(student); //append to list of students

        //notify teacher!
        cout << student.GetName() << " has successfully been added!" << endl;

        //ask if they would like to add another student
        cout << "Would you like to add another student: y/n?" << endl;
        cin >> instructorChoice;

    }

    //print all 
    for (i = 0; i < studentList.size(); ++i) { //iterate through object vector
        studentList[i].PrintStudents();
    }
    
    return 0;
}
/*
    Student Class Declaration
*/
#include <iostream>
#include <string>
using namespace std;

class Student {
    public:
        //set
        void SetName(string studentName);
        void SetGrade(char studentGrade);

        //get
        string GetName();
        int GetGrade();
        
        //print
        void PrintStudents();

    private:
        string name;
        char grade;

};


// public function definitions

//set
void Student::SetName(string studentName) { //Set new student name
    name = studentName;
}

void Student::SetGrade(char studentGrade) { //Set new student grade
    grade = studentGrade;
}


//get
string Student::GetName() { //return the student name
    return name;
}

int Student::GetGrade() { //return the student grade
    return grade;
}

//print
void Student::PrintStudents() { //Print student name and grade
    cout << "Student: " << name << " Grade: " << grade << endl;
}

我尝试使用 cin.ignore();

和cout<<齐平;

我能找到的几乎任何东西来清除任何剩余的缓冲区/输入。没用。

不确定问题到底是什么。

C++ 对象

评论

2赞 ikegami 9/29/2023
请不要标记您不使用的语言。(已修复)
3赞 273K 9/29/2023
你输入什么?我怀疑您尝试输入“John Doug”,并且进一步陷入失败状态。cin >> studentGradecin
1赞 Tim Roberts 9/29/2023
你的代码对我来说很好用。你到底在用什么输入?您的代码要求您输入一个整数作为成绩。您是否正在尝试输入字母等级?那会失败。
0赞 273K 9/29/2023
你的程序是一个坏代码的大演示。首先,您不会像 .if (cin >> studentGrade)
1赞 Peter 9/29/2023
一个问题是,它对用户输入的数据格式做出了非常具体的假设,如果用户不遵守,事情就会变得糟糕。像在代码时输入这样简单的事情会把事情搞砸( 获取值 ,in 会搞砸 的读取)。这是生产代码中通常不用于读取用户输入的原因之一。通常无法确保输入是预期的格式,甚至在不是时无法合理地恢复。cin >> ...John Jones 47cin >> studentName; cin >> studentGradestudentName"John"JJonesstudentGrade>>

答:

1赞 SA Moosavi 9/29/2023 #1

我读了你的代码,这对开始有好处。

无限循环的原因是你的类中的类型是 but in 是 。gradecharmain functionint

干净代码中的一些错误:

  • 写入所有文件中的 include 不要使用嵌套。

  • 的 write 函数。因为是重复的。cout << "Would you like to add a new student: y/n?" << endl; cin >> instructorChoice;

你的打印还不错,但你可以写,可以使用运算符重载(string get_student_data()operator<<)

评论

0赞 VX00 9/30/2023
是的,这是一个数据类型错误。我只需要将等级更改为 char。 谢谢大家!:D