1

I have a programme that when the command (cin) help is entered it brings up a help note. That note looks somewhat like this:

C++

if (cmd == "help")
{

    cout << "▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄\n";
    cout << "██             Help Menu                ██\n";
    cout << "▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀\n";
    cout << "Press a key then enter to continue\n" << string( 10, '\n' );
    cin.ignore();
    return 0;
}

the string var cmd being 'help'.

I am compiling this on a mac and have included:

C++

#include <iostream>
#include <string>
using namespace std;

What code can i use to make this work? cin.ignore(); doesn't seam to do anything and nether does cin.get();

abaft
  • 152
  • 2
  • 12
  • Are you asking for how to obtain user input? – jliv902 Feb 25 '14 at 21:48
  • This might help: http://stackoverflow.com/questions/7862582/press-anykey-to-continue-in-linux-c – trutheality Feb 25 '14 at 21:49
  • no and yes i need the program to stop till enter is pressed on the keyboard – abaft Feb 25 '14 at 21:49
  • Do you mean stop or wait? Stop suggests some background task like a video, wait suggests waiting for a key press. – ctrl-alt-delor Feb 25 '14 at 21:57
  • 1
    On that 10 newlines, note everyone has a different size terminal. I recommend the curses library for this type of screen layout. – ctrl-alt-delor Feb 25 '14 at 21:59
  • You say `cin.ignore()` doesn't seem to do anything. What problem are you seeing? The program seems like it works to me; The program pauses at that point until you enter some input, and `cin.ignore()` consumes one character of that input. Perhaps the issue is that you want it to consume all the characters of input up until the enter key is pressed? In that case you need `cin.ignore(numeric_limits::max(), '\n')`. – bames53 Mar 07 '15 at 18:15

2 Answers2

0

I think you might want something like:

string input;
getline(cin, input);
PlasmaPower
  • 1,864
  • 15
  • 18
-1
while (x!='\n')
{
    cin.get(x);
};

EDIT: Okay, I've done some digging. The above was my solution to the same problem. It looks like the reason you're having trouble is you need to clean out your input stream before you can use cin.get() or cin.ignore() to wait for ENTER, so:

cin.ignore(INT_MAX,'\n');
cin.ignore();

The bottom line is, you have to do it twice to make it work.

Nick
  • 1
  • 2