I'm a beginner making a program in Eclipse IDE (C++) and I've got a problem with a small piece of my code. The problem is shown below:
char dataArray[100];
cout<<"Please enter a data set string: ";
cin.getline(dataArray,100,'\n');
cout<<dataArray;
When I run it the program gets stuck at the getline command, whatever the input, it just lets me input characters indefinitely without continuing.
You are reading data from input stream or from dataArray ? Standard format of getline is
istream& getline (istream& is, string& str, char delim);
Where is is istream object from which characters are extracted
std::string data;
std::getline (std::cin,data);
try this.
Related
I know I know.
This question has been asked before, but I've looked at all the answers and none seem to solve my problem. When I use the getline function to get the contents of a line in the file, it doesn't work.
getline(file, line);
'File' is declared here:
ifstream File;
File.open("fruit.txt");
and 'line' is declared here:
int line = 0;
Getline is underlined in red with this message:
getline
no instance of overloaded function "getline" matches the argument list
argument types are :(std::ifstream, int)
What this means is no instance of getline has the argument list of the file stream and an integer.
This makes no sense as all the other questions on this matter state exactly that, that the arguments are the file stream and an integer.
What am I doing wrong?
EDIT:
Here is the full code:
ifstream fruitFile;
fruitFile.open("fruit.txt");
int line = 0;
int C_FRUIT = getline(fruitFile, line);
fruitFile.close();
The first line should be a number, and I need it.
getline() will read one line of text. It can't read directly an int. This is why you get your error message.
You have to be aware that there are two getline(). There is one which is istream::getline() and std::getline(). Both have different signatures. The first is a member function of a stream and is defined in the stream header; the latter is defined in the <string> header.
But pay attention: the return value of std::getline() is not an int ! It's a stream reference. This is why you get a second compiler error.
Finally if you want to read an integer x, it's easier to use extractors:
int value;
fruitFile >> value;
fruitFile.ignore(SIZE_MAX, '\n'); // in case you'd need to go to next line
Or if you really want to read an int in a full line:
string line;
getline(fruitFile, line);
stringstream sst(line); // creates a string stream: a stream that takes line as input
sst >> value;
The second argument of getline needs to be a string: http://www.cplusplus.com/reference/string/string/getline/
I think what you try to achieve is:
ifstream fruitFile;
fruitFile.open("fruit.txt");
int line = 0;
fruitFile >> line
fruitFile.close();
I faced the same error. add this to your code to solve the problem
Add the string library
include <string>
Add the below function call, where string_variable should be of type string.
std::getline(cin, sting_variable)
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);
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().
Basically I have a thread reading from c++ std::istream using
istream& getline ( istream& is, string& str );
And when I bind the istream with cin, then it works fine when I type the input from standard command line as the input.
But I am wondering is there a way that I can make the read thread getline get the string without
using the actual command line ?
Basically I want to achieve this:
By just calling a function WriteToIStream with parameter str (instead of type str in command line) and the read thread getilne() can get the str.
void WriteToIStream(string& str)
{
//...
}
Many Thanks
cin is only one instance of an input stream, bound to the standard input. For your case, you can use an istringstream (or more generally a stringstream) which is an input stream to read from a string:
std::istringstream istr("foobar");
getline(istr, some_string_variable);
This requires the standard header <sstream>.
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
}