Passing ifstream to a function help
Apr 19, 2013 at 11:27pm UTC
I am trying to do a couple things that I am not having any luck with.
1] Do i have to send ifstream to the function for the function to read data from that file? If so, what is the correct syntax?
2] I am trying to use a while(infile) loop to count how many instances of first name, Last name, and number there are. Is there a better way to do that then the way I am trying to do it? (remember, I am a beginner so no super advanced stuff please)
Sample Data:
{
Duckey Donald 85
Goof Goofy 89
Brave Balto 93
Snow White 93
}
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
ifstream infile;
int fileCount(ifstream&);
int main()//================================================== MAIN ===================
{
infile.open("Ch9_Ex2Data.txt" );
if (!infile)
{
cout << "Cannot open the input file. Program terminates!" << endl;
return 1;
}
cout << fileCount(infile);
system("PAUSE" );
return (0);
}
int fileCount(ifstream&)
{
int counter = 0;
int discard;
string discard1;
while (infile)
{
cin >> discard1;
cout << discard1<<endl;
cin >> discard1;
cout << discard1<<endl;
cin >> discard;
cout << discard<<endl;
counter++;
}
cout <<endl;
cout <<endl;
cout <<endl;
return (counter - 1);
}
Apr 19, 2013 at 11:46pm UTC
line 28:
int fileCount(istream &infile);
Line 34: (Preferably) use while
Then I noticed you didn't need any processing on the data in
infile , so you
may want to replace lines 36 through 41 with
then just leave your
count++
Apr 19, 2013 at 11:54pm UTC
@Matri X
Line 34: (Preferably) use while
(!infile.eof())
It is
while (infile) that is the preferable method not
while (!infile.eof())
You are always trying to read in a variable of type int while records of the file contain data as string literal: first name and last name.
Last edited on Apr 19, 2013 at 11:55pm UTC
Apr 20, 2013 at 12:25am UTC
GREAT! It works.. I finally figured it out. I have been using cin >> to read the files insted of ifile >> . Thank you for your help though.
The working code is as follows
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// declare functions
// void function to open the input and output files
void openFiles (ifstream& infile, ofstream& output);
int fileCount(ifstream &infile);
int main()//================================================== MAIN ===================
{
ifstream infile;
ofstream outfile;
int a = 0;
openFiles (infile, outfile);
a = fileCount(infile);
system("PAUSE" );
return (0);
}
void openFiles (ifstream& infile, ofstream& outfile)//=======================
{
infile.open("Ch9_Ex2Data.txt" );
if (!infile)
{
cout << "Cannot open the input file. Program terminates!" << endl;
//return 1;
}
cout << "this is the open files function" << endl;
}
int fileCount(ifstream &infile)
{
int counter = 0;
int discard;
string discard1;
while (infile)
{
infile >> discard1; //cin.getline(discardl);
cout << discard1<<endl;
infile >> discard1; //cin.getline(discardl);
cout << discard1<<endl;
infile >> discard; //cin.getline(discardl);
cout << discard<<endl;
counter++;
}
return (counter - 1);
}
Last edited on Apr 20, 2013 at 3:33am UTC
Topic archived. No new replies allowed.