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:
#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;
}
elseif (!(fin.good))
{
std::cout << "Input failure.";
exit(EXIT_FAILURE);
}
elsereturn num;
}
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.
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 ===|
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!