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:" <

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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;

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education