Thread: classify number-to file not screen

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    61

    Cool classify number-to file not screen

    I am trying to make this change to read and write to a file with the file length unknown and to add into the program the sum and average of the number read from the file


    Program: Classify Numbers
    This program counts the number of zeros, odd, and even numbers *?*/

    Code:
    #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 evens, int odds, int sum, int avg);
    void calcSum( int zeros, int evens, int odds);
    
    int main ()
    {
    
        ifstream numberFile;
        ofstream resultsFile;
        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;
    
        numberFile.open ("f:number.dat");
        if(numberFile)
        { //failure of opening number file
            cout << "Error in opening number file .\n" << endl;
            system ("pause");
            return 0;
        }
        resultsFile.open("f:results.dat");
        if(!resultsFile)
        { //failure of opeinging results file
            cout << "Error in opening results file .\n" << endl;
    
            system("pause");
            return 0;
        }
    
        while ( getNumber (numberFile, 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(resultsFile, 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 &#37; 2)
        {
        case 0:
            evenCount++;
            if (num == 0)
                zeroCount++;
            break;
        case 1:
        case -1:
            oddCount++;
    
        } //end switch
    }
    
    int calcSum( int zeroCount, int oddCount, int evenCount)
    {
        calcuSum =(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
    }
    Last edited by laserlight; 06-17-2007 at 10:22 AM. Reason: Indented code within code forum bbcode tags.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oops I thought you were referring to the old question you posted about writing to files. Well, the first thing I noticed with this was that you were trying to assign a value to the function, same problem you had with your triangle prog.

    Also one of your functions differs from the prototype:
    Code:
    void calcSum( int zeros, int evens, int odds);
    
    int calcSum( int zeroCount, int oddCount, int evenCount)
    {
    calcuSum =(zeroCount + oddCount + evenCount);
    }
    In C++ you dont need to name the variables in your prototype, so you can declare your prototype like:
    [code]int calcSum(int, int, int);[code]

    I changed your function from void to int as it looks like you want to be returning a total here.Your function should then look something like:
    Code:
    int calcSum( int zeroCount, int oddCount, int evenCount)
    {
        return zeroCount + oddCount + evenCount;
    }
    With those changes I am now getting error messages: undefined references to getNumber , and writeNumber. I'll look into that later if no-one else here helps out first.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you define
    void getNumber(int& num);
    but not
    bool getNumber(ifstream& numberFile, int& zeros, int& odds, int& evens);

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    Please help mike_g

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    Mike you may have trouble if you have not made the two files to retrieve and send data

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    void writeNumber(ofstream& resultsFile, int zeros, int evens, int odds, int sum, int avg);
    writeNumber(resultsFile, zeros, odds, evens, sum, avg);
    see any differences? (evens/odds)

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    no what difference? no wait take the void off?--may be stupid but I am new and I am trying

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    void writeNumber(ofstream& resultsFile, int zeros, int evens, int odds, int sum, int avg);
    writeNumber(resultsFile, zeros, odds, evens, sum, avg);
    they're switched.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    robwhit I did as you said and it comes immediately with an error saying odds not declared

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    robowhit earlier statement what?

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    mike_g I changed the type at the end of the program to int calculate sum but does it need to change when you delcare it at the top of the program

  12. #12
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    now the program is saying the bottom calculate sum is undelcared?

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    post the modified code.

    ps - you may want to use the edit function in the message board for when you have a little something additional to say.

  14. #14
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    I found the calcusum it was misspelled should be calcsum and it still has an error

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    thanks for finding that out of order sequence

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM