How to check if array[0] is ""

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;
		}
"" is an empty C-string. You can't compare that to a char.

Use single quotes for a character literal.
 
if (checker[0] == ' ')         
Last edited on
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.
Is there any other way to code it by using an array?
The end of the string is marked with a null character '\0', so if the string is empty the first char in the array will be a null character.
 
if (checker[0] == '\0')
But you still need to make your array bigger: if you say to getline that it is only 1 char large, only \0 will be stored no matter what you input
Topic archived. No new replies allowed.