Returning a value of 5

Feb 14, 2015 at 10:30pm
I have been working on an array that needs to return the number of even numbers from a file. My problem is that no matter what I have in the file for numbers it always says there are 5 numbers. I think the problem is in my for statements but I'm not sure how to use them without the way they currently are.

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
  #define MAX_ROWS 3
#define MAX_COLUMNS 2
ifstream in;

void main()
	{
		int E;
		in.open("numbers.txt"); //opens the input file
		if (!in)				//checks for the input file
		
		{
			cerr << "****Error numbers file does not exist****" << '\n';
			exit(1);
		}

		in >> E;
		int A[MAX_ROWS][MAX_COLUMNS] = { E }; //assign values to the array
		int B, C, D=0;
		for (B = 0; B < 3; B++)
	{
		for (C = 0; C < 2; C++)
		{

			if (A[B][C]%2 == 0)			//Check for even values
			{
				D++;
			}
		}

	}
	cout << "There are " << D << " even numbers" << '\n'; //output total even values
	cout << "The program is complete!"<<'\n';
	}


The program woks fine when I have the values declared in the program, it just doesn't work right when I am reading from a file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define MAX_ROWS 3
#define MAX_COLUMNS 2
int A[MAX_ROWS][MAX_COLUMNS] = { { 3, 2 }, { 4, 5 }, { 2, 2 } }; //assign values to the array
	int B, C, D;
int _tmain(int argc, _TCHAR* argv[])
	{
	for (B = 0; B < 3; B++)
	{
		for (C = 0; C < 2; C++)
		{

			if (A[B][C]%2 == 0)			//Check for even values
			{
				D++;
			}
		}

	}
	cout << "There are " << D << " even numbers" << '\n'; //output total even values
	cout << "The program is complete!"<<'\n';
	return 0;
Last edited on Feb 14, 2015 at 11:20pm
Feb 15, 2015 at 5:08am
From looking over my code and other examples on the site, I feel like my values from the file aren't getting passed into the program. As a result it is simply giving me to size of the two arrays, am I right?
Feb 15, 2015 at 6:24am
You are reading the entire file into a single variable of type int. No way that can work as you are intending.
Feb 15, 2015 at 6:16pm
Are you saying I need to change the type or have it read into multiple variables?
Feb 15, 2015 at 7:01pm
Do I need to create a string to read the information from the file?
Feb 15, 2015 at 8:21pm
closed account (D80DSL3A)
What I think happened:
One value only (1st in file) is read into E.
The array A[][] is initialized with A[0][0] = E and all other elements = 0.
If 1st value is 3 (like in top code example) then you have 1 odd #(3) and 5 even #'s(0).
Output is as expected.

Try this to replace all code in lines 16 through 30 of 1st code shown:
1
2
3
4
int D = 0;

while( in >> E )
    if( E%2 == 0 ) D++;
Last edited on Feb 15, 2015 at 8:22pm
Feb 15, 2015 at 8:33pm
That works, but I have no idea why, can you fill me in?
Feb 15, 2015 at 8:46pm
closed account (D80DSL3A)
Values are read one by one from the file into E, once per iteration of the while loop.
Each value of E is checked to see if it's even and if so, D is incremented.

The while loop goes until there is no more integer data to read from the file.
in >> E evaluates as false when it reaches the end of the file so the while loop is terminated.

This method also allows there to be any number of integers in the file, not just 6 as in the array examples you had.
Last edited on Feb 15, 2015 at 8:51pm
Feb 15, 2015 at 11:04pm
okay thanks for the explanation.
Feb 17, 2015 at 2:03am
fun2code,
I was looking over what you gave me again, and correct me if I'm wrong but I have now removed all traces of an array right?
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
#include "stdafx.h"

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


#define MAX_ROWS 3
#define MAX_COLUMNS 2
ifstream in;
void main()
	{
		int E;
		in.open("numbers.txt"); //opens the input file
		if (!in)				//checks for the input file
		
		{
			cerr << "****Error numbers file does not exist****" << '\n';
			exit(1);
		}
		
		
		int D = 0;

		while (in >> E)
			if (E % 2 == 0)D++;
	cout << "There are " << D << " even numbers" << '\n'; //output total even values
	cout << "The program is complete!"<<'\n';
	in.close();
	}
Last edited on Feb 17, 2015 at 2:03am
Feb 17, 2015 at 3:05am
closed account (D80DSL3A)
Yes you have.
You could also remove lines 8 and 9. Those constants are no longer in use.
They were for use with the array you had.
Feb 17, 2015 at 3:20am
okay, thanks
Topic archived. No new replies allowed.