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.