PDA

View Full Version : What for Enter Key Press



DGortze380
September 4th, 2008, 09:12 PM
Hey all,

I blew through writing this program for class right up until this last problem. I can't get the damn thing to wait for the user to press the enter key.

example (not the actual program):



#include <iostream>
using namespace std;

int main()
{
int x=0;

while (true)
{
cout << x << endl
x++;

switch (x) //syntax for this switch example is probably off, didn't look it up.
case 1:
//some function call
break;
case 2:
//some function call
break;
default:
break;

cout << "press enter to continue";

// WTF will work here? I tried the following:

// cin.ignore();
// cin.get();
// cin >> someVariable
// getch(); //this one didn't work because I don't have the library and don't want to install it. Need an ANSI C solution
// all of these do nothing, it just keeps looping.
// cin >> someVariable works, but you have to type something and press enter. I just want to wait for 1 key stroke.
}

return 0;
}



help.

DGortze380
September 4th, 2008, 09:19 PM
ok, here's a twist, cin.ignore() works in the code I just posted but not my actual source. Back to work, if i can't figure it out I'll post the source.

DGortze380
September 4th, 2008, 09:31 PM
I've got it working, but it's not really a clean solution. anyone that feels like looking at the source let me know I'll PM it to you.

LaRoza
September 4th, 2008, 09:41 PM
getch is not standard. Use getchar() (C) or another input function.

jinksys
September 5th, 2008, 12:55 AM
I've got it working, but it's not really a clean solution. anyone that feels like looking at the source let me know I'll PM it to you.

You can't just post it here?

cin.get(); works for me.

DGortze380
September 5th, 2008, 04:57 PM
You can't just post it here?

cin.get(); works for me.

I could, I try to avoid it because it's for a class, then it shows up in MOSS, I have to explain that it's not plagiarized, turns into a big thing.

I think the problem is that I'm using a switch statement in the loop. It work when I put cin.ignore() in each case and after the switch inside the loop. I'll edit the example in the first post to make it more clear.