myfile.getline problems

Hello all,

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

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
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;           
int RecordCount (const char * filename);      
const int 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;
    static char 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( const char * 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;
}


Any thoughts?
Last edited on
Wow never mind I figured it out. i had to change Recordcount(filename) to rcount. Only took me 4 hours to figure that out!
Last edited on
Topic archived. No new replies allowed.