title: C++ Void Function with File Stream Error
body: <p>I am trying to write a code that takes an input file and organizes it and computes some values. However, I am having a problem with my getName function that is supposed to assign a first and last name to call by reference parameters by using the get member function to sum up characters in the input file if a -1 is read. Here are the error arguments for the code:</p>
  
  <pre><code>main.cpp:7:14: error: variable or field 'getName' declared void
  main.cpp:7:14: error: 'ifstream' was not declared in this scope
  main.cpp:7:24: error: 'shoppingCartInput' was not declared in this scope
  main.cpp:7:43: error: 'string' was not declared in this scope
  main.cpp:7:51: error: 'firstname' was not declared in this scope
  main.cpp:7:62: error: 'string' was not declared in this scope
  main.cpp:7:70: error: 'lastname' was not declared in this scope
  main.cpp:102:50: error: 'getName' was not declared in this scope
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cctype&gt;
  
  void getName(ifstream&amp; shoppingCartInput, string&amp; firstname, string&amp; lastname);
  
  using namespace std;
  int main()
  {
      // declerations
      string infile_name = "";
      string shopper = "";
      string firstname ="";
      string lastname = "";
      int quantity;
      double price = 0.0, leastExpPrice = 0, mostExpPrice = 0;
      string item, leastExpItem, mostExpItem;
      double totalCost = 0.0;
      //1) Open Your File
      cout &lt;&lt; "please enter the name of the input file:";
      cin &gt;&gt; infile_name;
      cout &lt;&lt; "\n";
      ifstream shoppingCartInput;
      ofstream shoppingCartOutput;
      shoppingCartInput.open(infile_name.c_str());
      if(shoppingCartInput.fail( )) {
      cout &lt;&lt; "Input file opening failed.\n";
      exit(1);
      }
      shoppingCartOutput.open("output_file.dat");
      if(shoppingCartOutput.fail( )) {
          cout &lt;&lt; "Output file opening failed.\n";
          exit(1);
      }
      //2) Read the first line
      shoppingCartInput &gt;&gt; quantity &gt;&gt; price &gt;&gt; item;
      //printing the line to the console (properly organized)
      shoppingCartOutput.setf(ios::fixed);
      shoppingCartOutput.setf(ios::showpoint);
      shoppingCartOutput.setf(ios::left);
      shoppingCartOutput.width(15);
      shoppingCartOutput &lt;&lt; item &lt;&lt; " ";
      shoppingCartOutput.setf(ios::right);
      shoppingCartOutput.width(3);
      shoppingCartOutput &lt;&lt; quantity &lt;&lt; " ";
      shoppingCartOutput.setf(ios::right);
      shoppingCartOutput.width(6);
      shoppingCartOutput.precision(2);
      shoppingCartOutput &lt;&lt; price &lt;&lt; " ";
      shoppingCartOutput.setf(ios::right);
      shoppingCartOutput.width(7);
      shoppingCartOutput.precision(2);
      shoppingCartOutput &lt;&lt; (quantity * price) &lt;&lt; endl;
      //set it as the most expensive
      //set it as the least expensive
      leastExpPrice = price;
      leastExpItem = item;
      mostExpPrice = price;
      mostExpItem=item;
      //initialize total cost to get the cost of the first line of data
      totalCost = (quantity * price);
      //3) Read until the end of the file
      while(!(shoppingCartInput.eof())) {
          while(quantity != -1) {
              shoppingCartInput &gt;&gt; quantity &gt;&gt; price &gt;&gt; item;
              //printing the line to the console (properly organized)
              shoppingCartOutput.setf(ios::fixed);
              shoppingCartOutput.unsetf(ios::right);
              shoppingCartOutput.setf(ios::showpoint);
              shoppingCartOutput.setf(ios::left);
              shoppingCartOutput.width(15);
              shoppingCartOutput &lt;&lt; item &lt;&lt; " ";
              shoppingCartOutput.setf(ios::right);
              shoppingCartOutput.width(3);
              shoppingCartOutput &lt;&lt; quantity &lt;&lt; " ";
              shoppingCartOutput.setf(ios::right);
              shoppingCartOutput.width(6);
              shoppingCartOutput.precision(2);
              shoppingCartOutput &lt;&lt; price &lt;&lt; " ";
              shoppingCartOutput.setf(ios::right);
              shoppingCartOutput.width(7);
              shoppingCartOutput.precision(2);
              shoppingCartOutput &lt;&lt; (quantity * price) &lt;&lt; endl;
              if(price &lt; leastExpPrice) {
                  leastExpPrice = price;
                  leastExpItem = item;
              }
              if(price &gt; mostExpPrice) {
                  mostExpPrice = price;
                  mostExpItem=item;
              }
              totalCost += (quantity * price);
          }
          if (quantity == (-1)) {
              string first = "";
              string last = "";
              getName(shoppingCartInput, firstname, lastname);
              shopper = firstname + " " + lastname;
              shoppingCartOutput &lt;&lt; "\ncheapest item = " &lt;&lt; leastExpItem &lt;&lt;
                          endl;
              shoppingCartOutput &lt;&lt; "most expensive item = " &lt;&lt; mostExpIte &lt;&lt;
                          endl;
              shoppingCartOutput &lt;&lt; "total cost = " &lt;&lt; totalCost &lt;&lt; endl;
              shoppingCartOutput.unsetf(ios::right);
              shoppingCartOutput.setf(ios::left);
              shoppingCartOutput &lt;&lt; "shopper = " &lt;&lt; shopper;
          }
      }
      shoppingCartInput.close();
      shoppingCartOutput.close();
      return 0;
  }
  
  void getName(ifstream&amp; shoppingCartInput, std::string&amp; firstname, std::string&amp;
  lastname)
  {
      char letter;
      do {
           shoppingCartInput.get(letter);
           firstname += letter - 1;
      } while (letter != ',');
  
      do {
          shoppingCartInput.get(letter);
          lastname += letter;
      } while (letter != '\n' || letter != 'r');
  }
  </code></pre>