Determining number of evens, odds, and zeros in file

I've been working on this for so long now and this is where I'm at. It is required to include
1
2
3
4
5
void openFile() 
void closeFile()
int readFile()
void determine() //Determine if number is even, odd, or a zero
void display()   //Displays numbers on console  


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
  #include "pch.h"
#include <iostream>
#include <fstream>
using namespace std;

ifstream inputFile;		// Declares input file stream
int even;				// Declares even variable?
int odd;				// Declares odd variable?
int zero;				// Declares zero variable?

/************************************
 *         Function Header          *
 *           Opens File             *
 ************************************/
void openFile()
{
	inputFile.open("numbers.txt");
}

/***************************************
 *         Function Header             *
 * Determines if value is even or not. *
 ***************************************/
bool isEven(int number)
{
	if ((number % 2) != 0)
		number = true;
	else
		number = false;
	return even;
}

/***************************************
 *         Function Header             *
 * Determines if value is odd or not.  *
 ***************************************/
bool isZero(int number)
{
	bool zero;
	if (number == 0)
		number = true;
	return zero;
}

/***************************************
 *         Function Header             *
 *           Closes file               *
 ***************************************/
void closeFile()
{
	inputFile.close();
}

/***************************************
 *               Main                  *
 ***************************************/
int main()
{
	openFile();					// Call function openFile
	if (!inputFile)
	{
		cout << "Error opening file.\n";
		exit(0);
	}
	bool isEven(int number);	// Determines if number is even
	bool isOdd(int number);		// Determines if number is odd
	closeFile();				// Call function closeFile
	return 0;
}
Hello tyronejs,

Being a duplicate thread I would think that you would have learned something by now, but the program does not match up with what is in the other thread.

Then when I loaded the program it does not even compile.

using namespace std; // <--- Best not to use. Yhere has been much said here. Do a search if you are interested.

Lines 6 - 9 it is best to avoid using global variables because of the problems that it can cause. It is better to define them in "main" ,or the function(s) where they are needed, and pass them to the function(s) that need them.

In the "openFile" function the if statement that you have in "main" would be better here.
An example:
1
2
3
4
5
6
7
8
9
10
11
void openFile(ifstream& inputFile)
{
	inputFile.open("numbers.txt");

	if (!inputFile)
	{
		cout << "Error opening file.\n";
		std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread". Optional.
		exit(1);
	}
}

Line 8 is optional. In My VS it is set to close the console window when the program ends. I know I can change this, but it is the way I like it and that is why I use line 8 to give me time to see the message before the console window closes. You may not need this or even want to use it, That is OK.

If this if statement is left in "main" "return 1;" is the better choice. In a function "exit(1)" is what you have to use. In either case a value of zero means that there was no problem and it is a normal return or exit from the program. Any value greater than zero means that there is a problem. Should you have more than one "return" or "exit" any number greater than zero, like "1", "2" and "3" etc., can help to find where the problem is.

When I have to deal with opening a file for input I like using this:
1
2
3
4
5
6
7
8
9
10
11
const std::string inFileName{ "numbers.txt" };

std::ifstream inFile(inFileName);

if (!inFile)
{
	std::cout << "\n  File \"" << inFileName << "\" did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread". Optional.
	return 1;
        //exit(1);  // Use if not in "main".
}

The advantage I find with this is that you only have one place for the file name and one place to change it if needed. The two places in bold are where you use this variable name.

Over time I have developed the "cout" statement to be more descriptive of what went wrong.

Use if you like.

The function "isEven" right idea wrong approach.

The if statement does not work. "number" is what you are checking. Setting it to "true" or "false" makes no difference because when the function ends the variable is lost and so is any change you made.
When you are finished with the if/else you return the variable "even". Since this is a global variable and the compiler initialized this variable for you it is zero, so you will return "0" or "false" because this variable never changes.

The function "isEven" can be as simple as:
1
2
3
4
bool isEven(int number)
{
	return number % 2;
}

This will return "0" (false) or "1" (true) or possibly even a number greater than "1" which would still mean "true".

In the function "isZero" you define the variable bool zero;. By doing this you overshadow the global variable int zero;. So if you want to use int zero; in this function you can not. The next problem is that you are trying to return an uninitialized variable that has never been given a new value.

Again this function can be shortened to"
1
2
3
4
bool isZero(int number)
{
	return number == 0;
}


You have code to open and close a file stream, but never read the file.

In "main" lines 65 and 66 are very nice prototypes, but they do not need to be here.

You may find something like this of more use. At least to start with.
1
2
3
4
if (isEven(number))
	std::cout << "\n The nuber is odd." << std::endl;
else
	std::cout << "\n The number is even." << std::endl;


You have a workable start to the program and with the right changes it would work.

I was looking at the original code from the original post. Even it could work with the proper changes, but there are still part that can be put in this program. At least it tried to read the file.

Hope that helps,

Andy
Topic archived. No new replies allowed.