Thread: Returning values from functions

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Returning values from functions

    I'm having a lot of trouble figuring out how to pass values back and forth between functions.

    In the program I'm working on now, I'm supposed to declare an integer array (n[15000]) and an integer (count) in main, then pass those to a function which opens a txt file and then starts a loop. The loop takes the values in the txt file and stores them in n[], while count keeps track of how many times the loop runs. At the end the function should pass n[] and count back to main, then main prints the results. That's where I'm having trouble.

    Here's a sample of my code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int ReadFile(int n[], int count);
    
    int main()
    {
    	int n[15000];
    	int count = 0;
    
    	ReadFile(n, count);
    
    	cout << "There are " << count << " numbers in the file.\n";
    	cout << "The first number is " << n[0] << endl;
    
    	return 0;
    }
    
    int ReadFile(int n[], int count)
    {
    	ifstream input;
    	input.open("Numbers.txt");
    	if(!input)
    	{
    		cout << "Cannot open file" << endl;
    		return 0;
    	}
    	while (!input.eof())
    	{
    		input >> n[count];
    		++count;
    	}
    	input.close();
    
    	return n, count;
    }
    For some reason n[] makes it through and displays the correct number, but count always shows up as 0. Can anyone explain what I'm doing wrong?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to pass count as a reference,
    Code:
    int ReadFile(int n[], int &count);
    Since all C arrays are passed as "the address of", it will work fine for the array, but non-array parameters are passed by value, which means your function gets a copy of the variable, rather than the real value. And just like writing on a copy of a document, it doesn't change the original.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while (!input.eof())
    Also read the FAQ as to why this doesn't work as you expect.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Okay cool, everything seems to be working now.

    Thanks for the fast responses!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stat() returning same values all the time
    By pamplemoose in forum C Programming
    Replies: 6
    Last Post: 02-24-2008, 10:24 PM
  2. Returning Values in C
    By cuba06 in forum C Programming
    Replies: 6
    Last Post: 11-21-2007, 01:23 AM
  3. Returning strings from functions
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 08-31-2007, 09:07 AM
  4. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  5. Returning Multiple Values from a function
    By Eddie K in forum C Programming
    Replies: 6
    Last Post: 03-12-2006, 01:18 PM