Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating and implementing functions (C++).
#1
Okay, I've tried to understand functions but it's hard. This assignment has me stuck on it. I was able to do most of this without the need of functions, but once I realized I needed to implement functions, I had to change what I was doing and now it's all wrong.

This is obviously not the whole program, but if I can figure out how the hell I need to work functions, I should have the rest down.

Code:
#include <iostream>
#include <fstream>
using namespace std;

//Function Declarations

// -----------------------------------------------------------------------
// Return the number of values in input file (already open).
// -----------------------------------------------------------------------
int InputCount (ifstream &inF);

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

//Main function.

int main()
{

// ----------------------------------------------------------------------
//  Declare variables.
// ----------------------------------------------------------------------
// FORMAT: <type> <variable>;  // Declaration comment.
    
    int valueCount = 0;      //I doubt I had to declare this variable twice but.
    float value, square;
    float sum = 0;
    float sqSum = 0;

//-| ----------------------------------------------------------------------
//-| 1. Read names of input and output files.
//-| ----------------------------------------------------------------------
  
    ifstream inF;
    ofstream outF;

//-| ----------------------------------------------------------------------
//-| 2. Open input file. If this fails, terminate the program.
//-| ----------------------------------------------------------------------
  
    inF.open("numbersIn.txt");    //Opens input file.

    if(inF.fail())
    {
        return 1;                //Terminates program if file is not found.
    }

//-| ----------------------------------------------------------------------
//-| 3. Open output file.
//-| ----------------------------------------------------------------------

    outF.open("numbersOut.txt"); //Opens output file.

//-| ----------------------------------------------------------------------
//-| 4. Count the number of values in the input file and calculate the sum.
//-| ----------------------------------------------------------------------

    int InputCount (&inF);

    //Print results
    cout << valueCount << " VALUES IN FILE" << endl;

    system("pause");
    return 0;
} //main
Reply


Messages In This Thread
Creating and implementing functions (C++). - by MariettaRC - 2013-09-15, 10:57 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)