using file input to determine number of evens, number of odds, and number of zeroes using functions

Mar 17, 2019 at 9:02pm
Hi i have a project for my c++ class that has me stumped! The goal is to determine if numbers from an input file ("numbers.txt") are even, odd, or zeroes and to display them on the screen. The trick is that I cannot write the program using only main(), I have to use functions. This is what I have so far:

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

const int SIZE = 200;
ifstream inputFile; // Declares input file stream

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

/************************************
* Function Header *
* Reads File *
************************************/
int readFile()
{
while (inputFile >> )
}

/************************************
* Function Header *
* Splits numbers into digits. *
************************************/
void splitDigits(int integer, int digitCount, int digit[])
{
for (int i = 0; i < digitCount; i++)
{
digit[i] = int(integer / pow(10., double(i)));
digit[i] = int(digit[i]) % 10;
}
}

void countDigits(int &isEven, int &isOdd, int &isZero, int digitCount, int digit[])
{
for (int i = 0; i < digitCount; i++)
{
if (digit[i] == 0)
{
isZero++;
}
else if (digit[i] % 2 == 0)
{
isEven++;
}
else
{
isOdd++;
}
}
}

/************************************
* Function Header *
* Determines if int is *
* even, odd, or zero. *
************************************
void determine()
{

}/

/************************************
* Function Header *
* Displays total number of odds, *
* evens, and zeroes in input file. *
************************************
void display()
{

}/

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

/************************************
* Main *
************************************/
int main()
{
int integer;
openFile(); // Call function openFile
if (!inputFile)
{
cout << "Error opening file.\n";
exit(0);
}
int isZero = 0;
int isEven = 0;
int isOdd = 0;
int readFile(); // Call function readFile
{

}
// determine(); // Call function determine
// display(); // Call function display
closeFile(); // Call function closeFile



return 0;
}


Any feedback would be much appreciated. I'm new to programming and am just totally stumped in this "intro" class.
Last edited on Mar 17, 2019 at 11:01pm
Mar 17, 2019 at 11:14pm
You’re killing yourself with too much work.

You need three functions besides main():

1
2
3
  bool is_zero( int n );
  bool is_even( int n );
  bool is_odd( int n );

Hope this helps.
Mar 17, 2019 at 11:17pm
Thanks Duthomhas! Will definitely try this
Mar 17, 2019 at 11:24pm
Actually, it is a requirement to use

1
2
3
4
5
void openFile()
void closeFile()
int readFile()
void determine()
void display()


in my project. It is not so simple.
Mar 17, 2019 at 11:36pm
Alas, sorry. Your project is poorly-designed.

So, you seem to be doing okay so far.

By “odd” and “even”, does your assignment really mean for you to count the number of digits in the number?
Mar 17, 2019 at 11:41pm
It is unspecified whether or not to count the number of digits, however, I think it is safe to assume so since there are no single '0's in the .txt file.
Mar 17, 2019 at 11:43pm
I've also started my project over, this time using bool as my primary source.

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

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

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

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

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

int main()
{
	openFile();		// Call function openFile
	if (!inputFile)
	{
		cout << "Error opening file.\n";
		exit(0);
	}
	bool isEven(int number);
	bool isOdd(int number);

	return 0;
}

Is this any better than before? (even though it is obviously not finished)
It says that the 'even' and 'odd' local variables are "uninitialized".
Last edited on Mar 17, 2019 at 11:44pm
Mar 17, 2019 at 11:50pm
Much, much better.

Remember, the function is meant to return a truth value.

bool isEven( int number ) -- is number an even number?

1
2
3
4
5
bool isEven(int number)
{
	if ((number % 2) != 0) return false;
	return true;
}

:O)
Last edited on Mar 17, 2019 at 11:51pm
Mar 17, 2019 at 11:54pm
Like so..?
1
2
3
4
5
6
7
8
9
bool isEven(int number)
{
	bool even;
	if (number % 2)
		number = true;
	else
		number = false;
	return even;
}
Mar 18, 2019 at 12:01am
no.
Mar 18, 2019 at 12:04am
1
2
3
4
5
6
7
8
bool isEven(int number)
{
	if ((number % 2) != 0)
		number = true;
	else
		number = false;
	return even;
}

?
What was the bit about return false;return true;?
Last edited on Mar 18, 2019 at 12:06am
Mar 18, 2019 at 12:20am
also, this still doesn't set the code in "determine()" form. I cannot just use 'bool'. That would get me a 0. Not that I'm not appreciative or anything, I'm very grateful for every ounce of information that comes my way but there are certain criteria that I must meet.
Last edited on Mar 18, 2019 at 12:22am
Mar 18, 2019 at 12:47am
Unfortunately I am working on very limited information. Sorry.

You seem to be doing fairly well, the problem is you are having trouble with the idea of function return values.
Mar 18, 2019 at 1:27am
what information would you like? I'm basically an open book. lol
Mar 18, 2019 at 1:49am
> It is unspecified whether or not to count the number of digits, however, I think it is safe to assume so
no, it is not safe to assume
read your assignment or ask for a clarification to determine if you need to classify numbers or their digits
those are two diffferent problems with different approach (for example, for digits may use a look up table)


> this still doesn't set the code in "determine()" form
¿what's the purpose of determine()?
¿what''s the purpose of display()?
I highly doubt their signatures as they require an extensive use of globals.
Topic archived. No new replies allowed.