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);
Related
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.
just a quick question. I'm looking for the most efficient and clear way to get the user's input of any length and store it, so then I can retrieve it and compare to another input. I also need the user's input to be null-terminated. Can I write something like
string inp;
cin >> inp;
A std::string will manage itself and grow to accommodate the input that is given to the program. If you have
std::string input;
std::getline(std::cin, input);
This will get input from the user that includes spaces that can be as big as the input stream can hold. Now that you have the string if you need to pass it to some function that needs an old null terminated c-style string then you would use the c_str() function. c_str() does return a const char * so you will not be able to modify the string data with it.
If you really need a modifyable c-style string then you can make one with
char * old_style_string = new char[input.size() + 1];
std::strcpy(old_style_string, input.c_str());
#include <iostream>
#include <string>
using namespace std;
int main()
{
char addi[5];
string name;
string a="ADD:";
fgets(addi,5, stdin);
cin>>name;
addi[5]='\0';
cout<<"addi"<<addi<<endl;
i have a addi char array in which i wanna store "ADD:" and name string in which i wanna store the string to be added .the input will be in format ADD:wolf .why does addi not take the "ADD:" string?? OR alternatively how would i do it since i need to compare the ADD: with some string in further steps.
My guess is that you expect the fgets call to get the first four characters, and then use std::cin to get the remaining line. However it does not work like that. The fgets call gets the complete line, even it it only writes four characters to your buffer. Then the input with std::cin will wait for input that never comes.
Instead I suggest you read the complete line with std::getline, and then split the string at the colon to get the "key" and the "value".
Actually, since std::getline supports simple tokenization, you can use two calls to read the input:
std::string op;
std::string data;
std::getline(std::cin, op, ':');
std::getline(std::cin, data);
Now the string op will contain e.g. ADD (the string before the colon), and data will contain the string after the colon.
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.
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
}