2016-10-20 90 views
0

一些這可能看起來很簡單,但對我來說我似乎無法弄清楚爲什麼這不起作用。我知道我可以在while循環中每次複製和粘貼以獲得我想要的結果,但是我聽說如果您不得不重複一次以上來寫一個函數!我的代碼將雙擊打印數字,即使有人會輸入8,它仍然會進入while循環。希望有人能解釋爲什麼這發生在我身上。函數驗證輸入C++簡單

int main() 
{ 
int option = selectionHelper(); 
cout << selectionHelper() << endl; 
cout << endl; 
if(option == 8) 
{ 
    cout << "Exiting program..." << endl; 
    cout << endl; 
    cin >> option; 
} 
while (option != 8) 
{ 
if (option == 1){ 

    cout << selectionHelper() << endl; 
    cout << endl; 
    cin >> option; 
}else if(option == 2){ 

    cout << selectionHelper() << endl; 
    cout << endl; 
    cin >> option; 
}else if(option == 3){ 

    cout << selectionHelper() << endl; 
    cout << endl; 
    cin >> option; 
}else if(option == 4){ 

    cout << selectionHelper() << endl; 
    cout << endl; 
    cin >> option; 
}else if(option == 5){ 

    cout << selectionHelper() << endl; 
    cout << endl; 
    cin >> option; 
}else if(option == 6){ 

    cout << selectionHelper() << endl; 
    cout << endl; 
    cin >> option; 
}else if(option == 7){ 

    cout << selectionHelper() << endl; 
    cout << endl; 
    cin >> option; 
}else{ 
    cout << "Invalid input... Please try again..." << endl; 
    cout << endl; 
    cout << selectionHelper() << endl; 
    cout << endl; 
    cin >> option; 
}//end else if statement 
}//end while loop 

}//end function main 

,現在我的功能:

int selectionHelper() 
{ 
int option; 
cout << "1. Initialize seating for new performance." << endl; 
cout << "2. View seating chart." << endl; 
cout << "3. Reserve seats." << endl; 
cout << "4. Calculate tickets remaining in row." << endl; 
cout << "5. Calculate tickets remaining in theater." << endl; 
cout << "6. Calculate total tickets sold." << endl; 
cout << "7. Calculate ticket sales." << endl; 
cout << "8. Exit program." << endl; 
cout << "Option: " << endl; 
cin >> option; 
return option; 
}//end selectionHelper 

謝謝你看我的帖子!

回答

0

這個答案和其他人會告訴你寫什麼代碼來解決你的問題。如果您將來遇到類似問題,請嘗試使用調試器瀏覽代碼。 (經驗也有幫助。)關於代碼冗餘:首先,您最好使用selectionHelper()方法。這表明你有很好的直覺。以下是您可以做的更多:

  1. 查找相同或僅與變量不同的代碼行。
  2. 請考慮冷凝他們在以下幾個方面:
    • 你可以使用一個for循環?
    • 你可以把代碼放在一個方法中嗎(可能抽象出一個變量?)
    • 你可以使用更廣泛的條件語句嗎? (這是你在這裏會是什麼:如果一個數字是1,2,3,4,5,6或7,這也是> 0和< 7.這就是爲什麼我們的範圍。)

程序繼續進行時,他們輸入8,因爲你要求他們輸入8後輸入一個數字!所以,你的代碼的第一個塊可以更簡單:

int option = selectionHelper(); 
cout << option << endl <<endl; // <-- output option, don't call selectionHelper() here again, that's why you get double printing 
if(option == 8) 
{ 
    cout << "Exiting program..." << endl; 
    cout << endl; 
    //cin >> option; // <-- don't input here again! 
} 

這裏的關鍵簡化你的代碼是要認識到你只在少數情況下編寫不同的代碼。所以,這裏是你的while循環:

while (option != 8) 
{ 
    if (option > 0 && option < 8){ 

     cout << selectionHelper() << endl; 
     cout << endl; 
     cin >> option; 
    }else{ 
     cout << "Invalid input... Please try again..." << ends << endl; 
     cout << selectionHelper() << endl << endl; 
     cin >> option; 
    }//end else if statement 
} 
0

您必須修改通過以下方式代碼:

int option = selectionHelper(); 
cout << option << endl; 

而且也:

if(option == 8) 
{ 
    cout << "Exiting program..." << endl; 
} 

您可以簡化代碼到(使用部分由cuniculus作出簡化,而且固定錯誤代碼錯誤使用option):

#include <iostream> 
using namespace std; 

int selectionHelper() 
{ 
int option; 
cout << "1. Initialize seating for new performance." << endl; 
cout << "2. View seating chart." << endl; 
cout << "3. Reserve seats." << endl; 
cout << "4. Calculate tickets remaining in row." << endl; 
cout << "5. Calculate tickets remaining in theater." << endl; 
cout << "6. Calculate total tickets sold." << endl; 
cout << "7. Calculate ticket sales." << endl; 
cout << "8. Exit program." << endl; 
cout << "Option: " << endl; 
cin >> option; 
return option; 
}//end selectionHelper 

int main() 
{ 
int option = selectionHelper(); 
while (option != 8) 
{ 
    if (option > 0 && option < 8){ 
     option = selectionHelper(); 
    }else{ 
     cout << "Invalid input... Please try again..." << ends << endl; 
     option = selectionHelper(); 
    }//end else if statement 
} 
cout << "Exiting program..." << endl; 
return 0; 
}//end function main 

您的錯誤主要是在調用selectionHelper的次數過多,因爲只需要設置option變量的一次。你在整個if-else結構中都有類似的事情發生 - 但我會留給你解決,如果需要的話,解決方案是一樣的。

+0

這也幫助我看到哪部分代碼導致了錯誤。感謝您回覆我的文章! – Austin

+0

太好了。請注意那些您認爲有用的答案(點擊答案左側的uparrow)。 –