Hi guys i'm trying to write code that requires the user to hit return twice to terminate the project and I can't figure it out. I think i am supposed to use some variation of

if(input == " "){
   exit(1);
}

with a Boolean expression but I can't get it to work.

Recommended Answers

All 4 Replies

In your program do you prompt the user to hit 'enter' twice?Or should the program halt during two stroke captures?If it's the first case you should look for the hex value of the key enter.Otherwise you may need a separate thread in your program to monitor keyboard strokes.Depending on the operating system you are using you should use an API function such as the Win32 GetKeyState().

I did this....simple but works

int main()
{
  char c1, c2;
  c1 = getchar(); // get first input
  c2 = getchar(); // get second input
  if (c1 == '\n' && c2 == '\n') // if post inputs are enter
     exit(1); // exit
}

I did this....simple but works

int main()
{
  char c1, c2;
  c1 = getchar(); // get first input
  c2 = getchar(); // get second input
  if (c1 == '\n' && c2 == '\n') // if post inputs are enter
     exit(1); // exit
}

So what happens if someone types in " [ENTER][ENTER]"?

Put each getchar() in a loop testing for '\n': while (getchar() != '\n');

Thank you guys for your help, I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.