2013-09-15, 10:57 PM
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.
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
