my question is a little bit strange, but how is it possible to read some string from keyboard char by char without using scanf() and getchar() only by using operator<<, for example I want to change every letter a which I read by * thanks in advance
views:
81answers:
4
A:
Try writing out to a type of char.
char cReadFromStream;
stream >> cReadFromStream;
sheepsimulator
2010-09-24 15:10:48
+2
A:
char c;
std::string output;
while(std::cin >> c)
{
if(c == 'a')
c = '*';
output += c;
}
Wade Tandy
2010-09-24 15:14:49
The `push_back()` method can be replaced by concatenation: `output += c;`. This may be more natural since it is an append.
Thomas Matthews
2010-09-24 16:53:42
Good call. I've been stuck in C land for too long.
Wade Tandy
2010-09-24 20:30:02
A:
You cannot. operator << uses cooked inputs.
However, if you really meant you are looking for a solution using C++ iostreams and not C stdio functions, then use cin.get() which is the C++ iostreams equivalent to getchar.
Ben Voigt
2010-09-24 15:19:32
A:
Depending on your purposes, you may find it adequate to do this:
#include <iostream>
#include <iomanip>
char c;
std::cin >> std::noskipws;
while (std::cin >> c)
std::cout << c == 'a' ? '*' : c;
See http://www.cplusplus.com/reference/iostream/manipulators/noskipws/ for further details of noskipws, which is crucial as otherwise operator>> will skip over spaces, tabs and newlines until it finds other characters to put in 'c', resulting in all the aforementioned whitespace characters being removed from your output.