Hi I am coding a terminator for my cmd.exe, if the userinput is "" or "\n" which means the user pressed 'Enter' I have to close the cmd, can anyone help me with this?
1 2 3 4 5 6 7 8 9
char checker[5];
cout << "Please key in a total of 6 Numbers, after each number press enter, after that key in 0 to end it, " << endl;
for(int i = 0; i <10; i++)
{
cin.getline(checker, 1);
if (checker[0] == "") // here shows error
{
return;
}
If you press enter without entering any characters, then resulting string will not contain
any.
Additionally you are trying to compare char wiht char* — clearly uncompatible types. Usually you coud check if string has length of 0 by calling a strlen or manually checking if first character is null character, but there is another problem in your code.
As c-strings require 1 more byte for null terminator, your getline cannot extract any characters. It will just place null terminator (as there is space only for it) and sets stream into failed state (breaking all other inputs). If you want to read just one character, use stream::get() method.