0

特定のケースに直接「ジャンプ」できる簡単な方法があるかどうか疑問に思っていました。

do while ループを使用して実行できることはわかっています。もっとエレガントな方法があるかどうか疑問に思っていました。

たとえば(もちろんコード全体ではありません):

.
.
.
case 8:
            {
                if (grades.findStudent(tempid1)==-1)
                {
                    cout << "\nOne of the ID's you have entered wasn't found. Try again.\n" << endl;
                    case(8); //<- <- <- something like that
                }

            }
            break;
.
.
.
4

5 に答える 5

0

わかりませんが、これはある種の「メニューシステム」であり、8は「学生の検索」であると思われます。

その場合、次のことを行います。

            while (grades.findStudent(tempid1)==-1)
            {
                cout << "\none of the ID's you have entered wasn't found. Try again.\n" << endl;
                // add code to ask for tempid1 again
            }

したがって、「if」の代わりに「while」を使用します。

于 2013-04-12T18:52:51.197 に答える
0

ここに間違った制御構造があります。次のようなものが必要です。

int studentId;
while(true) {
    studentId = getStudentId();  // Sorry, I'm rusty.
    if (isValid(studentId)) { break;}
}
switch (...) {
    ...
}

一度に 1 つのことを行います。あなたはダフのデバイスのダフではありません。それを見てください。それは怖い!

于 2013-04-12T18:48:08.733 に答える
0

ひょっとしたらこっちの方がいいかも

case 8:
   while (grades.findStudent(tempid1)==-1)
   {
      cout << "\nOne of the ID's you have entered wasn't found. Try again.\n" << endl;
      // Do whatever to enter tempid1 (perhaps a function here that is used
      // before the start of this switch statement?
   }
于 2013-04-12T18:49:30.830 に答える