Counting Even and Odd Numbers

Hi I am currently in an intro to c++ course and got this for an assignment. I know how to check if a number from a text file is even or odd but I don't know how to COUNT how many even and odd numbers are in the file.Please help.These are the directions:

Use the appropriate loop structure to process all of the numbers in the file, keeping track of the number of even values and the number of odd values. Additionally, output the actual number of even and odd values in the file.
You need to use some sort of loop to input the values in the file into an array (do you know how to do that?), and while you are doing it, you can have some kind of counter variable, say num_evens and num_odds, that are incremented whenever a number read from the file is even or odd, respectively.
I do not know how to input the values of the file into an array.So far I've been trying to do it like this:

{


int evenCount=0;
int evenCount = 0;





if (num % 2 == 0)
{

evenCount = evenCount + 1;





}
else if (!num % 2 == 0)

{

oddCount = oddCount + 1;






}
Here's some example code for reading from a file:
http://www.cplusplus.com/doc/tutorial/files/

You can read that document to understand how fstreams work. Once you understand that, it should be simple. You should input the values from a file into an array (you can use a loop for this), and in the same loop that you are inputting the values into the array, you can use the code you have written above and count the number of odds and evens.
@TheToaster

I have a question - why should OP put the values into an array if the assignment is only asking for the number of even and odds?

Additionally, output the actual number of even and odd values in the file.


Unless I'm misinterpreting, isn't this just outputting the number of evens and the number of odds and not the actual values themselves?
I could have sworn I saw something about an array in there. Guess not.

OP, In any case, all that is really required is to output the number of evens and odds after counting them, which can be done in the aforementioned loop. You would still need to know how to use fstreams.
Last edited on
Something like this could work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>
#include <fstream>
#include <iterator>

int main()
{
    int num_evens{}, num_odds{};
    std::ifstream inputFile{"inputFile.txt"};
    std::for_each(std::istream_iterator<int>(inputFile), {}, 
                      [&](int i){i % 2 ? ++num_odds : ++num_evens;});
    std::cout << num_odds << " " << num_evens;

    return 0;
}
Last edited on
Thanks but Is there a way I can do this in some sort of loop structure instead of an array?
Pseudocode:

- Initialise your counts of evens and odds
- open your filestream (e.g. [code]ifstream input( "myfile.txt" );[/code] )
- loop whilst you can successfully read a num from file
{
   - update count of even or odd according to num (as in your initial post)
}
- output your counts of even and odd
Hello YoungProg19,

Thia should get you started:
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
#include <iostream>
#include <iomanip>
//#include <string>
#include <limits>

#include <fstream>

int main()
{
	int num{};
	size_t evenCount{};
	size_t oddCount{};
	size_t count{};

	const std::string inFileName{ "Input Data.txt" };

	std::ifstream inFile(inFileName);

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

	while (inFile >> num)
	{
	
             // <--- Code for dealing with "num" and counts.

	}


Andy
Okay Thank you so much
Topic archived. No new replies allowed.