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

?

Recommended Answers

All 4 Replies

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?

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?

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.

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)).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.