C++ 循环中的字符串输入。第一个输入工作正常,在其他输入上跳过
我需要为家庭作业编写一些程序,但我在字符串输入方面有一些问题 问题是我不能做2>=输入,它只是跳过了它。我不知道为什么。我希望有人能原谅我 我的代码:C++ 循环中的字符串输入。第一个输入工作正常,在其他输入上跳过,c++,C++,我需要为家庭作业编写一些程序,但我在字符串输入方面有一些问题 问题是我不能做2>=输入,它只是跳过了它。我不知道为什么。我希望有人能原谅我 我的代码: for (int f1=0; f1<5; f1++) { temp_st = new Schulernoten(); cout << "Name eingabe: "; name = "\n"; getline(cin, name); temp_st->set_name(name)
for (int f1=0; f1<5; f1++) {
temp_st = new Schulernoten();
cout << "Name eingabe: ";
name = "\n";
getline(cin, name);
temp_st->set_name(name);
// note for informatik
cout << endl << "Note Informatik eingabe: ";
cin >> note;
while (temp_st->check_note(note)) {
cout << "Falsch eingabe: ";
cin >> note;
}
temp_st->set_note_inf(note);
// note for math
cout << endl << "Note Math eingabe: ";
cin >> note;
while (temp_st->check_note(note)) {
cout << "Falsch eingabe: ";
cin >> note;
}
temp_st->set_note_mat(note);
// ausdrucken
cout << temp_st->get_name() << endl << temp_st->get_note_inf() << endl << temp_st->get_note_mat() << endl << endl;
}
正如您所看到的,第一个输入工作正常,然后它只是小口小口地留下空白
我怎样才能纠正这个问题?谢谢好的,我知道了。我忘了冲洗cin>> 这是正确的代码:
for (int f1=0; f1<5; f1++) {
temp_st = new Schulernoten();
cout << "Name eingabe: ";
name = "\n";
getline(cin, name);
temp_st->set_name(name);
// note for informatik
cout << endl << "Note Informatik eingabe: ";
cin >> note;
cin.ignore();
while (temp_st->check_note(note)) {
cout << "Falsch eingabe: ";
cin >> note;
cin.ignore();
}
temp_st->set_note_inf(note);
// note for math
cout << endl << "Note Math eingabe: ";
cin >> note;
cin.ignore();
while (temp_st->check_note(note)) {
cout << "Falsch eingabe: ";
cin >> note;
cin.ignore();
}
temp_st->set_note_mat(note);
// ausdrucken
cout << temp_st->get_name() << endl << temp_st->get_note_inf() << endl << temp_st->get_note_mat() << endl << endl;
}
for(int f1=0;f1检查注释(注释)){
注意;
cin.ignore();
}
临时->设置注释信息(注释);
//数学笔记
注意;
cin.ignore();
同时(临时->检查注释(注释)){
注意;
cin.ignore();
}
临时->设置注释材料(注释);
//奥斯德鲁肯
无法获取_name(),您甚至没有检查输入操作是否成功。尝试如果(!(cin>>x))cerr>note
在流中留下一个换行符,那么下一个getline()
读取该换行符,而不是您键入另一个名称的那一行……(您可以调用ignore
来使用note所在行的其余部分……)
for (int f1=0; f1<5; f1++) {
temp_st = new Schulernoten();
cout << "Name eingabe: ";
name = "\n";
getline(cin, name);
temp_st->set_name(name);
// note for informatik
cout << endl << "Note Informatik eingabe: ";
cin >> note;
cin.ignore();
while (temp_st->check_note(note)) {
cout << "Falsch eingabe: ";
cin >> note;
cin.ignore();
}
temp_st->set_note_inf(note);
// note for math
cout << endl << "Note Math eingabe: ";
cin >> note;
cin.ignore();
while (temp_st->check_note(note)) {
cout << "Falsch eingabe: ";
cin >> note;
cin.ignore();
}
temp_st->set_note_mat(note);
// ausdrucken
cout << temp_st->get_name() << endl << temp_st->get_note_inf() << endl << temp_st->get_note_mat() << endl << endl;
}