I need some help please with an assignment due tomorrow. Hey I\'m kinda new a C+
ID: 3630567 • Letter: I
Question
I need some help please with an assignment due tomorrow. Hey I'm kinda new a C++. I have an assignment due tomorrow... and I don't even know how to start. Could someone please help. here is my assignment.
Write a program that counts how many even and odd integers respectively in the input data file,
hw4dataf11.txt
Note:
1. Download hw4dataf11.txt to your local computer.
2. Use file I/O to read the integers from the file. If the data file is saved in your project folder, only
the file name is needed when you declare an ifstream variable, otherwise, a path to the file is
required.
3. Suppose the ifstream variable is input, then use while(!input.eof()) for testing whether the end
of file is reached.
This is all I have so far!
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
inFile.open (" hw4dataf11.txt")
y=x%2
if (y=0) = even;
if (y=1) = odd;
}
Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
int even=0,odd=0,x,y;
inFile.open ("hw4dataf11.txt");
if(inFile.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
inFile>>x;
while(!inFile.eof())
{y=x%2;
if (y==0)
even++;
if (y==1)
odd++;
inFile>>x;
}
cout<<"Number of even numbers "<<even<<endl;
cout<<"Number of odd numbers "<<odd<<endl;
system("pause");
return 0;
}