Function-change ouput to a file

This is a discussion on Function-change ouput to a file within the C++ Programming forums, part of the General Programming Boards category; I hope I did the code tags correctly: 1st I need to change the output to a file 2nd additional ...

  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

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    And what is the problem ? What's your question ?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> I hope I did the code tags correctly:
    Obviously not. Use the "Go Advanced" and "Preview Post" buttons to check before you make a reply.

    For now, you can edit your post (click the "Edit" button at the bottom of your post). You need to put the [CODE] At the top of the code and the [/code] at the bottom. Make sure the [ in front of CODE is there. Then keep previewing until it looks correct, something like this:

    Code:
      // All my code here.
    Edit: It looks like you were given explicit instructions on how to use code tags already in another thread. Please make an effort to make things easier on those of us try to help.
    Last edited by Daved; 06-14-2007 at 09:14 AM.

  4. #4
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    You should read some of the tutorials on this site. I'm sure they can help you understand your problem.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    I dont see an issue in it, very nicely done

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    Yes Thank you but what is some code to change output to a file? Assign a file and have a file name already set up--what type of extension does it need to be?
    Where in the code is it best to get th?e totals

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You can give a file any extension you like. You might want to make it a .txt if you want to notepad to open it for you by default tho.

    Heres an example of how to write to a file in c++:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() 
    {
      ofstream afile;
      afile.open("example.txt");
      afile << "Gdnfsbfbkjdbd dgbfsdb.\n";
      afile.close();
    }
    Last edited by mike_g; 06-14-2007 at 01:13 PM.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    To give the option to print to the screen or a file you could change printResults() to this:
    Code:
    void printResults(ostream &out, int zeroCount, int oddCount, int evenCount)
    { 
        out << "There are " << evenCount << " evens, "
             << "which includes " << zeroCount << " zeros"
             << endl;
    
        out << "The number of odd numbers is: " << oddCount
             << endl;
    }
    The if you want to print to the screen:
    Code:
        printResults(cout, zeros, odds, evens);               //Step 4
    And to a file:
    Code:
    #include <fstream>
    .
    .
        string filename = "file.txt";
        ofstream out(filename.c_str());
        if (!out.is_open())
        {
            cout << "Unable to open file: " << filename << endl;
            return 1;
        }
        printResults(out, zeros, odds, evens);               //Step 4
    You can even print to both:
    Code:
    #include <fstream>
    .
    .
        printResults(cout, zeros, odds, evens);               //Step 4
    
        string filename = "file.txt";
        ofstream out(filename.c_str());
        if (!out.is_open())
        {
            cout << "Unable to open file: " << filename << endl;
            return 1;
        }
        printResults(out, zeros, odds, evens);               //Step 4
    Last edited by swoopy; 06-14-2007 at 05:35 PM.

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, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21