Passing an fstream object to a function help.

Basically, I'm trying to right a program that reads a list of winning lottery numbers from a text file and outputs how many times each number won. Each line of the file contains a weeks winning numbers, 5 regular picks and 1 Powerball pick, hence 6 numbers to each line. I wrote a function to handle the input, but am having problems passing the fstream object to the function. I guess I could open and close the file each time the function is called, but that seems horribly inefficient as well as I'm not sure how to keep track of where you are in the file each time you read in this way. Anyway, here's 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
#include <iostream>
#include <fstream>
#include <cstdlib>

int get_num(std::fstream&);

int main()
{
    int number = 0;
    int picks[60]{};
    int pb[60];

    std::ifstream fin;
    fin.open("numbers.txt");
    if (!(fin.is_open()))
        exit(EXIT_FAILURE);

    do
    {
        for (int i = 0; i <= 5; i++)
        {
            number = get_num(fin);
            while (number > 0)
                picks[number]++;
        }

        number = get_num(fin);
        while (number > 0)
            pb[number]++;
    }
    while (number > 0);

    for (int i = 0; i < 60; i++)
    {
        std::cout << "Number " << i << " was picked " << picks[i]
                  << " times for regular picks and " << pb[i]
                  << " times for the Power Ball." << std::endl;
    }

    return 0;
}

int get_num(fstream& fin)
{
    int num = 0;

    fin >> num;
    if (fin.eof())
    {
        std::cout << "End of file reached.";
        return 0;
    }

    else if (!(fin.good))
    {
        std::cout << "Input failure.";
        exit(EXIT_FAILURE);
    }

    else return num;
}


And here are the errors when compiling:

C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp||In function 'int main()':|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|22|error: invalid initialization of reference of type 'std::fstream& {aka std::basic_fstream<char>&}' from expression of type 'std::ifstream {aka std::basic_ifstream<char>}'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|5|error: in passing argument 1 of 'int get_num(std::fstream&)'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|27|error: invalid initialization of reference of type 'std::fstream& {aka std::basic_fstream<char>&}' from expression of type 'std::ifstream {aka std::basic_ifstream<char>}'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|5|error: in passing argument 1 of 'int get_num(std::fstream&)'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|43|error: 'int get_num' redeclared as different kind of symbol|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|5|error: previous declaration of 'int get_num(std::fstream&)'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|43|error: 'fstream' was not declared in this scope|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|43|note: suggested alternative:|
c:\mingw\bin\..\lib\gcc\mingw32\4.7.0\include\c++\iosfwd|165|note:   'std::fstream'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|43|error: 'fin' was not declared in this scope|
||=== Build finished: 10 errors, 0 warnings ===|


I'm not too familiar with passing by reference yet, it's actually a bit beyond what I've been reading, I just got bored and wanted to see if I could do this. Kind of a self-teaching exercise. Which is now something more. Heh...
Mainly you are mixing fstream and ifstream. You need to be consistent in the function declaration, definition, and when calling the function, to use the same type (that is ifstream).

Lines 5 and 43 should both match, one has std::, the other does not.
Last edited on
I think your problem lies with using fstream& as your argument. Also, for file input and output, you use ostream for output and istream for input.

I'm still pretty new to file handling myself, but I think you need to adjust this:
int get_num(fstream& fin)

To this:
int get_num(istream& fin)

The same goes for line 13:
std::istream fin;
Ah ok, that's one problem down. With both using std:: I still get some errors though.:

C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp||In function 'int main()':|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|22|error: invalid initialization of reference of type 'std::fstream& {aka std::basic_fstream<char>&}' from expression of type 'std::ifstream {aka std::basic_ifstream<char>}'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|5|error: in passing argument 1 of 'int get_num(std::fstream&)'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|27|error: invalid initialization of reference of type 'std::fstream& {aka std::basic_fstream<char>&}' from expression of type 'std::ifstream {aka std::basic_ifstream<char>}'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|5|error: in passing argument 1 of 'int get_num(std::fstream&)'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp||In function 'int get_num(std::fstream&)':|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|54|error: cannot convert 'std::basic_ios<_CharT, _Traits>::good<char, std::char_traits<char> >' from type 'bool (std::basic_ios<char>::)()const' to type 'bool'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|54|error: in argument to unary !|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|61|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 6 errors, 1 warnings ===|
See the letter 'i'?

ifstream versus fstream?
Ah I see on line 13 ya, I converted that to std::fstream and it got rid of most of the remaining errors. Just have one now. Well, 2, but I'm guessing they are related to the same issue.

C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp||In function 'int get_num(std::fstream&)':|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|54|error: cannot convert 'std::basic_ios<_CharT, _Traits>::good<char, std::char_traits<char> >' from type 'bool (std::basic_ios<char>::)()const' to type 'bool'|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|54|error: in argument to unary !|
C:\Users\Raezzor\Desktop\Coding Projects\pb picker\main.cpp|61|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 2 errors, 1 warnings ===|


Edit: Bah, I'm a goof. Line 54 fin.good is a function call, and I forgot the (). Changed it to read if (!fin.good()) and all is good. Now on to testing! Thanks for the help guys!
Last edited on
good is a function of fin, not a member. Instead of:
else if (!(fin.good))
use
else if ( !fin.good() )
http://cplusplus.com/reference/iostream/ios/good/
Last edited on
Topic archived. No new replies allowed.