-1

I am working on a code for my c++ class. The assignment is to read the names from 2 different txt files(already in my directory) and find if the string/name that the user searched for matches any of the names already in the files. My code seems good to me, but I am getting an error in my function prototype saying "string was not declared in this scope." Any solutions? My code is here as follows:

   #include <fstream>
   #include <string>
   #include <vector>
   void boysfunc(string&, string&);
   void girlsfunc(string&, string&);
   using namespace std;
   int main()
   {
    vector<string> boysnames;
    vector<string> girlsnames;
    string boysname, girlsname;
    ofstream outputFile;
    cout << "Enter a boy's name, or N if you do not want to 
   enter a name: ";
    cin >> boysname;
    cout << "Enter a girl's name, or N if you do not want to 
   enter a name: ";
    cin >> girlsname;
    if (boysname != "N")
    {
    boysfunc(boysname, boysnames);
    }
    if (girlsname != "N")
    {
    girlsfunc(girlsname, girlsnames);
    }
   }
   void boysfunc(string &boysname, string &boysnames)
   {
    outputFile.open("BoysNames.txt");
        while(outputFile >> boysnames)
    {
        /*Declare local variable count to use as a counter*/
            int count = 0;
            if (boysnames(count) == boysname)
            {
                outputFile.close();
                cout << "The name " << boysname << " is very 
   popular among boys.";
                return;
            }
            else
            {
            count++;
            }
        }
    }
   void girlsfunc(string &girlsname, string &girlsnames)
   {
    outputFile.open("GirlsNames.txt");
        while(outputFile >> girlsnames)
    {
        /*Declare local variable count to use as a counter*/
            int count = 0;
            if(girlsnames(count) == girlsname)
            {
                outputFile.close();
                cout << "The name " << boysname << " is very 
   popular among girls.";
                return;
                }
                else
                {
                    count++;
                }
    }
   }  

2 Answers2

1

There are two major errors that you need to fix here.

  • using namespace std; must be written before the use of strings if you wish to omit std:: before writing string. Otherwise, you can write std::string& in the function declarations.
  • boysfunc() and girlsfunc() are taking vector<string>& as the second argument, whereas you incorrectly mentioned string& in the functions' declaration and definition. Fix that.
Param Siddharth
  • 858
  • 2
  • 7
  • 20
0

In this snippet

string s = "hello";
using namespace std;

the type string is not known to the compiler. That's what using namespace std; does. It basically turns string into std::string.

You could swap the 2 lines above, and it will work, but I highly recommend just saying std::string explicitly everywhere. I'm sure your IDE will let you do this easily.

cigien
  • 57,834
  • 11
  • 73
  • 112