Trying to use getline but the error keeps saying:
No instance of overloaded function matches argument list.
#include <iomanip>
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int lengthInput;
int widthInput;
ifstream fileOpening;
fileOpening.open("testFile.txt");
while (!fileOpening.eof())
{
//ERROR ON NEXT TWO LINES OF CODE**
getline(fileOpening, lengthInput, ' ');
getline(fileOpening, widthInput, ' ');
}
system("pause");
return 0;
The 2nd parameter of std::getline() expects a std::string to write to, but in both cases you are passing in an int instead. That is why you are getting the error - there really is no version of std::getline() that matches your code.
The second argument of getline is expected to be a reference to a std::string, not a reference to an int.
If you expect that the pair of values can be read from multiple lines, you can use:
while (fileOpening >> lengthInput >> widthInput)
{
// Got the input. Use them.
}
If you expect that the pair of values must be read from each line, you'll have to use a different strategy.
Read lines of text.
Process each line.
std::string line;
while ( fileOpening >> line )
{
std::istringstream str(line);
if (str >> lengthInput >> widthInput)
{
// Got the input. Use them.
}
}
Important Note
Don't use
while (!fileOpening.eof()) { ... }
See Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?.
Related
Lets say I have a code :
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string line,wantedString,newString;
fstream subor("test.txt");
while (!subor.fail()) // read line from test.txt
{
getline(subor,line);
line.substr(line.find("?")+1);
wantedString=line.substr(line.find("?")+1); // will take everything after '?' character till
'\n'
}
cout<<"Enter new text to replace wantedString :";
getline(cin,newString);
// how to use string::replace() please ?
/I tried this but does not work
getline(subor,line);
line.replace(wantedString,string::npos,newString);
return 0;
}
In test.txt is written only one line :
something?replace
note: there is no '\n' in the file
error thrown by compiler is :
error: no matching function for call to 'std::__cxx11::basic_string<char>::replace(std::__cxx11::string&, const size_type&, std::__cxx11::string&)'
can you please answer working code with commented explaining why is it like you did it ?
I have studied string::replace() method here:
http://www.cplusplus.com/reference/string/string/replace/
Is my logic of using string::find() as a starting point for string to be replaced ?
Is my logic of using string::find() as a starting point for string to be replaced ?
Yes, but then you threw away the iterator/index result of find and went to get the substring instead.
Replace doesn't take a string.
It takes an iterator/index.
So just pass what you got from find, into replace. (Be careful of edge cases! Check for errors! Read the documentation for both functions.)
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
fstream subor("test.txt");
string line;
while (getline(subor,line))
{
// Find the index of the character after the first '?'
const size_t wantedStringPos = line.find("?")+1;
// Prompt for a replacement string
cout << "Enter new text to replace wantedString: ";
string newString;
getline(cin,newString);
// Perform the replacement
line.replace(wantedStringPos, string::npos, newString);
// Now do something with `line`
// [TODO]
}
}
(I've also fixed an off-by-one error in your loop.)
You then need to actually write the new, modified string back to the file: the file doesn't automatically get updated in sync with the copy of the data you previously read out from it.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line,line2;
char dude[20];
cin.getline(dude,20);
fstream myfile ("example.txt",ios::in);
if(!myfile)
{
cout<<"Not Found! ";
system("pause");
}
while (!myfile.eof())
{
getline(myfile,line);
cout<<line;
}
myfile.close();
exit(0);
}
This works but if i do this:
int main () {
string line,line2,dude;
It gives me an error.
[Error] no matching function for call to 'std::basic_istream::getline(std::string&, int)'
WHY?
std::cin.getline() expects a char* buffer (docs)
std::getline() expects a std::string buffer (docs)
When you change the type, you have to change which function you call, too.
char buf[20] is not a string, it is a character array. If you terminate the array with a '\0' byte, then it can be said to be a c-string. Still not a std::string, though.
The function cin.getline() expects two parameters: a pointer to an array of characters and a count of how many characters the buffer supports - it then populates it with a c-string from cin.
There is no variant of cin.getline() which supports a std::string. For that, you need to use std::getline(iostream, string), e.g.
std::string line;
std::getline(std::cin, line);
If I have a string containing unknown number of words, and I have to scan it in multiple strings in C++. How can I do it?
For eg:
"I am a boy". I want, each of these individual words to be in a string.
"My name is John Lui". Each of these as well.
One way that I could think of was to use, getline in c++ and then parse through the entire string until a character is found and store in seperate strings. I want to know is there a better method? Thanks!
Also, I want to know, that when using a delimiter in getline command, getline basically scans the input strings till the point delimiter is not found and puts that part of a string into a new string. However, I want to know, if the delimiter is not present at all, then what happens? Does it throw an exception or it takes input the whole string till the newline character? Thanks!
However you could use std::getline
Which uses a string instead of a char array. It's easier to use string
since they know their sizes, they auto grow etc. and you don't have to
worry about the null terminating character and so on. Also it is
possible to convert a char array to a string by using the appropriate
string contructor.
You can do it by stringstream:
// stringstream::str
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream, std::stringbuf
using namespace std;
int main ()
{
std::string str;
getline( std::cin, str );
std::stringstream ss;
ss<<str;
std::string s;
while(ss>>s)
{
std::cout << s << '\n';
}
return 0;
}
Input: I am a boy
Output:
I
am
a
boy
If you think that, you want each word to store in a vector, you can do it like:
// stringstream::str
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream, std::stringbuf
#include <vector>
using namespace std;
int main ()
{
vector <string> V;
V.clear();
std::string str;
getline( std::cin, str );
std::stringstream ss;
ss<<str;
std::string s,s1;
while(ss>>s)
{
V.push_back(s);
}
return 0;
}
this is what I have done till now: I want to read words from file in C++ and I am allowed to use only cstring library. this is my piece of code
#include <cstring>
#include <fstream>
#include <stdio.h>
using namespace std;
int main(){
ifstream file;
char word[1];
file.open("p.txt");
while (!file.eof()){
file >> word;
cout << word << endl;
}
system("pause");
return 0;
}
It is working fine and reading one word at a time. But I don't understand how this is working fine.
How can char array of any size be it char word[1] or char word[50] read only one word at a time ignoring spaces.
And further I want to store these words in dynamic array. How can I achieve this? Any guidance would be appreciated?
Your code has undefined behaviour. operator >> simply overwrites memory beyond the array.
Take into account that included by you header <stdio.h> is not used in the program. On the other hand you need to include header <cstdlib> that declares function system.
As for your second question then you should use for example standard container std::vector<std::string>
For example
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
int main()
{
std::ifstream file("p.txt");
std::string s;
std::vector<std::string> v;
v.reserve( 100 );
while ( file >> s ) v.push_back( s );
std::system( "pause" );
return 0;
}
Or you can simply define the vector as
std::vector<std::string> v( ( std::istream_iterator<std::string>( file ) ),
std::istream_iterator<std::string>() );
provided that you will include header <iterator>
For example
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <cstdlib>
int main()
{
std::ifstream file("p.txt");
std::vector<std::string> v( ( std::istream_iterator<std::string>( file ) ),
std::istream_iterator<std::string>() );
for ( const std::string &s : v ) std::cout << s << std::endl;
std::system( "pause" );
return 0;
}
Your code is invoking undefined behavior. That it doesn't crash is a roll of the dice, but its execution is not deterministic precisely because that is the nature of being undefined.
The easiest way (I've found) to load a file of words with whitespace separation is by:
std::ifstream inp("p.txt");
std::istream_iterator<std::string> inp_it(inp), inp_eof;
std::vector<std::string> strs(inp_it, inp_eof);
strs will contain every whitespace delimited char sequence as a linear vector of std::string. Use std::string for dynamic string content and don't feel the least bit guilty about exploiting the hell out of the hard work those that came before you gave us all: The Standard Library.
Your code is failing due to the overload of char * for operator>>.
An array of char, regardless the size, will decompose to the type char * where the value is the address of the start of the array.
For compatibility with the C language, the overloaded operator>>(char *) has been implemented to read one or more characters until a terminating whitespace character is reached, or there is an error with the stream.
If you declare an array of 1 character and read from a file containing "California", the function will put 'C' into the first location of the array and keep writing the remaining characters to the next locations in memory (regardless of what data type they are). This is known as a buffer overflow.
A much safer method is to read into a std::string or if you only want one character, use a char variable. Look in your favorite C++ reference for the getline methods. There is an overload for reading until a given delimiter is reached.
You only need a couple changes:
#include <cstring>
#include <fstream>
#include <stdio.h>
#include <string>
int main(){
ifstream file;
string word;
file.open("p.txt");
while (file >> word){
cout << word << endl;
}
system("pause");
return 0;
}
It works because you are lucky and you don't overwrite some critical memory. You need to allocate enough bytes for char word array, say char word[64]. And use while(file>>word) as your test for EOF. In the loop you can push_back the word into a std::vector<string> if you are allowed to use C++ STL.
If you want a simple C++11 STL-like solution, use this
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream fin("./in.txt"); // input file
vector<string> words; // store the words in a vector
copy(istream_iterator<string>(fin),{}, back_inserter(words)); // insert the words
for(auto &elem: words)
cout << elem << endl; // display them
}
Or, more compactly, construct the container directly from the stream iterator like
vector<string> words(istream_iterator<string>(fin),{});
and remove the copy statement.
If instead a vector<string> you use a multiset<string> (#include <set>) and change
copy(istream_iterator<string>(fin),{}, back_inserter(words)); // insert the words
to
copy(istream_iterator<string>(fin),{}, inserter(words, words.begin())); // insert the words
you get the words ordered. So using STL is the cleanest approach in my opinion.
You're using C++, so you can avoid all that C stuff.
std::string word;
std::vector<std::string> words;
std::fstream stream("wordlist");
// this assumes one word (or phrase, with spaces, etc) per line...
while (std::getline(stream, word))
words.push_back(word);
or for multiple words (or phrases, with spaces, etc) per line separated by commas:
while (std::getline(stream, word, ','))
words.push_back(word);
or for multiple words per line separated by spaces:
while(stream >> word)
words.push_back(word);
No need to worry about buffer sizes or memory allocation or anything like that.
file>>char *
Will work with any char * and you are using
file >> word;
and it simply sees work variable as a char * but you are getting a segemntation fault somewhere and if your code grows you will see something is not working without any logical reason. GDB debugger will show you the seg fault
This is my first C++ program. It prints the number of words in the input.
My first question, how does it go into the loop and add to the count? is it every time i type the space character? if so, how does it know I'm trying to count words?
using namespace std;
int main() {
int count;
string s;
count = 0;
while (cin >> s)
count++;
cout << count << '\n';
return 0;
}
My second question. Can someone explain to me what namespace std means for a begineer?
When you do cin >> string. You will read a word and put it in the string. Yes, it will read char by char until reach the delimiter.
Std means Standard. Standard C++ library is inside the std namespace. You can rewrite or code without the using namespace std:
int main() {
int count;
std::string s;
count = 0;
while (std::cin >> s)
count++;
std::cout << count << '\n';
return 0;
}
I discourage that novices use the using namespace std statement because it is harder to understand what is going on.
Cin will capture input until a space, yes. The specific style of loop you have will go until an End-Of-File (EOF) is found or until bad input is provided. That loop doesn't look like common C++ practice to me, but it's described here.
2.namespace std is how you tell the compiler where to look to find the objects you're referencing in your code. Because different objects are "inside" different namespaces, you either have to tell the compiler where they are specifically (aka std::cin) or tell it for convenience where an object you use will be in the future (with using namespace std).
In your code, cin >> s attempts to read a std::string from input stream. If the attempt succeeds, then the returned value of cin >> s implicitly converts into true and the while loop continues, incrementing the counter. Otherwise, the while loop exits when the attempt fails, as there is no more data to read from the input stream.
You can use std::distance to count the words, as shown below:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
int main() {
std::istream_iterator<std::string> begin(std::cin), end;
size_t count = std::distance(begin, end);
std::cout << count << std::endl;
return 0;
}
Demo : http://www.ideone.com/Hldz3
In this code, you create two iterators begin and end, passing both to std::distance function. The function calculates the distance between begin and end. The distance is nothing but the number of strings in the input stream, because the iterator begin iterates over strings coming from the input stream, and end defines the end of the iterator where begin stops iterating. The reason why begin iterates over strings is because the template argument to std::istream_iterator is std::string:
std::istream_iterator<std::string> begin(std::cin), end;
//^^^^^^^^^^^
If you change this to char, then begin will iterator over char, which means the following program will count the number of characters in the input stream:
#include <iostream>
#include <algorithm>
#include <iterator>
int main() {
std::istream_iterator<char> begin(std::cin), end;
size_t count = std::distance(begin, end);
std::cout << count << std::endl;
return 0;
}
Demo : http://www.ideone.com/NH52y
Similarly, you can do many cool things if you start using iterators from <iterator> header and generic functions from <algorithm> header.
For example, let say we want to count the number of lines in the input stream. So what change would we make to the above program to get the job done? The way we change std::string to char when we wanted to count characters, immediately suggests that now we need to change it to line so that we could iterate over line (instead of char).
As no line class exist in the Standard library, we've to define one ourselves, but the interesting thing is that we can keep it empty as shown below, with full working code:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
struct line {}; //Interesting part!
std::istream& operator >>(std::istream & in, line &)
{
std::string s;
return std::getline(in, s);
}
int main() {
std::istream_iterator<line> begin(std::cin), end;
size_t count = std::distance(begin, end);
std::cout << count << std::endl;
return 0;
}
Yes, along with line, you've to define operator>> for line as well. It is used by std::istream_terator<line> class.
Demo : http://www.ideone.com/iKPA6