Input from console

I want to take an input from user between two quotes like that for example:
MY NAME " HERE IS USER'S INPUT "

I don't want to take the name and then cout the statement as mentioned, I want to take the user's input in-between the two quotes as the user types, Any suggestions!?

Thanks a lot in advance.
I want to take the user's input in-between the two quotes as the user types


I think you're asking how to read from the keyboard without having to wait for the user to press ENTER.

This depends on your operating system. http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.17
Last edited on
I am so sorry the link is not working with me, I tried more than browser but still the same..

Anyway what I meant that I need the second quote to appear to the user while writing the input like that for example :

My name "_"
My name "A_"
My name "Ag_"

and so on until he press an enter.

Thanks
C++ has no understanding of what a keyboard is, or what your monitor is, so these things depends on your operating system. What operating system are you using?
Microsoft Windows
Win generally comes with the header conio.h which contains the function getch, which will read keys from the keyboard without waiting for the user to press ENTER.

There are other ways to do it. This is just one that's quite simple.



Here is some code that will let you write over the same output line repeatedly. This code is cross-platform, just because being cross-platform is nice :) You should probably just use the Win bits.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iomanip>
#include <iostream>
using namespace std;

#ifdef __GNUC__
#include <unistd.h>
#define pause(n) sleep(n)
#elif defined _WIN32
#include <cstdlib>
#define pause(n) _sleep((n)*1000)
#endif

int main()
{

  cout << "CTRL=C to exit...\n";

  for (int units = 10; units > -1; units--)
  {
    cout << setw(2) << setprecision(2) << units << '\r';
    cout.flush();
    pause(1);
  }
  return 0;
}
Topic archived. No new replies allowed.