How does one make the <enter> key a variable?

I'm trying to construct a program wherein the program will display a line of text, then the user is supposed to press enter, and then another line of text will appear. I have it all figured out except that I can't get the int variable for the cin to be anything other than numbers (sorry if I sound stupid). I would just like the enter key to make the program display the next line of text. Here is my source code thus far:

#include <iostream>
using namespace std;

int main()

{
int iRandom = 0;

cout << "Hey baby..." << endl;
cin >> iRandom;
cout << "How's it going?" << endl;
cin >> iRandom;
cout << "(self-censored for the purpose of this forum)" << endl;

system("PAUSE");
return 0;
}
Last edited on
If you want to read a character then you have to ask for a char.

1
2
3
4
5
char ch; // note: its a type char

cout << "Hey baby..." << endl;
cin >> ch;
cout << "How's it going?" << endl; 
Thank you, Mr/Ms Galik. Unfortunately though, that still only allows the user to type letters. I was hoping that the user could press the 'enter' or 'space' key instead so that the strings of text like, "Hey baby..." and "How's it going?" would not have anything between them, and yet would not all be displayed at once.
That will work for the enter and space keys also. But it will only take ONE key.

EDIT: Actually that wrong, you need to use this: cin.get();

If you want to be able to print separate outputs on the same line after an input then you need to use a special library. You can't really do that from the standard libraries.

You would need something like ncurses: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
Last edited on
Option one: use ignore()

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <limits>
using namespace std;

int main()
  {
  cout << "Hey, handsome, why don't you frob my ENTER key?" << flush;
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  cout << "Woah! You're little forward, aren't you?\n";
  return 0;
  }

Option two: use getline()

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
  {
  cout << "Hey, handsome, why don't you frob my ENTER key?" << flush;
  string s;
  getline( cin, s );
  cout << "Woah! You're little forward, aren't you?\n";
  return 0;
  }
Hey, it worked!

Laughing my arse off, you guys are great. Thx.
Topic archived. No new replies allowed.