'How to fix arguments and operators error, and can i outfile vector?
i'm new to all this coding stuff. So i'm trying to comparing some accounts names and these errors poped up: no instance of overloaded function getline matches the argument list, and no operator"<<" matches the operands. Please explain, thanks a lot.
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using namespace std;
int main()
{
ifstream inFile("test.txt");
if (!inFile)
{
cout << "Cannot open file.";
exit(1);
}
string input;
vector<string> tennguoikhac;
vector<string> tennguoidung;
cin >> input;
tennguoidung.push_back(input);
while (inFile)
{
inFile.getline(tennguoikhac);
if (tennguoikhac == tennguoidung)
{
cout << "ten da co nguoi dung" << endl;
}
}
ofstream outFile("test.txt");
if (!outFile)
{
cout << "Cannot open file.";
exit(1);
}
outFile << tennguoidung << endl;
outFile.close();
inFile.close();
return 0;
}
Solution 1:[1]
You cannot do getline into vector. What you want is:
std::string tennguoikhacString;
std::getline(std::cin, tennguoikhacString);
std::istringstream tennguoikhacStream(tennguoikhacString);
std::copy(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>(), std::back_inserter<>(tennguoikhac));
You also cannot directly print the vector:
outFile << tennguoidung << endl;
The closest you may want:
std::copy(tennguoidung.cbegin(), tennguoidung.cend(), std::ostream_iterator<std::string>(std::cout, " "));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
