Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating and implementing functions (C++).
#7
MariaColette Wrote:Ahh, that helps a lot, thanks! I still have another problem, though...

I've got errors stating that value is an undeclared variable, and I can see why now. But isn't there a way for the program to find it initialized outside of the function or would I have to declare these variables within the function?

EDIT: I ask this because the basic format of our program has the variable declaration inside the main function, and it sort of feels like I'll have all of my declarations within my declared functions.

If you want to use a variable that you declared in main in some other function, you'll need to pass that variable. If you need to make changes to the value of that variable and you want those changes to also take place in main, then pass your variable by reference.

Code:
int InputCount (ifstream &inF, float &value)
{
    int valueCount = 0;        //Initialize the value count at 0.
    while (inF >> value)    
    {
        valueCount += 1;    //Increment value count.
    }
    return valueCount;
}

int main()
{
    float value;
    InputCount(inF, value)

-insert rest of code here-
}

Sorry if I screwed up the syntax, but I think its right...
Reply


Messages In This Thread
Creating and implementing functions (C++). - by Quickjumper7 - 2013-09-16, 01:04 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)