I'm having some trouble with a loop in my program. The point of this program is to read in a file that will have a first name, last name, and a number all on the same line. My code reads in the amount of line correctly and print how many lines there actually are but when I try to do a cout inside the loop, it is printing everything twice instead of just once. Here is my code
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
usingnamespace std;
int RecordCount (constchar * filename);
constint MAXFILENAMESIZE = 80;
char * GetName();
int main(void)
{
char * filename; // makes a filename pointer
int rcount =0;
filename = GetName(); // sets filename equal to what ever is returned by the GetName function
rcount = RecordCount(filename);
cout << endl << "The amount of people who took this survey was " << RecordCount(filename) << endl;
return 0;
}
char * GetName()
{
ifstream myfile;
staticchar fn [MAXFILENAMESIZE];
cout << "Please enter a file name : ";
cin >> fn;
myfile.open(fn);
while(!myfile)
{
cout << "Filename does no exist, please re-enter: ";
cin >> fn;
myfile.open(fn); // need this line to set up the loop
}
return fn;
}
int RecordCount( constchar * filename)
{
int count= 0;
char inputline [80];
ifstream myfile;
myfile.open(filename);
myfile.getline(inputline,80);
//***********************************************
//* THIS IS THE LOOP THAT IS GIVING ME TROUBLE *
//***********************************************
do
{
count++;
cout << inputline << endl;
myfile.getline(inputline ,80);
}
while (!myfile.eof());
{
myfile.close();
}
return count;
}