C++ getline method not working

前端 未结 4 1987
借酒劲吻你
借酒劲吻你 2021-01-24 23:41

I\'m sorry but I\'m quite new to C++ but not programming in general. So I tried to make a simple encryption/decryption. However when I added the modification to my previous code

4条回答
  •  再見小時候
    2021-01-24 23:54

    That's because std::cin >> op; leaves a hanging \n in your code, and that's the first thing getline reads. Since getline stops reading as soon as it finds a newline character, the function returns immediately and doesn't read anything more. You need to ignore this character, for example, by using cin.ignore(std::numeric_limits::max(), '\n'); (std::numeric_limits is defined in header ), as stated on cppreference.

提交回复
热议问题