0
我正在寫一個代碼,對於一行或多行字符串,查找整體輸入是否只有「酷」(它的第一個中間和最後一個字符串是相同的)行,只有「 uncool「系列或兩者的組合。在特定情況下跳出外部循環
我遇到的問題是每當我輸入偶數循環終止。調試我發現,在跳出之前n得到值0,但我不明白這將如何使循環結束。
這是代碼:
#include <iostream>
using namespace std;
int main() {
// Bool has control if we have found a cool line/non-cool line
bool cool = false;
bool uncool = false;
int n; //lenght of input
while (cin >> n) {
if (cool and uncool) break; // we have found one of each so we know it is a mixed input
else if (n%2 == 0) uncool = true; // if the lenght is even there is no middle string
else {
// we are trying to see if the middle and last string are equal to the first
string comparing_string;
cin >> comparing_string;
string rest_of_sequence;
bool this_is_cool = true;
for (int i = n-2; i >= 0; i--) { // we input the rest of strings and compare them to the first
cin >> rest_of_sequence;
if ((i == n/2 or i == 0) and rest_of_sequence != comparing_string) this_is_cool = false;
}
if (this_is_cool) cool = true;
else uncool = true;
}
}
if (cool and uncool) cout << "both types" << endl;
else if (cool and not uncool) cout << "all cool" << endl;
else if (uncool and not cool) cout << "none cool" << endl;
}
任何幫助表示讚賞!我目前在uni的第一年,並始終打開推薦的書籍/網頁/視頻,以繼續學習:)
替換if(cool and uncool)break; to if(cool && uncool)break; 這可能會幫助 –
@Saiful爲什麼會這樣? – LogicStuff
@SaifulIslam:並非如此,因爲['和'意味着'&&'](https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#C.2B.2B_operator_synonyms)。 – Cornstalks