0

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;
}
3
Contributors
5
Replies
8
Views
6 Years
Discussion Span
Last Post by codingNewB
Featured Replies
  • 1

    You cant get the file to open? Does the file exist in your project directory? Also, in lines 37 and 38 you do: [CODE=c++]if (i % 2 == 0) evenSum += i; if (i % 2 > 0) oddSum += i;[/CODE] You have never initialized i with a value. This … Read More

1

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.

0

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;         }
0

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

0

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???:-/

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.