-3

次の C++ コードについて助けが必要ですcontinue。プログラムの最後に a を追加して、ユーザーが指定した四角形の寸法を作成し、ユーザーにプログラムをやり直すように求めます。

プログラムの最後の部分でばかげた if ステートメントと else ステートメントを使用せずにコンパイルして実行すると、動作します。しかし、continue/ 再帰を使用すると、惨めに失敗しました。笑。私=ノブ。


int main()
{


    int height, width, tmp, tmp2;

    char continue;

    cout << "Please Enter The Height Of A Rectangle (whole numbers only): ";
height:
    cin >> height;
    if(height<1)
    {
        cout << "   Please Enter A Height Of Between 1 And 20: ";
        goto height;
    }
    cout << "Please Enter The Width Of A Rectangle  (whole numbers only): ";
width:
    cin >> width;
    if(width<1)
    {
        cout << "   Please Enter A Width Of Between 1 And 38: ";
        goto width;
    }

    cout << ' ';                                         // Add a space at the start (to neaten top)
    for(tmp=0; tmp!=width; tmp++) cout << "__";          // Top Of Rectangle
    for(tmp=0; tmp!=(height-1); tmp++)
    {
        cout << "\n|";   // Left Side Of Rectangle
        for(tmp2=0; tmp2!=width; tmp2++) cout << "  ";    // Create A Gap Between Sides
        cout << "|";
    }                                  // Right Side Of Rectangle
    cout << "\n|";                                       // Left Side Of Bottom Of Rectangle  to neaten bottom)
    for(tmp=0; tmp!=width; tmp++) cout << "__";          // Bottom Of Rectangle
    cout << '|';                                         // Right Side Of Bottom Of Rectangle (to neaten bottom)

    cout << "Type 'y' if you would like to continue and any other combination to quit.";
continue:
    cin >> continue;
    if(continue == 'y')
    {
        main();
        cout << "\n\n";
        system("PAUSE");
        return 0;
    }
    else
        cout << "\n\n";
    system("PAUSE");
    return 0;
}
4

6 に答える 6

5

コードをwhileループに入れる必要があります。

int main()
{
    //  declaration of variables here

    do
    {
        // code here

        cout << "Type 'y' if you would like to continue and any other combination to quit.";
        cin >> doYouWantToContinue; // change the keyword!
    }
    while (doYouWantToContinue == 'y');
}
于 2011-06-03T00:59:32.243 に答える
5

continueは C++ のキーワードであるため、その名前の変数を持つことはできません。

于 2011-06-03T00:57:55.117 に答える
4

continue予約語であることに加えてmain、C++で呼び出すことは違法です。'03標準から、§3.6.1/ 3:

この関数mainは、プログラム内では使用できません。のリンケージmainは実装定義です。main不正であるinline、または不正であると宣言するプログラムstatic。名前mainは他の方法で予約されていません。[main:他の名前空間のエンティティと同様に、メンバー関数、クラス、および列挙を呼び出すことができます。]

于 2011-06-03T01:01:20.590 に答える
3

継続は、ループを短絡するために使用されます。例:

for (i = 0; i < 10; ++i)
{
    if (f(i))
    {
        continue; // skip the rest of the loop
    }

    do_something_interesting_with(i);
}
于 2011-06-03T01:02:32.983 に答える
2

continueはc++キーワードであり、別の名前を使用してください

それ以外の

char continue;

試す

char cont;
于 2011-06-03T00:59:05.130 に答える
0

私の2¢:これらすべてのものを捨てて、それを覚えて書き直してください

  1. gotosとラベルが悪い。
  2. それらはループに置き換える必要があります(おそらくdo...whileあなたの場合)。
  3. 予約済み/すでに使用されている名前で変数/ラベルを宣言することは悪いことです(そしてほとんどの場合それは違法です)。
  4. 繰り返しmainは悪いです(実際には、標準によれば違法です)。
  5. 適切なインデントはオプションではありません(コンパイラが気にしない場合でも)。
  6. #includesはオプションではありません。

また、コードの重複を避けるために、ユーザー入力/検証ロジックを別の関数に移動することもできます。

于 2011-06-03T01:04:55.707 に答える