Can someone help me understand why the output file prints an extra number 1, even though the input does not have one. I do not understand why it is adding an extra number what I want it to do is just reprint the input again on the output #include #include #include using namespace std; int getNumber(ifstream& infile, ofstream& outfile){ int num; infile >> num; outfile << setw(5) << num << " "; return num; } void classifyNumber(ifstream& infile,int&even,int&odd,int&zero){ int num; while(!infile.eof( )){ infile >> num; if (num % 2 == 0) { even++; if (num == 0) zero++; } else odd++; } } void printResult(ofstream& outfile,int& even,int& odd,int& zero,int& sum,double& average ){ outfile << endl; outfile << "There are " << even << " even numbers." << endl; outfile << "There are " << odd << " odd numbers" << endl; outfile << "Total number of zeroes:" << zero << endl; outfile << "Sum of numbers:" << sum << endl; outfile << "Average of numbers:" << average; cout << "There are " << even << " even numbers." << endl; cout << "There are " << odd << " odd numbers." << endl; cout << "Total numbers of zeroes:" << zero << endl; cout << "Sum of numbers:" <
Can someone help me understand why the output file prints an extra number 1, even though the input does not have one. I do not understand why it is adding an extra number what I want it to do is just reprint the input again on the output
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int getNumber(ifstream& infile, ofstream& outfile){
int num;
infile >> num;
outfile << setw(5) << num << " ";
return num;
}
void classifyNumber(ifstream& infile,int&even,int&odd,int&zero){
int num;
while(!infile.eof( )){
infile >> num;
if (num % 2 == 0)
{
even++;
if (num == 0)
zero++;
}
else
odd++;
}
}
void printResult(ofstream& outfile,int& even,int& odd,int& zero,int& sum,double& average ){
outfile << endl;
outfile << "There are " << even << " even numbers." << endl;
outfile << "There are " << odd << " odd numbers" << endl;
outfile << "Total number of zeroes:" << zero << endl;
outfile << "Sum of numbers:" << sum << endl;
outfile << "Average of numbers:" << average;
cout << "There are " << even << " even numbers." << endl;
cout << "There are " << odd << " odd numbers." << endl;
cout << "Total numbers of zeroes:" << zero << endl;
cout << "Sum of numbers:" <<sum << endl;
cout << "Average of numbers:" << average;
}
int main(){
ifstream in;
in.open("numbers.txt");
ofstream of;
of.open("out.txt");
int sum=0,count = 0;
double average = 0;
int n;
while(!in.eof())
{
n = getNumber(in,of);
count++;
if(count%10 !=0 )
cout<<setw(4)<<n<<" ";
else{
cout<<setw(5)<<n<<endl;
of<<setw(5)<<endl;
}
sum +=n;
}
average = sum/(count*1.0);
in.close();
int count_even,count_odd,count_zero;
in.open("numbers.txt");
classifyNumber(in,count_even,count_odd,count_zero);
printResult(of,count_even,count_odd,count_zero,sum,average);
//close the files
in.close();
of.close();
return 0;
}

Trending now
This is a popular solution!
Step by step
Solved in 2 steps









