提问人:VX00 提问时间:9/29/2023 最后编辑:Marwan_TamerVX00 更新时间:10/1/2023 访问量:100
在接收进一步输入之前继续循环迭代
Loop iteration continues before receiving further input
问:
我对使用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<<齐平;
我能找到的几乎任何东西来清除任何剩余的缓冲区/输入。没用。
不确定问题到底是什么。
答:
我读了你的代码,这对开始有好处。
无限循环的原因是你的类中的类型是 but in 是 。grade
char
main function
int
干净代码中的一些错误:
写入所有文件中的 include 不要使用嵌套。
的 write 函数。因为是重复的。
cout << "Would you like to add a new student: y/n?" << endl; cin >> instructorChoice;
你的打印还不错,但你可以写,可以使用运算符重载(string get_student_data()
operator<<
)
评论
cin >> studentGrade
cin
if (cin >> studentGrade)
cin >> ...
John Jones 47
cin >> studentName; cin >> studentGrade
studentName
"John"
J
Jones
studentGrade
>>