I am doing a team project with some people that prefer Linux and some people prefer Windows. We are also not allowed to share code for security reasons. One thing the Windows people love to whine about is a way to hold the window open. So the way they usually solve the problem is with:

#include <conio.h>
getch(); 

This creates a problem for the Linux users since we do not have that library. I have been looking for ways to solve this problem and I really like this idea.

https://www.dreamincode.net/forums/topic/30581-holding-the-execution-window-open/page__st__30__p__575874entry575874

#include <stdlib.h> //For system commands

//Detect and define which command to use
#ifndef WIN32
    #define COMMAND "clear" //Clears a linux console screen
#else
    #define COMMAND "cls" //Clears a windows console screen
#endif

#define wipe() system( COMMAND )
//To clear the console screen use wipe();

#define wait() {printf("Hit \"Enter\" to continue\n");fflush(stdin);getchar();fflush(stdin);}
//To pause the program or hold the console screen open use wait();

This seems to work when I use #defines but not when I just use this line:

printf("Hit \"Enter\" to continue\n");fflush(stdin);getchar();fflush(stdin);

Since I know most of the programmers pretty well I know they use scanf so I added this and it worked:

while ((getchar()) != '\n'); 

Why did the #defines work without the extra while loop, but I needed the extra while loop when not using the #defines? Do #defines behave a little different? I can not look at the others peoples code unfortunately. Is there a better way to solve the problem of holding the window open for Windows people but not screwing up the Linux people?

You left me confused. You wrote you don't share code then proceed to try to use shared code.

Beyond that, the C code you shared looks to be old command line stuff and no indication a windows system was in use.

For decades when I ran a small C app from the command line to today, it's the same as it was 2 decades plus ago with an exception I might be using Gnome or another system. Just open a bash shell, run the command or C app and all is fine. Not once does the base shell in a window system close on me.

fflush() on an input thread is not actually defined in the C standard, thus its behaviour is compiler-dependant. It would be nice if it cleared an input buffer, but for most compilers, it does nothing. It won't fail, but it does not clear the buffer either. The behaviour of fflush() is only defined in the standard for an output buffer.

The #define pre-preprocessor directive does a text replacement for the rest of the source file, prior to the compiler itself getting the source file to parse. The only difference I can see is that your wait() #define has curly braces around it, but when used on its own, you left those out.

Nice Post! I also read your blog and I very impress and I'll go to bookmark your website.

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.