0

Possible Duplicate:
Read a password from std::cin

I'm new to c++ programming and i have the following situation. I'm reading a char from stdin and assign it to a variable "v". Using "cin" the variable is printed to stdout before reading it. I want only to read it and store in "v", not print it.

#include <iostream>  
using namespace std;  

int main()  
{  
    char v;  
    cout << "Choose!\n";  
    cout << "a. Choose a\n";  
    cout << "b. Choose b\n";  
    cin >> v;  
    return 0;  
}

Thanks in advice.

Community
  • 1
  • 1
maryo
  • 153
  • 1
  • 5
  • maybe `cin.get(&v);` or `getch()` – Imre L Apr 09 '12 at 12:24
  • i did it with `v = getch();` tnk for pointing me to it @outis @Imre L – maryo Apr 09 '12 at 12:32
  • @maryo - `getch` makes your program curses specific of course. – Omnifarious Apr 09 '12 at 12:36
  • the problem still persists. if "v" is of int type `getch()` will return it's ASCII value. – maryo Apr 09 '12 at 12:52
  • i've managed to get it work with both `int` and `char`. `#include #include using namespace std; int main() { //for using a char char cv; cout << "Choose char!\n"; cout << "a. Choose a\n"; cout << "b. Choose b\n"; cv = getch(); cout << cv << "\n"; //for using a int int iv; cout << "Choose int!\n"; cout << "1. Choose 1\n"; cout << "2. Choose 2\n"; iv = getch(); cv = (char) iv; cout << cv << "\n"; return 0; }` any better workaround for the case of using a `int`? – maryo Apr 09 '12 at 13:43

0 Answers0