Creating Structures via fstream
<p>One of the exercises in C++ Primer Plus is having me use fstream to open a txt file and input data into a structure then output it. First line of the txt file is the number of "donors". The problem I seem to be having is that (I think) when I use "inFile >> value;" to retrieve the number and then allocate the structure via new, its expecting an int and its getting a string? Is this correct? What should I be doing differently?</p>

<pre><code>//ch6 p278 exercise #9
#include &lt;iostream&gt;
#include &lt;cstring&gt;
#include &lt;fstream&gt;
#include &lt;cstdlib&gt;
using namespace std;
const int SIZE = 60;
struct contributions
{
    char name[20];
    double dollars;
};

char filename[20];
string donors;
bool donated;

int main()
{
    char filename[SIZE];
    cout &lt;&lt; "Enter name of file to scan: ";
    cin &gt;&gt; filename;
    fstream inFile;
    inFile.open(filename);
    if(!inFile.is_open())
    {
        cout &lt;&lt; "Could not open the file " &lt;&lt; filename &lt;&lt; endl;
        cout &lt;&lt; "Program terminating.\n";
        exit(EXIT_FAILURE);
    }
    inFile &gt;&gt; donors;
    contributions * ptr = new contributions[donors];
    for(int h = 0; h &lt; donors; h++)
    {
        inFile &gt;&gt; ptr[h].name);
        inFile &gt;&gt; ptr[h].dollars);
    }
//BEGIN OUTPUT OF STRUCTURES
    cout &lt;&lt; "GRAND PATRONS:\n";
    for(int i = 0; i &lt; donors; i++)
   {
       if(ptr[i].dollars &gt;= 10000)
       {
           cout &lt;&lt; ptr[i].name &lt;&lt; " donated " &lt;&lt; ptr[i].dollars &lt;&lt; endl;
           donated = true;
       }
   }
   if (!donated) {cout &lt;&lt; "none.\n";}
   donated = false;
   cout &lt;&lt; "PATRONS:\n";
   for(int i=0; i &lt; donors; i++)
   {
       if(ptr[i].dollars &lt; 10000)
       {
           cout &lt;&lt; ptr[i].name &lt;&lt; " donated " &lt;&lt; ptr[i].dollars &lt;&lt; endl;
           donated = true;
       }
   }
   if (!donated) {cout &lt;&lt; "none.\n";}
   delete ptr;
    return 0;
}
</code></pre>
