Я пишу код, который для одной или нескольких строк строк находит, если общий ввод имеет только «круто» (это первая средняя и последняя строка - это те же самые) линии, uncool "или их сочетание.jumping out loop в конкретном случае
Проблема, с которой я столкнулась, - это когда я вводим четное число, цикл while завершается. Отладка Я обнаружил, что, как раз перед тем, как выпрыгнуть, 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 && uncool) break; Это может помочь –
@Saiful Почему? – LogicStuff
@SaifulIslam: Не совсем, учитывая, что ['и' означает '&&'] (https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#C.2B.2B_operator_synonyms). – Cornstalks