Thread: C++ Program that reads numbers from a file and calculates the average of them

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    10

    C++ Program that reads numbers from a file and calculates the average of them

    https://gist.github.com/anonymous/9536644

    Here is my code and basically these are the steps.
    I feel like we have something good to work on but we keep getting errors. Literally any amount of help would be appreciated.

    a. Data to the program is input from a file of an unspecified length; that is, the program does not know in advance how many numbers are in the file.

    b. Save the output of the program in a file.

    c. Modify the function getNumber so that it reads a number from the input file (opened in the function main), outputs the number to the output file (opened in the function main), and sends
    the number read to the function main. Print only 10 numbers per line.

    d. Have the program find the sum and average of the numbers.

    e. Modify the function printResult so that it outputs the final results to the output file (opened in the function main). Other than outputting the appropriate counts, this new definition of the function printResult should also output the sum and average of the numbers.

  2. #2
    Registered User
    Join Date
    Mar 2014
    Posts
    10
    Code:
    //Program: Classify Numbers
    //This program counts the number of zeros, odd, and even numbers *?*/
     
    #include <iostream>
    #include <iomanip>
    #include <fstream>
     
    using namespace std;
     
     
        //Function prototypes
    void initialize(int& zeroCount, int& oddCount, int& evenCount);
    void getNumber(int& num);
    void classifyNumber(int num, int& zeroCount, int& oddCount, 
                        int& evenCount);
    void printResults(int zeroCount, int oddCount, int evenCount);
    bool getNumber(ifstream& numberFile, int& zeros, int& odds, int& evens);
    void writeNumber(ofstream& resultsFile, int zeros, int odds, int evens, 
         int sum, int avg);
    int calcSum( int zeros, int evens, int odds);
     
    int main ()
    {
         
        ifstream inFile;
        ofstream outFile;
        int N;  
        int sum;
        int avg; 
         
     
         
         
            //Variable declaration
        int counter; //loop control variable 
        int number;  //variable to store the new number 
        int zeros;   //variable to store the number of zeros 
        int odds;    //variable to store the number of odd integers s
        int evens;   //variable to store the number of even integers 
        //int sum;     // sum of the numbers
        //int avg;     //average of the number
     
        initialize(zeros, odds, evens);                 //Step 1
     
        //cout << "Please enter " << N << " integers."
         //    << endl;                                   //Step 2
        //cout << "The numbers you entered are: "
         //    << endl;
     
    //numberfile.open ("f:number.dat");
     
              //  if(inFile)
                //cout<< "Open \ 'f:numberfile.dat\' " << endl;
                //else
                      
                //cout << "File not opened \ 'f:number.txt\' << endl;
                 
    inFile.open ("problem01Data.txt");
    if(inFile)
                  {                 //failure of opening number file
                  cout << "Error in opening number file .\n" << endl;
                  system ("pause");
                  return 0;
                  }
    outFile.open("f:results.dat");
    if(!outFile)
                    {                 //failure of opeinging results file
                    cout << "Error in opening results file .\n" << endl;
                     
                    system("pause");
                    return 0;
                    }
                     
    while ( getNumber (inFile, zeros, odds, evens))
          {
          
        for (counter = 1; counter <= N; counter++)      //Step 3
        {
            getNumber(number);                          //Step 3a
            cout << number << " ";                      //Step 3b
            classifyNumber(number, zeros, odds, evens); //Step 3c
        }// end for loop 
     
        // calcSum (zeroCount,  evenCount,  oddCount);
          writeNumber(outFile, zeros, odds, evens, sum, avg);
          }
     
         
        cout << endl;
     
        printResults(zeros, odds, evens);               //Step 4
     
        return 0;
    }
     
    void initialize(int& zeroCount, int& oddCount, int& evenCount)
    {
        zeroCount = 0;
        oddCount = 0;
        evenCount = 0;
    }
     
    void getNumber(int& num)
    {
        cin >> num;
    }
     
    void classifyNumber(int num, int& zeroCount, int& oddCount,
                        int& evenCount)
    {
        switch (num % 2)
        {
        case 0: 
            evenCount++;  
            if (num == 0)
                zeroCount++;  
            break;
       case 1: 
       case -1: 
           oddCount++;
           
       } //end switch
    }
     
    int calcSum( int zeroCount, int oddCount, int evenCount)
    {
    	
        return zeroCount + oddCount + evenCount;
    }
         
     
    void printResults(int zeroCount, int oddCount, int evenCount)
    { 
       cout << "There are " << evenCount << " evens, "
          << "which includes " << zeroCount << " zeros"
        << endl;
     
     cout << "The number of odd numbers is: " << oddCount
        << endl;
         
    // calcuSum
    }

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I feel like we have something good to work on but we keep getting errors.
    What are these errors? If they're compiler errors post the complete error messages exactly as they appear in your development environment.

    Jim

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    10
    1>------ Build started: Project: Project 1 part one, Configuration: Debug Win32 ------
    1>Build started 3/13/2014 5:25:25 PM.
    1>InitializeBuildStatus:
    1> Touching "Debug\Project 1 part one.unsuccessfulbuild".
    1>ClCompile:
    1> Project 1 Part one.cpp
    1>e:\john addy\documents\visual studio 2010\projects\project 1 part one\project 1 part one\project 1 part one.cpp(77): warning C4700: uninitialized local variable 'N' used
    1>e:\john addy\documents\visual studio 2010\projects\project 1 part one\project 1 part one\project 1 part one.cpp(85): warning C4700: uninitialized local variable 'avg' used
    1>e:\john addy\documents\visual studio 2010\projects\project 1 part one\project 1 part one\project 1 part one.cpp(85): warning C4700: uninitialized local variable 'sum' used
    1>ManifestResourceCompile:
    1> All outputs are up-to-date.
    1>Project 1 Part one.obj : error LNK2019: unresolved external symbol "void __cdecl writeNumber(class std::basic_ofstream<char,struct std::char_traits<char> > &,int,int,int,int,int)" (?writeNumber@@YAXAAV?$basic_ofstream@DU?$char_tra its@D@std@@@std@@HHHHH@Z) referenced in function _main
    1>Project 1 Part one.obj : error LNK2019: unresolved external symbol "bool __cdecl getNumber(class std::basic_ifstream<char,struct std::char_traits<char> > &,int &,int &,int &)" (?getNumber@@YA_NAAV?$basic_ifstream@DU?$char_trai ts@D@std@@@std@@AAH11@Z) referenced in function _main
    1>e:\john addy\documents\visual studio 2010\Projects\Project 1 part one\Debug\Project 1 part one.exe : fatal error LNK1120: 2 unresolved externals
    1>
    1>Build FAILED.
    1>
    1>Time Elapsed 00:00:00.57
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    And what don't you understand about those first three warnings? The number in the parentheses is the line number, 77, 85, and 85.

    Where have you implemented your writeNumber() and getNumber() functions that have the proper number and type of parameters?

    Jim

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    10
    Honestly, and sorry if this sounds dumb, but our main problem is figuring out where to put those things. We were instructed to take code out of our book and modify it. Basically what I posted is what we made it too before we decided that we were completely stumped. About the warnings though, we thought that we had properly initialized them, but alas we keep getting the errors.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Do you understand what initialized means?

    It means that you have assigned an initial value to the variable. Where before line 77 did you assign a value to N?

    Jim

  8. #8
    Registered User
    Join Date
    Mar 2014
    Posts
    10
    We initialized N in the int main() function, was that the wrong place to initialize it?

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    We initialized N in the int main() function, was that the wrong place to initialize it?
    Where? What line?

    Jim

  10. #10
    Registered User
    Join Date
    Mar 2014
    Posts
    10
    It is in line 27

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by John Addy View Post
    It is in line 27
    But you don't. You declare it, but never initialize it. You don't give it an initial value.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Mar 2014
    Posts
    10
    Code:
    //Program: Classify Numbers
    //This program counts the number of zeros, odd, and even numbers *?*/
     
    #include <iostream>
    #include <iomanip>
    #include <fstream>
     
    using namespace std;
     
     
        //Function prototypes
    void initialize(int& zeroCount, int& oddCount, int& evenCount);
    void getNumber(int& number, ifstream& inFile);
    void classifyNumber(int number, int& zeroCount, int& oddCount, 
                        int& evenCount);
    void printResults(int zeroCount, int oddCount, int evenCount);
    bool getNumber(ifstream& numberFile, int& zeros, int& odds, int& evens);
    void writeNumber(ofstream& outFile, int zeros, int odds, int evens, 
         int sum, int avg);
    int calcSum( int zeros, int evens, int odds);
     
    int main ()
    {
         
        ifstream inFile;
        ofstream outFile;
        
        int sum;
        int avg; 
         
     
         
         
            //Variable declaration
        int counter; //loop control variable 
        int number;  //variable to store the new number 
        int zeros;   //variable to store the number of zeros 
        int odds;    //variable to store the number of odd integers s
        int evens;   //variable to store the number of even integers 
        //int sum;     // sum of the numbers
        //int avg;     //average of the number
     
        initialize(zeros, odds, evens);                 //Step 1
     
        //cout << "Please enter " << N << " integers."
         //    << endl;                                   //Step 2
        //cout << "The numbers you entered are: "
         //    << endl;
     
    //numberfile.open ("f:number.dat");
     
              //  if(inFile)
                //cout<< "Open \ 'f:numberfile.dat\' " << endl;
                //else
                      
                //cout << "File not opened \ 'f:number.txt\' << endl;
                 
    inFile.open ("problem01Data.txt");
    if(inFile)
                  {                 //failure of opening number file
                  cout << "Error in opening number file .\n" << endl;
                  system ("pause");
                  return 0;
                  }
    outFile.open("f:results.dat");
    if(!outFile)
                    {                 //failure of opeinging results file
                    cout << "Error in opening results file .\n" << endl;
                     
                    system("pause");
                    return 0;
                    }
                     
    while (inFile)
    {
    	getNumber(number, inFile);
    	classifyNumber(number, zeros, odds, evens);
    	counter++;
    	sum = sum+number;
    	if (counter % 10 == 0)
    		outFile << endl;
        
    }
     
    void initialize(int& zeroCount, int& oddCount, int& evenCount)
    {
        zeroCount = 0;
        oddCount = 0;
        evenCount = 0;
    }
     
    void getNumber(int& num)
    {
        cin >> number;
    }
     
    void classifyNumber(int num, int& zeroCount, int& oddCount,
                        int& evenCount)
    {
        switch (number % 2)
        {
        case 0: 
            evenCount++;  
            if (number == 0)
                zeroCount++;  
            break;
       case 1: 
       case -1: 
           oddCount++;
           
       } //end switch
    }
     
    int calcSum( int zeroCount, int oddCount, int evenCount)
    {
    	
        return zeroCount + oddCount + evenCount;
    }
         
     
    void printResults(int zeroCount, int oddCount, int evenCount)
    { 
       cout << "There are " << evenCount << " evens, "
          << "which includes " << zeroCount << " zeros"
        << endl;
     
     cout << "The number of odd numbers is: " << oddCount
        << endl;
         
    // calcuSum
    }
    Basically we changed a little after talking to our professor and our only error left is that one of the brackets is producing an error.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you were using consistent indenting you would probably be able to see where the missing brace was located.

    Code:
    //Program: Classify Numbers
    //This program counts the number of zeros, odd, and even numbers *?*/
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    
    //Function prototypes
    void initialize(int& zeroCount, int& oddCount, int& evenCount);
    void getNumber(int& number, ifstream& inFile);
    void classifyNumber(int number, int& zeroCount, int& oddCount,
                        int& evenCount);
    void printResults(int zeroCount, int oddCount, int evenCount);
    bool getNumber(ifstream& numberFile, int& zeros, int& odds, int& evens);
    void writeNumber(ofstream& outFile, int zeros, int odds, int evens,
                     int sum, int avg);
    int calcSum( int zeros, int evens, int odds);
    
    int main ()
    {
    
       ifstream inFile;
       ofstream outFile;
    
       int sum;
       int avg;
    
    
    
    
       //Variable declaration
       int counter; //loop control variable
       int number;  //variable to store the new number
       int zeros;   //variable to store the number of zeros
       int odds;    //variable to store the number of odd integers s
       int evens;   //variable to store the number of even integers
       //int sum;     // sum of the numbers
       //int avg;     //average of the number
    
       initialize(zeros, odds, evens);                 //Step 1
    
       //cout << "Please enter " << N << " integers."
       //    << endl;                                   //Step 2
       //cout << "The numbers you entered are: "
       //    << endl;
    
       //numberfile.open ("f:number.dat");
    
       //  if(inFile)
       //cout<< "Open \ 'f:numberfile.dat\' " << endl;
       //else
    
       //cout << "File not opened \ 'f:number.txt\' << endl;
    
       inFile.open ("problem01Data.txt");
       if(inFile)
       {  //failure of opening number file
          cout << "Error in opening number file .\n" << endl;
          system ("pause");
          return 0;
       }
       outFile.open("f:results.dat");
       if(!outFile)
       {  //failure of opeinging results file
          cout << "Error in opening results file .\n" << endl;
    
          system("pause");
          return 0;
       }
    
       while (inFile)
       {
          getNumber(number, inFile);
          classifyNumber(number, zeros, odds, evens);
          counter++;
          sum = sum+number;
          if (counter % 10 == 0)
             outFile << endl;
    
       }
    
       void initialize(int& zeroCount, int& oddCount, int& evenCount)
       {
          zeroCount = 0;
          oddCount = 0;
          evenCount = 0;
       }
    
       void getNumber(int& num)
       {
          cin >> number;
       }
    
       void classifyNumber(int num, int& zeroCount, int& oddCount,
                           int& evenCount)
       {
          switch (number % 2)
          {
             case 0:
                evenCount++;
                if (number == 0)
                   zeroCount++;
                break;
             case 1:
             case -1:
                oddCount++;
    
          } //end switch
       }
    
       int calcSum( int zeroCount, int oddCount, int evenCount)
       {
    
          return zeroCount + oddCount + evenCount;
       }
    
    
       void printResults(int zeroCount, int oddCount, int evenCount)
       {
          cout << "There are " << evenCount << " evens, "
               << "which includes " << zeroCount << " zeros"
               << endl;
    
          cout << "The number of odd numbers is: " << oddCount
               << endl;
    
          // calcuSum
       }
    Jim

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>if(inFile)
    This is wrong.

    Indent properly and you'll see your other error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Mar 2014
    Posts
    10
    The bracket on like 86 is what is causing the big problems for us.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Average of numbers in a file
    By starghost3 in forum C Programming
    Replies: 1
    Last Post: 12-14-2011, 03:20 PM
  2. Replies: 13
    Last Post: 03-30-2011, 01:44 AM
  3. Help C program that reads Odd numbers and evens
    By Cyberman86 in forum C Programming
    Replies: 4
    Last Post: 02-27-2009, 11:59 AM
  4. C++ Program that Calculates average of three scores
    By dccog in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2002, 12:03 AM
  5. A program which reads the first characters in a file
    By Zewu in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2002, 03:24 PM

Tags for this Thread