2016-05-16 66 views
-5

你能說出什麼是錯誤嗎?它編譯並運行正常,但程序不會結束。C++程序不會結束

我正在使用dev C++。該代碼是:

#include <iostream> 
using namespace std; 

main() 
{ 

    char num; 
again: 

    int usr; 
    float area; 

    cout << "\nEnter 1 to calculate area of rectangle\n"; 
    cout << "Enter 2 to calculate area of trapezoid\n"; 
    cout << "\nEnter your choice: "; 
    cin >> usr; 

    if (usr == 1) 
    { 
     double width, length; 
     cout << "Enter the width of rectangle: "; 
     cin >> width; 
     cout << "Enter the length of rectangle: "; 
     cin >> length; 
     area = length * width; 
     cout << "The area of rectangle is: " << area; 
    } 

    if (usr == 2) { 
     double base1, base2, height; 
     cout << "Enter the base 1 of trapezoid: "; 
     cin >> base1; 
     cout << "Enter the base 2 of trapezoid: "; 
     cin >> base2; 
     cout << "Enter the height of trapezoid: "; 
     cin >> height; 
     area = (((base1 + base2)/2) * height); 
     cout << "The area of trapezoid is: " << area; 
    } 

    cout << "\n\ndo you want to do another calculation?"; 
    cin >> num; 
    { 
     goto again; 
    } 

    if (num == 'y') 
    { 
     goto again; 
    } 

    if (num == 'n') { 
     exit(0); 
    } 
} 
+5

你期望什麼'cin >> num; {goto again; }'做什麼? – Andrew

+0

我說你應該改進它第一 –

+0

如果我刪除括號其結束,並不會問第二次我希望它再次問,如果我說'是'是它應該再次做計算,當我說'N'沒有它退出但它不會退出其開始它 –

回答

5

決不用不完goto,除非你有一個很好的理由(你沒有)。

cin >> num;{ 
goto again; 
} 

我不知道你爲什麼寫這個,但這就是問題所在。如果你會用正確的格式寫它,它會是這個樣子的

cin >> num; 
{ 
    goto again; 
} 

支架{}只是改變範圍(變量和這些有用的),但不會做別的。 goto again;仍然執行,所以你遇到了無限循環。

這兩個條件後,它是好的,所以只是刪除{ goto again; }將「解決」你的問題。

+0

我應該刪除它,或者你可以爲我修復它,請檢查它讓我知道其中的任何其他錯誤 –

+0

@AnamMiR如果你刪除它將工作,因爲'如果'後確定,所以你的程序將退出。 – Rakete1111

+0

cout <<「\ n \ ndo你想做另一次計算?」; cin >> num; 再次轉身; if(num =='y') goto again;如果(num =='n'){ exit(0);} } –

0

刪除線沒有44在你的代碼

代碼

cin >> num;{ 
goto again; 
} 

溶液中取出

轉到再次

cin >> num;{ 

} 

這將溶膠ve你的問題

+0

'cin >> num''後面的括號是否確定?他們的服務目的是什麼? –

+0

您可以刪除括號。 – vab

+0

可以幫助重寫和退出程序 –