2011-06-03 35 views
-2

我需要以下C++代碼的幫助,試圖在程序結尾處添加一個continue,以便它爲用戶指定尺寸的矩形並要求用戶重新執行程序。當我嘗試使用遞歸時,我的C++程序不能工作嗎?

在程序的最後部分沒有愚蠢的if和else語句的情況下編譯並運行它,並且它可以工作。但隨着continue /遞歸失敗慘敗。 LOLZ。我= noob。


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; 
} 
+0

我知道有可行的一個更好的方式做它,我只是不知道該怎麼做繼續抱歉,哈哈 – user352258 2011-06-03 00:56:31

+1

(移動在[答案](http://stackoverflow.com/questions/6221953/noob-編程幫助c/6222004#6222004)) – 2011-06-03 01:02:23

+1

@Matteo:值得承認'壞'和'非法'之間的區別。 'goto'通常不好,在main中遞歸是非法的。 – ildjarn 2011-06-03 01:05:04

回答

5

continue是C++中的關鍵字,所以你不能有一個同名的變量。

2

繼續是一個C++的關鍵字,使用不同的名稱爲它

,而不是

char continue; 

嘗試

char cont; 
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'); 
} 
+1

您的測試無法檢查用戶是否首先輸入了某些東西!如果你只是按回車,'doYouWantToContinue'將保持不變(並且可能未初始化)。 – MSalters 2011-06-03 10:39:06

4

除了continue是一個保留字,它是非法的呼叫main在C++中。從'03標準中,§3.6.1/ 3:

功能main不應在程序中使用。 main的鏈接是實現定義的。聲明maininlinestatic的程序不合格。名稱main未另外保留。 [示例:成員函數,類和枚舉可以調用main,其他名稱空間中的實體也可以調用成員函數,類和枚舉。 ]

+0

+1提及有關'繼續'被保留和調用'main'的非法性,並提供解釋。 – razlebe 2011-06-03 10:14:48

3

繼續用於短路迴路,例如:

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

    do_something_interesting_with(i); 
} 
0

我的2¢:扔掉所有這些東西,並把它改寫牢記

  1. goto小號標籤不好;
  2. 他們應該循環(可能do ... while你的情況)來代替;與保留/已使用的名稱
  3. 聲明變量/標籤是壞的(在大多數情況下,這是非法的);
  4. 遞增main不好(實際上,根據標準它是違法的);
  5. 好的縮進不是可選的(即使編譯器不介意)。
  6. #include s是不是可選。

也許移動至用戶輸入/驗證邏輯在一個單獨的函數來避免代碼重複。

相關問題