0

Is it possible to do this:


Print out something on the screen, when the user presses enter, it should print out something else, but on the same line:

cout <<"Hello ";
-wait for an enter-
cout <<"there";

with it looking like this:
Hello there

?

Edited by sha11e: n/a

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by mike_2000_17
0

Not possible in the confines of standard C++. You would need either more control over the display, or more control over the input shell. What compiler and operating system are you using?

0

Not possible in the confines of standard C++. You would need either more control over the display, or more control over the input shell. What compiler and operating system are you using?

Compiler: Code Blocks
OS: Windows 7 Home Premium

I looked around, and would flushing the input stream help somehow?

0

I looked around, and would flushing the input stream help somehow?

No. You need raw input because otherwise by the time your program gets any characters, a newline has already been displayed by the shell. Unless you've replaced the compiler, Code::Blocks supports raw character input by way of getch():

#include <iostream>
#include <conio.h>

using namespace std;

void wait_for_enter()
{
    while (getch() != '\r')
        ;
}

int main()
{
    cout << "Hello " << flush;
    wait_for_enter();
    cout << "there\n";
}

That's the simplest solution to your problem.

0

You can achieve something reasonably close to what you want with this:

#include <iostream>
#include <string>

int main() {
  std::cout << "Hello";
  std::string tmp;
  std::getline(std::cin,tmp,'\n');
  std::cout << "there" << std::endl;
  return 0;
};

This will just have the effect of printing the second part when the user presses enter (and it will ignore any other characters typed, but they will still display on the screen, and so will the enter character (it will make a new line)).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.