Determining User's OS

Hey, all. I'm trying to find a way of making a platform independent way of pausing the program pending a key-press, without requiring a carriage return. I found numerous posts and topics regarding this subject, but this one in particular worried me: http://cplusplus.com/forum/beginner/1574/#msg5512.

Since the problem is apparently platform dependent, is there a way of determining the user's operating system? I am guessing there is a macro somewhere that would work, but I haven't been able to find it. Here's my idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void pressKeyToExit()
{
    std::cout << "Press any key to exit." << std::endl;

// What is the correct macro?
#ifndef __WIN_32
#include <termios.h>
    termios oldSettings = NULL;
    tcgetattr(fileno(stdin), &oldSettings);

    termios newSettings = oldSettings;
    newSettings.c_lflag &= (~ICANON & ~ECHO);
    tcsetattr(fileno(stdin), TCSANOW, &newSettings);

    getc(stdin);
    tcsetattr(fileno(stdin), TCSANOW, &oldSettings);
#else
#include <conio.h>
    _getch();
    // Remove character from output
    std::cout << static_cast<char>(8);
#endif
}


Also, this is a blatantly hackish workaround, and looks like it. Is there any way of making it more graceful?
You can't detect the user's OS. Well, not at run time, anyway. You can use compiler-defined macros to compile different code based on the compiler's target platform. Pretty much what you're doing up there.
http://predef.sourceforge.net/preos.html

As for your real question, provided that you consistently used nothing but std::getline() to get user input from the console, a simple
1
2
std::string s;
std::getline(std::cin,s);

will work everywhere.
Topic archived. No new replies allowed.