stopping when user presses enter only, during c-string entry

Okay this is extremely silly, but I'm having a huge hard time with it.
I'm trying to make a loop and the user adds a new product to an array on each iteration. The iterations stop when the user just enters an empty string. We're only allowed to use c-strings however. I can't seem to figure out how to do it and I've tried so many things and am running out of time to turn in my assingment please help me.

I've tried.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  char temp[51];
  bool flag = true;
  do
  {
  std::cout << "Enter item name (Just Press Enter to Exit): "
  std::cin >> temp;
  std::cin.ignore();
  if(temp[0] == '\n')
  {
  flag = false;
  }

  
  } while( flag );
Hello, again, I also tried doing this:


1
2
3
4
5
6
7
8
char temp[51] = { '\0' };
	int count = 0;
	do
	{
		std::cout << "Enter item description (press just Enter to end):";
		std::cin >> temp;
		std::cout << temp;
	} while (temp[0] != '\0');


It's still not working!!!
The extraction operator, when extracting strings or numbers leaves the end of line character in the input buffer, it isn't extracted so you really can't test for that. You may want to consider using getline() instead of using the extraction operator>> and you may want to ask the user to signal EOF instead of looking at the string.

Topic archived. No new replies allowed.