문제는 위와 같이 while 안에 있는 출력문을 없애면, 에러가 나타난다.
원인은 std::getline()을 통해 얻은 string은 마지막에 개행을 포함하지 않기 때문에 while문이 정상적으로 종료되지 않는 것이 문제였다.
그 사이에 std::cout이 있을 때 정상적으로 종료되는 이유는 잘 모르겠다…
#include <iostream>#include <string>#include <array>#include <algorithm>int main(){ std::array<int, 26> alphabet = {0}; // A to Z int index; // index of alphabet std::string input; char c; // input elements std::getline(std::cin, input); // Store the number of lower case alphabet for(size_t i=0; i<input.length(); ++i) { c = (char) tolower(input[i]); index = c - 'a'; ++alphabet[index]; } int max = -1; int where_max; // Find the most frequently used alphabet character for(size_t j=0; j<alphabet.size(); ++j) { if(alphabet[j] > max) { max = alphabet[j]; where_max = j; } } std::sort(alphabet.begin(), alphabet.end(), std::greater<int>()); if(alphabet[0] == alphabet[1]) std::cout << "?" << std::endl; else std::cout << (char) toupper(where_max + 'a') << std::endl; return 0;}