Hello Everybody

I cannot get my file to open in my eof program. Outlined below is what the program needs to do:

1) Program needs to read from a file (ex. "Data.txt")
2) Program needs to read even & odd numbers down file. (ex. 2
3
12
5)
3) Program needs to add even numbers and display sum
4) Program needs to ass odd numbers and display sum also
5) Program needs to close properly

Here is what I have so far:

// Author: 
// Ch.5 Exercise 6
// Program: EOF-even/odd
// Oct. 4, 2010

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
		// Delcare Variables
         int i;
	int evenSum = 0;
	int oddSum = 0;

		//Comment: Declare stream variables
         ifstream inFile;

		//Comment: Open Input File
	inFile.open("Data.txt");
	
		//Comment: Test Opening File
         if (!inFile.is_open()) 
	{
             cout << "There was a problem opening file "
                  << endl;
             return 1;
         }
		//Comment: File Read Operations
         while (!inFile.eof()) 
	{
		//Comment: Adding Even & Odds From 
		//         File.
             if (i % 2 == 0) evenSum += i;
	    if (i % 2 > 0) oddSum += i;
         }
		// Comment: Display Even Sum & Odd Sum
	cout << "Evens: " << evenSum << endl;
	cout << "Odds: " << oddSum << endl;
	
	// Comment: Close of File
	inFile.close();
         return 0;
}

Dani AI

Generated

Two things are causing the behavior you see: the program never extracts values from the file, and the loop uses the eof() check incorrectly. As hinted you must pull values from the stream, and as pointed out leaving i uninitialized (or setting i = 0 and never changing it) masks the real problem. If the loop tests !inFile.eof() but you never do inFile >> i inside the loop, the stream never advances and the loop never terminates — that produces the “black screen” that only disappears when you force the program to exit.

Use the extraction as the loop condition so each read both tests the stream and stores the value. This is robust and avoids the classic while(!stream.eof()) anti-pattern:

#include <fstream>
#include <iostream>

std::ifstream in("Data.txt");
if (!in) return 1;
int n, even = 0, odd = 0;
while (in >> n) {
    if ((n & 1) == 0) even += n;
    else odd += n;
}
std::cout << "Evens: " << even << "\nOdds: " << odd << "\n";

Practical tips: the filename is resolved relative to the program’s current working directory (typically the folder containing the running executable), so either place Data.txt next to the .exe (e.g., Debug/Release) or supply an absolute path while testing. To debug, print each value as it is read or run a quick check after opening the file. Do not “fix” the loop by preinitializing i — fix the read logic instead. If input may contain non-numeric tokens, guard against in.fail() and clear/skip bad input before continuing.

With the read-in-the-condition pattern and the correct file placement you should get the expected sums and the program will exit normally.

Recommended Answers

All 5 Replies

inFile >> i;

You cant get the file to open? Does the file exist in your project directory? Also, in lines 37 and 38 you do:

if (i % 2 == 0) evenSum += i;
if (i % 2 > 0) oddSum += i;

You have never initialized i with a value. This will produce a logical error.

would I put inFile >> i after

inFile.open("Data.txt");

inFile.open("Data.txt"); Because its not even opening up, when I debug the command promp comes up and says "there was a problem opening file"

if (!inFile.is_open()) 	{             cout << "There was a problem opening file "                  << endl;             return 1;         }

alls I did was create a notepad file w numbers on each line, then saved it to my desktop. did not no I needed to put the file in my project file. I will initialize i to 0. but other than that would you say the code looks ok

You cant get the file to open? Does the file exist in your project directory? Also, in lines 37 and 38 you do:

if (i % 2 == 0) evenSum += i;
if (i % 2 > 0) oddSum += i;

You have never initialized i with a value. This will produce a logical error.

Now I have it opening, I put the data file in my projects file and also initiated i=0. But now when i debug, the test window comes up and nothing is show just a black screen until I press a key to exit???:-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.