So im trying to write a program that reads the contents of two files into two separate vectors (GirlNames.txt and BoyNames.txt). Basically, I want the user to enter a boy name and a girl name and the code will judge whether or not those names are popular. I don't really understand what the error means or what to do. I was told it might be my compiler but I'm not sure. (I use Dev-C++) I tried Visual Studios but I don't understand how to use it and I think online compilers just run the code without searching through my folders for the txt files.
This is what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
vector<string> readNames(string fileName) {
vector<string> names;
string line;
ifstream f2(fileName); // This is the line with the error
while (getline (f2, line)) {
names.push_back(line);
}
f2.close();
return names;
}
int isNamePresent(vector<string> names, string nameToCompare) {
int result = 0;
for(int i = 0; i < names.size(); i++) {
if(names[i] == nameToCompare) {
result = 1;
break;
}
}
return result;
}
void displayResult(vector<string> names, string nameToCompare, string label) {
if(nameToCompare != "N") {
if(isNamePresent(names, nameToCompare) == 1)
cout << nameToCompare << " is one of the most popular " << label << "'s names." << endl;
else
cout << nameToCompare << " is not one of the most popular " << label << "'s names." << endl;
}
}
int main()
{
string fileNameBoys = "BoyNames.txt";
string fileNameGirls = "GirlNames.txt";
vector<string> boyNames = readNames(fileNameBoys);
vector<string> girlNames = readNames(fileNameGirls);
string userInputBoyName, userInputGirlName;
cout << "Enter a boy's name, or N if you do not wish to enter a boy's name: ";
cin >> userInputBoyName;
cout << "Enter a girl's name, or N if you do not wish to enter a girl's name: ";
cin >> userInputGirlName;
displayResult(boyNames, userInputBoyName, "boy");
displayResult(girlNames, userInputGirlName, "girl");
return 0;
}
|
This is the error I'm getting along with some other things:
(In function)
[Error] no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::string&)'
[Note] candidates are:
(In file)
[Note] std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
[Note] no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'
[Note] std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char; _Traits = std::char_traits<char>]
[Note] candidate expects 0 arguments, 1 provided
[Note] std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)
[Note] no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const std::basic_ifstream<char>&'