views:

102

answers:

3

//can someone with a little time on their hands please compile and run this code and see where I am going wrong. Help! Thanks everyone! //

#include <iostream>
#include <fstream>

using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::ios;


int main()
{ 

int numbers = 0;

//create and open file
ifstream inFile;
ofstream outFile;
inFile.open ("numbers.txt", ios::in);
outFile.open ("evenNumbers.txt", ios::out);


//determine whether the file was opened
if (inFile.is_open() && outFile.is_open())
{
//read numbers file
inFile >> numbers;

while (!inFile.eof())
{
   //look for even numbers
   if (numbers %2 == 0)
   {
    outFile << numbers << endl;
    //cout << numbers << endl;
    }
    inFile >> numbers;

 }
//end while

//close files
outFile.close();
inFile.close();

cout << "Program successful. File complete." << endl;

}

//if file fails to open, display error message else cout << " File could not be opened " << endl;

//end if


system ("pause");
return 0;

} //end of main function

A: 

Well, the heading variable is undeclared, but I guess you already know that....

Pawel Marciniak
A: 

The error is here:

outFile << heading << endl;
outFile << columnHeaders << endl;
outFile << underLines << endl;

This is the first time you these variables heading, columnHeaders and underLines are mentioned in your program. The compiler complains because they have not been declared anywhere (Should they be int or std::string ore some other type?). Also they won't contain any useful values because nothing has been assigned to them.

sth
+1  A: 

Your code has quite a few problems:

ifstream inFile;
inFile.open("numbers.txt", ios::in);

It's not exactly an error, but ios::in is the default for an ifstream, and you typically supply the file name to the constructor, something like this:

ifstream inFile("numbers.txt");

Then we have this:

getline(inFile, name);
inFile >> num;
while (inFile.eof())

while (inFile.eof()) seems to have the logic backwards -- you want to read until you reach the end of the, then quit. The rest of your loop will work (unusual for one that uses file.eof() as the condition) but is unnecessarily long and difficult to read.

 //create file object and open the file
 ofstream outFile;
 outFile.open("updatedNumbers.txt", ios::out);

As you'd expect from the previous comment, ios::out is the default for an ofstream, and you usually give the file name to the constructor: ofstream outFile("updatedNumbers.txt");

 //write the updated numbers to the file
 outFile << heading << endl;
 outFile << columnHeaders << endl;
 outFile << underLines << endl;

heading, columnHeadders and underLines seem to be undefined variables.

    for (x=0;x<20; x++)
    {
    if (int x%2==0)
      sample[x] = x+2;
    else
      sample[x] = x+20;

sample also seems to be undefined.

   outFile << num[x] << endl;

num also seems to be undefined. Perhaps you intended it to be the same as sample? Otherwise, you don't seem to have any code to set it to any particular value before you write it out.

Probably worse than any of that is the fact that your heading talks about writing the even numbers from one file to another, but your code doesn't seem to do anything even vaguely similar to that at all.

Jerry Coffin