### How to count the number of even numbers in a text file

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main() {

ifstream inputFile;
string filename;
int number;
int counter = 0;
int counter_even = 0;

cout << "Enter the file name: " << endl;
cin >> filename;

inputFile.open(filename.c_str());

if (filename.c_str()) {
while (inputFile >> number) {
counter++; }
while (inputFile >> number) {
number % 2;
counter_even++;

}
cout << counter << endl;
cout << counter_even << endl;

return 0; }

This is my first time taking a C++ course and I can't seem to figure out how to count the number of even numbers in my text file. Any help would be greatly appreciated.

I keep getting 0 as my output for counter_even.
Last edited on

 ``12`` ``````while (inputFile >> number) { // READ IN ALL NUMBERS counter++; } // INCREMENT COUNTER EACH TIME ``````

Your code reads in ALL the numbers and just increments the counter. Shouldn't you be examining each number when you read it in to see if it's even?

The second while loop; what work does it do? All the number have already been read in.
This is my assignment:
A) The number of numbers in the file:
B) The number of even numbers in the file:
C) The number of odd numbers in the file:
D) The sum of all the numbers in the file:
E) The average of all the numbers in the file:

What you referred to is part A and I got that down, but I'm trying to figure out part B.

if (filename.c_str()) {
while (inputFile >> number) {
counter++;
if (number % 2)
counter_even++; }

With this code, I get 101 as my output, but I don't know how to verify if I'm correct without having to actually count all the numbers myself.
`number % 2` returns a zero, or a one.

number %2 isn't inherently a test for evenness. You need to look at the result, and compare it with something.

I suspect you're looking for:

 ``1234`` ``````if (number%2 == 0) { counter_even++; }``````

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include #include #include using namespace std; int main() { ifstream inputFile; string filename; int number; int average; unsigned int sum = 0; int counter = 0; int counter_even = 0; int counter_odd = 0; cout << "Enter the file name: " << endl; cin >> filename; inputFile.open(filename.c_str()); if (filename.c_str()) { while (inputFile >> number) { counter++; if (number % 2 == 0) counter_even++; else counter_odd++; sum += number; average = sum / (counter_even + counter_odd); } } cout << "The number of numbers in the file is " << counter << "." << endl; cout << "The number of even numbers in the file is " << counter_even << "." << endl; cout << "The number of odd numbers in the file is " << counter_odd << "." << endl; cout << "The sum of the numbers in the file is " << sum << "." << endl; cout << "The average of the numbers in the file is " << average << "." << endl; return 0; } ``````

Okay, so this is what I have so far. Now I just need input on whether or not this would give me an accurate display.
Last edited on
The best way to determine that is to make a test file with a known number of elements.