C++ Problem with std::cin std::cout
I have a problem, the code is calmly compiled into the exe, and the first two inputs work perfectly, but when it comes time for the third variable, the program starts to work wrong, while everything in the code is the same https://www.onlinegdb.com/online_c++_compiler - here you can quickly compile.
#include <iostream>
int main()
{
std::cout << "Enter a: "; // просим пользователя ввести любое число
int a = 0;
std::cin >> a; // получаем пользовательское число и сохраняем его в переменную a
std::cout << "Enter b: "; // просим пользователя ввести любое число
char b = 0;
std::cin >> b; // получаем пользовательское число и сохраняем его в переменную b
std::cout << "Enter c: "; // просим пользователя ввести любое число
int c = 0;
std::cin >> c; // получаем пользовательское число и сохраняем его в переменную a
std::cout << "Finish" << std::endl;
return 0;
}
The first two variables go right here's what happens on the third
Please tell me how to do so that I can enter the third variable I have this c. If you have any questions, please ask.
[23.03.2020 19:22] Corrected: Changed the names of the variables, for greater simplicity and understanding.
1 answers
Well, what do you want?
char dll = 0;
std::cin >> dll;
Once in char
read - reads one character (you are not surprised by the output D
in the picture?). And everything else - LL.dll
- comes from the buffer when reading
std::cin >> lvls;
It is clear that this is not a number - that's really in lvls
nothing is read, for cin
the error flag fail
is set (you do not check it...).
It's simple.
Read lines-in lines :)