input/output file stream variables as function parameters?

This is a discussion on input/output file stream variables as function parameters? within the C++ Programming forums, part of the General Programming Boards category; This is my main function for a program with two subfunctions (one to get user input of numbers that need ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Poughkeepsie, NY
    Posts
    5

    input/output file stream variables as function parameters?

    This is my main function for a program with two subfunctions (one to get user input of numbers that need to be averaged and another that averages the numbers):
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    
    
    using namespace std;
    
    
    void getNumbers (double num, int& count, char yn, ofstream outData);
    double averageNumbers (double num1, double sum, double average, int counter, ifstream userData);
    
    
    int main ()
    {
        string sentinel;
        double number, sum, average;
        int counter;
        char yn;
        ofstream outData;
        ifstream userData;
    
    
        cout << "To continue with the program enter 'continue'. Otherwise enter 'STOP'";
        cin >> sentinel;
    
    
        if (sentinel != "STOP")
            {
            getNumbers (number, counter, yn, outData);
            
            averageNumbers (number, sum, average, counter, userData);
            cout << "The numbers you entered are: " << userData <<endl;
            cout << "The average of the numbers is: " << average <<endl;
    
    
            cout << "To continue with the program enter 'continue'. Otherwise enter 'STOP': ";
            cin >> sentinel;
        }
    
    
        return 0;
    }
    The problem is, my compiler is not letting me use input/output file stream variables as parameters for my functions. It is not 100% necessary that I use functions in this program but I want to impress the teacher and having all these operations enclosed in the function main is like just meeting expectations..not exceeding them like I want to. Is there a way to get around this?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,555
    Pass the streams by reference; they cannot be copied.
    Also, you are passing a lot of uninitialised data into functions. You should either pass them by reference if you mean to modify them in the function and reflect it in main, initialize the data before passing them or simply move the declarations into the functions.
    Last edited by Elysia; 03-21-2013 at 12:02 PM.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Poughkeepsie, NY
    Posts
    5
    Yeah, I removed some of the uninitialized variables shortly after I copied that code onto here. But I'm still not sure how to pass the files by reference into the functions.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    What have you tried?

    Show some code where you are trying to send these parameters by reference. You already seem to know about passing by reference since you are already doing it in one of your functions.

    You may want to study the following tutorials for more hints: Functions I and Functions II

    Jim

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by physicsandpink View Post
    But I'm still not sure how to pass the files by reference into the functions.
    You don't seem to have any difficulties in passing the counter variable by reference to your getNumbers function. Try to apply the same method for the ofstream parameter.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Mar 2013
    Location
    Poughkeepsie, NY
    Posts
    5
    this is the entire code so far. everything seems to be working except the file stream variables.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    
    
    using namespace std;
    
    
    void getNumbers (double num, int& count, char yn, ofstream& outData);
    double averageNumbers (double num, double sum, double average, int count, ifstream userData);
    
    
    int main ()
    {
    	string sentinel;
    	double num, sum, average;
    	int count;
    	char yn;
    	ofstream outData;
    	ifstream userData;
    
    
    	cout << "To continue with the program enter 'continue'. Otherwise enter 'STOP'";
    	cin >> sentinel;
    
    
    	if (sentinel != "STOP")
    		void getNumbers (double num, int& count, char yn, ofstream& outData);
    		double getAverage (double num, double sum, double average, int count, ifstream userData);
    
    
    		cout << "The numbers you entered are: " << userData <<endl;
    		cout << "The average of the numbers is: " << average <<endl;
    
    
    		cout << "To continue with the program enter 'continue'. Otherwise enter 'STOP': ";
    		cin >> sentinel;
    	}
    
    
    	return 0;
    }
    
    
    
    
    
    
    void getNumbers (double num, int& count, char yn, ofstream& outData)
    {
    	ofstream outData;
    		outData.open("numbers.txt");
    
    
    	count = 0;
    	 
    	cout << "Would you like to average some numbers? ";
    	cout << "Enter Y for yes or N for no." <<endl;
    	cin >> yn;
    	
    	do 
    	{
    		cout << "Enter a number you wish to average: ";
    		cin >> num;
    		outData << num << " ";
    		count++;
    		
    		cout << "Would you like to enter another number? " ;
    		cout << "Enter Y for yes or N for no." <<endl;
    		cin >> yn;
    	}
    	while (yn != 'N');
    	outData.close();
    }
    
    
    double averageNumbers (double num1, double sum, double average, int count, ifstream userData)
    {
    	ifstream userData;
    	userData.open("numbers.txt");
    	sum = 0;
    
    
    	while (!userData.eof())
    	{ 
    		cin >> num;
    		sum = sum + num;
    	}
    
    
    	average = sum/count;
    	return average;
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,713
    Code:
     
        if (sentinel != "STOP")
            void getNumbers (double num, int& count, char yn, ofstream& outData);
            double getAverage (double num, double sum, double average, int count, ifstream userData);
    Is that really the way you try to call functions? Read the links I posted in my last post!

    this is the entire code so far. everything seems to be working except the file stream variables.
    And what seems to be wrong with the stream variables? Other than you never use the stream variables you passed into your functions.
    Code:
    void getNumbers (double num, int& count, char yn, ofstream& outData)
    {
        ofstream outData;  // This creates a new local instance of your stream.
            outData.open("numbers.txt");
    Jim

  8. #8
    Registered User
    Join Date
    Mar 2013
    Location
    Poughkeepsie, NY
    Posts
    5
    Quote Originally Posted by jimblumberg View Post

    Is that really the way you try to call functions? Read the links I posted in my last post!


    And what seems to be wrong with the stream variables? Other than you never use the stream variables you passed into your functions.
    Code:
    void getNumbers (double num, int& count, char yn, ofstream& outData)
    {
        ofstream outData;  // This creates a new local instance of your stream.
            outData.open("numbers.txt");
    Jim

    Okay, I see whats wrong with the function calls. I thought I was following the textbook but I forgot that we dont need the data type of the function listed before the function calls. I'm probably doing something else wrong too but I dont have visual studio on this particular computer so I can't check and see if it compiles once I remove the data types from the function calls.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Location
    Poughkeepsie, NY
    Posts
    5
    Actually, I originally had tried to call the functions without putting the data types of the functions in front of the function calls. (See first post). I dont know how those data types snuck back in there. But that means something else IS still wrong and I dont know what it is

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,555
    Your first code is wrong because you don't pass the streams by reference.
    The second code is wrong because you aren't calling the functions properly and you redeclare the parameters as local variables (don't declare the local variables).
    If you can't fix the problem with these two advices, then do your best effort and post the updated code.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-11-2011, 08:44 PM
  2. File Input Stream constructor
    By yes2 in forum C++ Programming
    Replies: 0
    Last Post: 12-15-2010, 09:59 PM
  3. Replies: 9
    Last Post: 05-03-2007, 03:50 PM
  4. Replies: 3
    Last Post: 10-23-2006, 06:37 PM
  5. Using SSCANF input string output two variables
    By kevndale79 in forum C Programming
    Replies: 9
    Last Post: 10-02-2006, 02:57 PM

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