0

What could we use in place of getch() in the following code

char a; a=getch();

11th Jun 2018, 9:28 AM
Abhishek Kumar
7 Antworten
+ 1
Abhishek Yes, and there are very many things you can't do in standard C/C++ using only the standard library: GUIs, graphics, network ports, calling other programs, getting keyboard/mouse/other peripheral input, navigating a file system (in C and in C++ before C++17), multi-threading (in C before C11 and in C++ before C++11). It doesn't mean that you shouldn't. It's also worth noting that C was built with Unix in mind, where you don't end your programs waiting for input because the output stays on the screen.
11th Jun 2018, 11:18 AM
Vlad Serbu
Vlad Serbu - avatar
+ 2
In C++ use cin to accept input and cout to write output. #include <iostream> int main() { char a; std::cin >> a; // read one character std::cout << a // output the character return 0; } In C use scanf() to accept input, printf() to write output. #include <stdio.h> int main() { char a; scanf("%c", &a); // read one character printf("%c", a); // output the character return 0; } Hth, cmiiw
11th Jun 2018, 10:44 AM
Ipang
+ 1
char a will save keyboard button you pressed, it will save a character. We often told that getch will hold program execution, however this is not the main purpose of this function. It's just side effects of input a char to the console.
11th Jun 2018, 10:05 AM
Owenizedd
Owenizedd - avatar
+ 1
a = getchar(); in C or std::cin >> a; in C++ Note that there is no way of doing unbuffered input in standard C/C++
11th Jun 2018, 10:55 AM
Vlad Serbu
Vlad Serbu - avatar
0
Owenizedd but what if I want to perform that function as in every platform it won't work
11th Jun 2018, 10:54 AM
Abhishek Kumar
0
Vlad Serbu so we can't create any password type program in standard c/c++??
11th Jun 2018, 10:59 AM
Abhishek Kumar
0
Abhishek Yes you can, because it's the only function that's available in C/C++. By the way conio.n is not standard, but you can still create the program If you want to do that(holding screen) in c++ way then you can just cin >> a; but we have to input something. So use cin.ignore() once, if doesn't work use twice and it'll work just fine. if you wanna use getch for Password typing in c++ add #include <conio.h>
11th Jun 2018, 11:08 AM
Owenizedd
Owenizedd - avatar