Thread: Function-change ouput to a file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    61

    Cool Function-change ouput to a file

    I hope I did the code tags correctly:
    1st I need to change the output to a file
    2nd additional programming to find the sum and average of the number
    3rdchange print result to print to a file
    This is not an assigned homework just another example I want to try to figure out.
    //code
    Code:
    //Program: Classify Numbers
    //This program counts the number of zeros, odd, and even numbers 
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    const int N = 20;   
    
        //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);
    
    int main ()
    {
            //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 
        int evens;   //variable to store the number of even integers 
    
        initialize(zeros, odds, evens);                 //Step 1
    
        cout << "Please enter " << N << " integers."
             << endl;                                   //Step 2
        cout << "The numbers you entered are: "
             << endl;
    
        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 
    
        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
    }
    
    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;
    }
    //code
    Last edited by BJtoVisualcC++; 06-14-2007 at 12:44 PM. Reason: Fixing the code tags - AGAIN - last chance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM