Check if 1 or 2 is pressed

This is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>



int main()
{
    std::cout << "You wake up hanging upside down from your parachute, dangling about 15 feet" << std::endl << "in the air." << std::endl << std::endl;
    std::cout << "In the distance you see a small cave below a cliff." << std::endl << std::endl;
    std::cout << "What do you do?" << std::endl;
    
    std::cout << "1) Scale the cliff" << std::endl;
    std::cout << "2) Enter the cave" << std::endl;
}


How can i then check if 1 or 2 is pressed and make it print different things depending on which key is pressed?
Keypresses are not directly supported through the standard C++ library. The only way to do it via the standard libraries would be to use cin. http://www.cplusplus.com/reference/iostream/cin/

If you, however, insist in using keypresses instead of regular inputs, you should try the popular NCurses library which is available on the internet, free of charge.

If you are on a Windows system, you might also try the (unpopular) conio.h library.
limeua30681
For a simple example of an 'cin' command to illustrate (because i am bored);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
#include <iostream>
using namespace std;

int main (){

int choice;
cout << "What is your favorite number, 1 or 2?" << endl;
cin >> choice;

if (choice == 1) {
cout << "oh, so your favorite number is 1 ey!" << endl;
} ;
if (choice == 2){
cout << "so your favorite number is 2 eyy!" << endl;
}

system("pause");
return 0; 
} 



EDIT: Please no lectures on System("pause"); please. Not in the mood. lol, it's simple for illustration purposes
Last edited on
Topic archived. No new replies allowed.