Need some help with my program

Hello,
I am attempting to create a program that uses 5 different functions to open, read, and close a file, as well as determine the # of odd/even #s, in the file, and then display the output. I have written most of the code and it seems to be executing fine but I cannot figure out how to carry all of my input data between functions. Here is what I have so far:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Prototypes
void openFile(ifstream& numbersFile);
void readFile(ifstream& numbersFile, int &numbers);
void determine (int numberstester, int& even_counter, int& odd_counter, int& zero_counter);
void display(int even_counter, int odd_counter, int zero_counter);
void closeFile(ifstream&);


//Main function of the program
int main()
{

    //introduce name for the file stream
    ifstream numbersFile;
    
    //Declare variables
    int numbers;
    int even_counter;
    int odd_counter;
    int zero_counter;
    
    openFile (numbersFile);                         //call openFile function

    readFile (numbersFile, numbers);            //call readFile function
    
    determine (numbers, even_counter, odd_counter, zero_counter);         //call determine function
    
    display (even_counter, odd_counter, zero_counter);                //call display function
    
    closeFile (numbersFile);               //call closeFile function
    
    
}


//**********************************
//Write function to open input file
//***********************************
void openFile(ifstream& numbersFile)
{
    numbersFile.open("numbers.txt");
    if (!numbersFile)
    {
        cerr << "Input file did not open correctly." << endl;
    }
}


//****************************************
//Write a function to read the input file
//****************************************
void readFile(ifstream& numbersFile, int &numbers)
{
        while (numbersFile >> numbers)
        {
            cout << numbers << endl;
        }
}
//********************************************************************
//Write a function to determine whether the input data is even or odd
//********************************************************************

void determine (int numberstester, int& even_counter, int& odd_counter, int& zero_counter)
{
    even_counter = 0;
    odd_counter = 0;
    zero_counter = 0;
    if (numberstester == 0)
    {
        zero_counter++;
    }
    else if (((numberstester % 2) == 0))
    {
        even_counter++;
    }
    else if ((numberstester % 2) == 1)
    {
        odd_counter++;
    }
    else
    {
        cerr << "The inputed numbers were not read properly" << endl;
    }
}

//**********************************************************************************
//Write a function to display the information gatherred (Amount of even and odd #s)
//***********************************************************************************

void display(int even_counter, int odd_counter, int zero_counter)
{
    cout << "There are " << zero_counter << " zeros in the inputed file" << endl << endl;
    cout << "There are " << even_counter << " even numbers in the inputed file" << endl << endl;
    cout << "There are " << odd_counter << " odd numbers in the inputed file" << endl << endl;
}

//*****************************************
//Write a function to close the input file
//*****************************************
void closeFile(ifstream&)
{
    ifstream numbersFile;
    numbersFile.close();
}
[/code]
]
Put the code you need help with here.
When I do this, it outputs all the data that I need.

1
2
3
4
5
6
7
void readFile(ifstream& numbersFile, int &numbers)
{
        while (numbersFile >> numbers)
        {
            cout << numbers << endl;
        }
}


But then I cannot seem to use that data in the determine() function.
Please help
Last edited on
numbers should be a vector or array or the like. currently you read the numbers, print them, and throw them all but the last one away... int numbers only holds ONE number at a time.

do you know array or vector?
Your readFile() function is going to read the entire file placing every number in the same place (numberrs). You don't call determine() until you've read the entire file. Don't you think it would be a good idea to call determine() each time you've got a new number?

In your closeFile() function, you're declaring a local copy of numbersFile, which was never opened.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

I have tried using arrays but I continued getting errors because it told me that arrays were not compatible with data type, int&. And no, we have not learned vectors yet.

Maybe should i try to call determine inside of the readFile function? Would i then need to include my counters in the read function as well and redeclare them?
So I tried to call the determine() function inside of the read function and have something like this

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Prototypes
void openFile(ifstream& numbersFile);
void readFile(ifstream& numbersFile, int& even_counter, int& odd_counter,int& zero_counter);
void determine (int numberstester, int& even_counter, int& odd_counter, int& zero_counter);
void display(int even_counter, int odd_counter, int zero_counter);
void closeFile(ifstream&);


//Main function of the program
int main()
{

    //introduce name for the file stream
    ifstream numbersFile;
    
    //Declare variables
    int even_counter;
    int odd_counter;
    int zero_counter;
    
    openFile (numbersFile);                         //call openFile function

    readFile (numbersFile, even_counter, odd_counter, zero_counter);            //call readFile function
    
    display (even_counter, odd_counter, zero_counter);                //call display function
    
    closeFile (numbersFile);               //call closeFile function
    
    
}


//**********************************
//Write function to open input file
//***********************************
void openFile(ifstream& numbersFile)
{
    numbersFile.open("numbers.txt");
    if (!numbersFile)
    {
        cerr << "Input file did not open correctly." << endl;
    }
}


//****************************************
//Write a function to read the input file
//****************************************
void readFile(ifstream& numbersFile, int& even_counter, int& odd_counter,int& zero_counter)
{
    int numbers;
    while (numbersFile >> numbers)
    {
        determine(numbers, even_counter, odd_counter, zero_counter);
    }
    cout << even_counter << endl << odd_counter << endl << zero_counter << endl;
}
//********************************************************************
//Write a function to determine whether the input data is even or odd
//********************************************************************

void determine (int numberstester, int& even_counter, int& odd_counter, int& zero_counter)
{
    even_counter = 0;
    odd_counter = 0;
    zero_counter = 0;
    if (numberstester == 0)
    {
        zero_counter++;
    }
    else if (((numberstester % 2) == 0))
    {
        even_counter++;
    }
    else if ((numberstester % 2) == 1)
    {
        odd_counter++;
    }
    else
    {
        cerr << "The inputed numbers were not read properly" << endl;
    }
}

//**********************************************************************************
//Write a function to display the information gatherred (Amount of even and odd #s)
//***********************************************************************************

void display(int even_counter, int odd_counter, int zero_counter)
{
    cout << "There are " << even_counter << " even numbers in the inputed file" << endl << endl;
    cout << "There are " << odd_counter << " odd numbers in the inputed file" << endl << endl;
    cout << "There are " << zero_counter << " zeros in the inputed file" << endl << endl;
}

//*****************************************
//Write a function to close the input file
//*****************************************
void closeFile(ifstream&)
{
    ifstream numbersFile;
    numbersFile.close();
}


But I am still getting only 1 read. At this point, shouldnt the numbers go directly into the counters and then they would go back to the main function. Let me know if you can help
Last edited on
Lines 23-25: Your counters are uninitialized (garbage).

Line 70-72: Each time you call determine() from your read loop, you're going to set the counters back to 0.

Line 107: You still have numbersFile as a local variable. It should be passed as an argument.
Topic archived. No new replies allowed.