まず、これは私のCS296クラスのため、すべてがmain()関数内になければならず、ループ、スイッチ、if文以外は使用できません。私のC++の宿題のデザインについての質問
とにかく、クラスの学生数とその名前を入力するようにプログラムに指示します。次に、A-Zのアルファベット順に並んでいる学生に基づいて、どの生徒がラインの前にいなければならず、どちらの生徒がラインの後ろにいるかを表示します。
プログラムは実行されますが、デザインが悪いと感じます。具体的にどのように私はカウント変数を使用しています。おそらくそれについてのフィードバックを得ることを望んでいた。ありがとう!
int main(void)
{
string front, back, student;
unsigned short count = 1, students;
cout << "Enter the number of students in the class. Valid range is 1 - 25." << endl
<< endl
<< "Students: ";
cin >> students;
while (students < 1 || students > 25)
{
cout << endl
<< "ERROR: Valid range is 1 - 25" << endl
<< endl
<< "Students: ";
cin >> students;
}
cout << endl
<< "Enter the name of student " << count << ": ";
cin.ignore();
getline(cin, student);
front = back = student;
for (count = 2; count <= students; count++)
{
cout << "Enter the name of student " << count << ": ";
getline(cin, student);
if (student < front)
front = student;
else if (student > back)
back = student;
}
cout << endl
<< front << " should be at the head of the line." << endl
<< back << " should be at the end of the line." << endl;
return 0;
}
宿題として明確に識別するのにうまくいっています。 –
私はそれが全く悪いとは思わない。私はおそらく、ストリームの抽出とgetlineを 'if(...)'チェックに入れて、操作が成功したかどうかを調べます。 –