How to provide your own delimiter for cin? - c++

In c, I can use newline delimeter ([^\n]) with scanf. Using which I can store the line. Similarly for cin, I can use getline.
If I have to store a paragraph, I can simulate the functionality using my own special char delimiter like [^#] or [^\t] with scanf function in c.
char a[30];
scanf("%[^\#]",a);
printf("%s",a);
How to achieve the similar functionality with cin object in cpp.

istream.getline lets you specify a deliminator to use instead of the default '\n':
cin.getline (char* s, streamsize n, char delim );
or the safer and easier way is to use std::getline. With this method you don't have to worry about allocating a buffer large enough to fit your text.
string s;
getline(cin, s, '\t');
EDIT:
Just as a side note since it sounds like you are just learning c++ the proper way to read multiple deliminated lines is:
string s;
while(getline(cin, s, '\t')){
// Do something with the line
}

Related

How can I write into a string like I write into a char[]

For example, if I have this function:
void read(std::string& s)
{
std::cin >> s;
}
and the input
first sentence
s will contain 'first' only.
If I would've used something like:
void read(char array[100])
{
std::cin.get(array, 100);
}
for the same input, I would've got "first sentence".
Is there any way to achieve the same effect using std::string?
This might happen (I think) because cin.get with char[] takes all the characters, even blank spaces, because it ends when it detects an end-of-line character, but cin with string stops when it encounters a space character. (just like cin>> does when being used with char[]).
So, I think that I need a cin.get() which would work for std::string.
I'm thinking about reading into a char array and then using the char* constructor of the string in order to convert the char array into a string, but I think there might be a better way to do it.
You can use std::getline().
void read(std::string& s)
{
std::getline(std::cin, s);
}
You can use getline to input the whole string std::getline(std::cin, s);
using namespace std;
void read(string& s) {
getline(cin, s);
}
Use above code for reading whole line instead of first word.

Difference between istream::get(char&) and operator>> (char&)

My question seems to be the same as this one, but I didn't find an answer since the original question seems to ask something more specific.
In C++98, what is the difference between
char c;
cin.get(c);
and
char c;
cin >> c;
?
I've checked the cplusplus reference for get and operator>>, and they look the same to me.
I've tried above code and they seem to behave the same when I input a char.
The difference depends on when there is a whitespace character on the stream buffer.
Consider the input ' foo'
char c;
cin.get(c);
Will store ' ' in c
However
char c;
cin >> c;
Will skip the whitespace and store 'f' in c
In addition to what's already been said, std::istream::get() is also an unformatted input function so the gcount() of the stream is affected, unlike the formatted extractor. Most of the overloads of get() and getline() have mostly been made obselete by the introduction of std::string, its stream extractors, and std::getline(). I'd say to use std::istream::get() whenever you need a single, unformatted character straight from the buffer (by using its single or zero argument overload). It's certainly quicker than turning off the skipping of whitespace first before using the formatted extractor. Also use std::string instead of raw character buffers and is >> str for formatted data or std::getline(is, str) for unformatted data.

Fastest way to input huge strings?

I have to read huge lines of strings from stdin so time is a critical issue. Strings are on consecutive lines and have no spaces so I can simply use while(cin>>str) { //code } but this is extremely slow. I have heard that scanf is much more faster than cin but if I use scanf("%s,str) I think that str is treated as char* and not a C++ string so I can't use the STL. I could take input as char* and copy all the chars into a C++ string but IMO that will also be slow.
Is there a way to get input using scanf or something but still get a C++ string as a result?
If you know the average or maximum size of the text, you create std::string with a pre-allocated size. One area occupying a lot of time is the memory (re) allocation by std::string.
cin >> str is the closest thing you'll find in STL to scanf("%s, str"). The only reason scanf would be faster than cin is because it would be giving you a char* instead of a string, and while you can create a new string from the char* by just passing them in to the string() constructor, that would be almost the same thing as using cin >> str.
You can use getline:
for (std::string line; getline(std::cin, line); ) {
do_something_with(line);
}
I don't know if it is any faster than cin >> line, but it might be, since it doesn't need to deal with whitespace other than newlines. But I don't believe this is as significant as the overhead of sentry construction.

Reading an input with space in c++

currently I am doing to read input with spaces in it.
int main() {
char str[100];
string st;
cin.getline(str,100);
st=str;
}
I want to utilize the functions that come along with the string, so I am reading the input into a string. Is there any other way to read the input directly into the string which also allow space.
If you're going to use std::string objects, just use std::getline.
std::string st;
std::getline(std::cin, st);
using gets () function.
It accepts even space as input.
Eg. )
gets (variable_name);

cin doesn't take input as ENTER

char ch[4];
char* ptr;
ptr = ch;
while(1)
{
cin >> *ptr;
if(*ptr == '\n')
break;
ptr++;
}
Here I just wrote a bit of sample code where I am trying to get out of a while loop when user writes ENTER but it's not working. Please help me. Thank you in advance.
To get a single character, use std::istream::get. This should work for getting newlines as well.
But instead of getting characters in a loop until you get a newline, why not just use something like std::getline:
std::string str;
std::getline(cin, str);
Or if you only want to get max three characters you can use std::istream::getline:
char ch[4];
cin.getline(ch, 4, '\n');
You are reading input into the value of a character. That's what *ptr means. I think you want just plain ptr, which is a pointer to an array of characters, which is something that is meant to receive data. What you wrote is basically this:
char c;
cin >> c;
I don't think that's what you meant, nor would it work even if it were, since as Joachim Pileborg points out above, the >> operator skips whitespace like newlines. In general, it is always best to be very robust when it comes to reading input. Provide adequate space, and either use a variable that can grow automatically (like std::string) or tell the system how much space you have (like fgets()).
The following will read a line:
istream& getline (char* s, streamsize n );
The extraction operator would skip leading white-spaces and stop execution on encountering any subsequent white-space. So, when you want to do something like this, use std::istream::get() or std::istream::getline().