
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Read a character from standard input without waiting for a newline in C++
A portable solution doesn't exist for doing this. On windows, you can use the getch() function from the conio(Console I/O) library to get characters pressed.
example
#include<iostream> #include<conio.h> using namespace std; int main() { char c; while(1){ // infinite loop c = getch(); cout << c; } }
This will output whatever character you input to the terminal. Note that this will only work on windows as the conio library exists only on windows. On UNIX, you can achieve this by entering in system raw mode.
example
#include<iostream> #include<stdio.h> int main() { char c; // Set the terminal to raw mode system("stty raw"); while(1) { c = getchar(); // terminate when "." is pressed if(c == '.') { system("stty cooked"); exit(0); } std::cout << c << " was pressed."<< std::endl; } }
- Related Articles
- How can we read from standard input in Java?
- Haskell program to read numbers from standard input
- Java Program to Read The Number From Standard Input
- Kotlin Program to Read The Number From Standard Input
- How to Read The Number From Standard Input in Swift Program?
- With JavaScript RegExp find a character except newline?
- Way to read input from console in C#
- Standard Input Stream (cin) in C++
- How to print without newline in Python?
- Standard Size of character ('a') in C/C++ on Linux
- Ways to read input from console in Java
- Python program to read input from console
- How to Remove Newline Characters from a text File?
- Search for a character from a given position in Java
- How to read an input value from a JTextField and add to a JList in Java?

Advertisements