a friend asked me to help him with his lab. the lab involves opening a file and adding the numbers in the file together displaying how many even and how many odd numbers there are. I have the file "textinput.txt" in the directory with the .cpp file but when i build i still get the error message. I don't get why. I'm using XCode if that helps.

Did i forget to include a correct library or is there something about how XCode reads the ifstream.open that i do not completely understand?

#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main ()
{

    int evensum=0, even=0, odd=0, oddsum=0, counter=0, list=0, sum=0;
    
    ifstream infile;
    infile.open("input.txt");
    
    if (infile.fail()) {
        cout << "Sum:\t" << "Error" << endl;
        cout << "Even:\t" << "Error" << ", " << "Error" << endl;
        cout << "Odd:\t" << "Error" << ", " << "Error" << endl << endl;
        cout << "Error opening file!!! Now closing program!!!" << endl;
        exit(1);
	}
    
    else {
        while (counter<=50) {
            
            if (list%2) {
                infile >> list;
                even=even++;
                evensum=evensum+list;
            }
            
            else if (!(list%2)){
                infile >> list;
                odd=odd++;
                oddsum=oddsum+list;
            }
            
            counter=even+odd;
        }
        
        sum=evensum+oddsum;
        
        cout << "Sum:\t" << sum << endl;
        cout << "Even:\t" << even << ", " << evensum << endl;
        cout << "Odd:\t" << odd << ", " << oddsum << endl;
        
    }
    
    infile.close();
    return 0;
}

Oh and I know exit(1); is an un-natural way of closing the program because it literally crashes it but I was testing with some other code within this so I left it.

Edited 4 Years Ago by pattmorter: n/a

but when i build i still get the error message

What's "the error message"? If you mean when you *run* the program you get the error opening file message, then I'd suggest using perror() to get more details:

if (infile.fail()) {
    perror("Error opening file!!! Now closing program!!!");
    exit(1);
}

Don't forget to include <cstdio> for perror()'s declaration.

Oh and I know exit(1); is an un-natural way of closing the program because it literally crashes it

Not really. exit(1) is identical to return 1 from main(). You're probably thinking of abort().

Ok, i used perror() and it says no such file or director. I have the correctly named .txt file in the same directory as the .cpp file so I'm lost right now.

in the same directory as the .cpp file so I'm lost right now.

That's probably your problem. Typically relative file names will be looked for in the current working directory, not the source file directory. Sometimes those will be the same, but more often than not they won't be.

The current working directory is probably going to be the same directory as where the executable is. You might be able to see it with a test program like this:

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    if (argc >= 1)
    {
        cout << "Current working directory: " << argv[0] << endl;
    }
}

Edited 4 Years Ago by deceptikon: n/a

Here is the final code that works completely. Couldn't get the code to work on XCode so i stuck it in Visual Studio and it worked the first time with the input.txt file in the same directory as the .cpp file.

#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib> //needed for exit(1);
#include <cstdio> //for perror(); displays that no such directory/file exists if there is an error.

using namespace std;

int main ()
{

    int evensum=0, even=0, odd=0, oddsum=0, counter=1, list=0, sum=0;

    ifstream in_file;
    in_file.open("input.txt"); //file must be in same directory as the .cpp file!!!

    if (in_file.fail()) { //checks the validity of the in_file.open("input.txt")
        perror("Error opening file!"); //displays that no such directory/file exists if there is an error.
        cout << "Now closing program!";
        exit(1); //exits program upon error
	}

    else {
        while (counter<50) {

            //for even numbers
            if (list%2) {
                in_file >> list;
                even=even++; //keeps count of how many even numbers have been read
                evensum=evensum+list; //seperate sum for all even numbers
            }

            //for odd numbers
            else if (!(list%2)){
                in_file >> list;
                odd=odd++; //keeps count of how many odd numbers have been read
                oddsum=oddsum+list; //seperate sum for all odd numbers
            }

            counter=even+odd; //counter for while loop. only loops until 50 lines are read
        }

        sum=evensum+oddsum; //sums both oddsum and evensum together

        cout << "The sum of the first " << counter << " lines from the file " 
               << "\'input.txt\'" << " is " << sum << endl;
        cout << "There were " << even << " even numbers for a sum of " << evensum << endl;
        cout << "There were " << odd << " odd numbers for a sum of " << oddsum << endl;

    }

    in_file.close();
    return 0;
}

Here is the output I recieved:

The sum of the first 50 lines from the file 'input.txt' is 1803
There were 23 even numbers for a sum of 646
There were 27 odd numbers for a sum of 1157
This question has already been answered. Start a new discussion instead.