How to get the next character from input sequence without extracting it in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <cctype>

using std::cout;
using std::endl;

int main()
{
	std::cout << "Enter a word: ";
	std::cout.flush();    

	std::cin >> std::ws;  
	int ch = std::cin.peek();  

	if (ch == EOF) return 1;

	cout << (char)ch << endl;
		
	std::string s;
	std::cin >> s;
	cout << s << endl;

	return 0;
}

/*
run:

Enter a word: c++
c
c++

*/

 



answered Jun 19, 2018 by avibootz
...