这是一个家庭作业问题,但我已经完成了,这只是为了我的理解。补充一下,我不能使用 STL :(
基本上,我们要实现一个“学生”类,它有 5 个参数(名字/姓氏、ID、年级和专业)。然后每个学生对象必须存储在一个数组中(我们称之为教室)。
当用户添加新学生时,我的问题就出现了。起初我的代码只是像这样获取每个参数:
cin >> first >> last >> id >> grade >> major;
然后将它们传递给另一个将其存储在数组中的函数。如果用户按应有的方式输入所有内容,这可以正常工作,例如输入:
students> add John Smith 123 Freshman Computerscience
但是当输入不正确时,特别是使用 ID 输入(它是一个 int),如下所示:
students> add John Smith 123abc Freshman Computerscience
它将以“abc”作为成绩,以“新生”作为专业,其余的都会出现错误。
所以我决定将整个输入作为一个字符串解析出来,代码如下:
class Student {
private:
string first_name;
string last_name;
int ID;
string classification;
string major;
public:
Student();
/* Main class functions */
void add(string, string, int, string, string);
void print(string);
bool remove(int);
/* Helper class functions */
bool empty();
bool ifexist(int);
};
int main()
{
bool done = false;
string command, first, last, grade, major;
int id;
string query, input;
string inputArray[6];
string token = " ";
while(!done) {
cout << prompt;
cin >> command;
if(command == "add")
{
/* Get new student info */
cin >> first >> last >> id >> grade >> major;;
// this commented code is my attempt to fix error
// count = 0;
// while(getline(cin, input)) {
// stringstream s(input);
// while(s >> token) {
// inputArray[count] = token;
// count++;
// }
// if(count == 5) break;
// }
// first = inputArray[0];
// last = inputArray[1];
// if(atoi(inputArray[2].c_str()))
// id= atoi(inputArray[2].c_str());
// else cout << ERROR_INPUT << endl;
// grade = inputArray[3];
// major = inputArray[4];
// for (int i=0; i<5; i++) {
// cout << inputArray[i] << endl;
// }
//cout << first << endl << last << endl << id << endl << grade << endl << major << endl;
int i = getInsertPoint();
bool e = ifexist(id);
/* Check if student already exists */
if(e) cout << "Error! Already exists, try again" << endl;
else classroom[i].add(first, last, id, grade, major);
}
else if(command == "print")
{
/* Get query type to be printed (ex: firstname) */
cin >> query;
print(query);
}
else if(command == "remove")
{
cin >> id;
remove(id);
}
else if(command == "quit") done = true;
else cout << ERROR_INPUT << endl;
}
return 0;
}
我可以打印出数组,一切都很好,但是当我将每个数组传递给将它存储在对象中的函数时,什么也没有出现。但是,当我使用我提到的第一个代码时,它确实有效。
所以我的问题是:有没有更好的方法来解析/检查输入字符串(不带 STL),允许我将每个令牌单独发送到另一个函数?