Hit enter and set that value to an char?

Hey guys...again. Hehe i seem to have my problems with what seems to be the simplest stuff. To the point. So what I was trying to do is make it so when a user hits enter the program does one thing and exits if they actually type something, here is a crude explination:
1
2
3
4
5
6
7
8
if(user hits enter)
{
//do some stuff cause the user hit enter
}
else
{
return 0
}


Now I understand that is the completely wrong syntax for something like that, but I hope the main idea is perceived. Here is the code I am trying to implement that into. Where as I thought '\n' would do the trick.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void slot_display();
int numbers(int x);

int display[9] {0,1,2,3,4,5,6,7,8};

int main()
{
    char play = '\n';
    srand (time(NULL));

    while(play == '\n'){

          cout<<"Press Enter to spin the wheel. Enter anything else to exit. :";
            cin>>play;
            if(play == '\n')
            {

            for(int run = 0;run < 9;run++)
                {
                display[run] = rand() % 9;
                }

                slot_display();

            if(display[3] == display[4] && display[4] == display[5])
                {
                    cout<<"You are a winner!!\n\n";
                }
                else{
                    cout<<"You lose.\n\n";
                }


            }

            else
            {
                return 0;
            }



    }

}

void slot_display()
{

cout<<"     |     |     "<<endl;
cout<<"  "<<display[0]<<"  |  "<<display[1]<<"  |  "<<display[2]<<endl;
cout<<"     |     |     "<<endl;
cout<<"     |     |     "<<endl;
cout<<"--"<<display[3]<<"--|--"<<display[4]<<"--|--"<<display[5]<<"--"<<endl;
cout<<"     |     |     "<<endl;
cout<<"     |     |     "<<endl;
cout<<"  "<<display[6]<<"  |  "<<display[7]<<"  |  "<<display[8]<<endl;
cout<<"     |     |     "<<endl;



}


Thank-You in advanced.
Last edited on
I think i know what your looking for, seeing as you want someone to press 'enter' to do something you need to use the kbhit(); function found in the conio library. If you don't have the ability to use this library than i think there is a separate function on windows. Just ask me if you don't have the conio library. But getting to your problem, I would like you to take a look at this code to see how to use that function, adapting it to use enter should be easy once you run it but if your confused then you can ask me for more help.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream.h>
#include <conio.h>

int main()
{
int key_stroke=0;
while (key_stroke!=27) // 'esc' = 27
{
clrscr();
cout<<"Press any key to see it's ASCII translation."<<endl;
	while (!kbhit())  // conio.h function to get direct key board feed
	{
	key_stroke=0;
   // redundant loop to keep the computer waiting for a key to be pressed
	}
key_stroke=getch();     // key = character entered
cout<<"You pressed key: "<<key_stroke<<endl;
//                           ^ display's ASCII equivalent of key stroke
cout<<"Press enter to continue..."<<endl;
cin.get();
}
return 0;
}
That helped oh so very much 8) Thank-You!
you don't have to use conio. Use getline instead.

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

int main()
{
    char play[2] = "";

    while(!play[0])  //while play is an empty string
    {
        cout<<"Press Enter to spin the wheel. Enter anything else to exit. :";
        cin.getline(play,2);
        if (!play[0])  //play is empty means the user only hits enter key
        {
            //...
        }
        else
        {
            return 0;
        }
    }
}
Topic archived. No new replies allowed.