Functions (Pass by reference/value)

Attempting to edit a code to utilize a function instead of previously used method to calculated percentage of failures. Objective is to use only one function. However, I cannot figure it out. The best I was able to reason was with 4 functions. I will be posting the original code as well as my 4 function (wrong) attempt and exclude the cout/outfile portions. Thank you in advance.

[Original code]
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main ()

{int ftemp(0), fpres(0), ftime(0), fbatch(0), count(0); //need to declare variables for counters of failed values

double batch, temp, pres, time; //need to declare doubles for values being inputted from file

ifstream infile;

infile.open("abc.txt"); //open file with data

while (infile>>batch>>temp>>pres>>time) //order of reading values in file

{
if (temp<150 || temp>170) //temp unacceptable range
ftemp++; //failed temp counter

if (pres<60 || pres>70) //pressure unacceptable range
fpres++; //failed pressure temp counter

if (time<2.00 || time>2.50) //time unacceptable range
ftime++; //failed time counter

if (temp<150 || temp>170 || pres<60 || pres>70 || time<2.00 || time>2.50) //find any unacceptable peice of data

fbatch++; //unacceptable batch counter
if (batch>0)
count++; //counter value increase as batch value is accepted

}

double tep, pp, tip, bp; //initiate percentage values
tep = (ftemp/25.0)*100; //formula for percentage
pp = (fpres/25.0)*100;//formula for percentage
tip = (ftime/25.0)*100;//formula for percentage
bp = (fbatch/25.0)*100;//formula for percentage

infile.close(); //done with infile

return 0;

}
[/Original code]






[Attempt Code]

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int FailPerTemp (double failtemp);
int FailPerPres (double failpres);
int FailPerTime (double failtime);
int FailPerBatch (double failbatch);

int main ()
{
int ftemp(0), fpres(0), ftime(0), fbatch(0), count(0);

double batch, temp, pres, time;

double ftempfail, fpresfail, ftimefail, fbatchfail;

ifstream infile;

infile.open("abc.txt"); //open file with data

while (infile>>batch>>temp>>pres>>time) //order of reading values in file
{
if (temp<150 || temp>170) //temp unacceptable range
ftemp++; //failed temp counter

if (pres<60 || pres>70) //pressure unacceptable range
fpres++; //failed pressure temp counter

if (time<2.00 || time>2.50) //time unacceptable range
ftime++; //failed time counter

if (temp<150 || temp>170 || pres<60 || pres>70 || time<2.00 || time>2.50) //find any unacceptable peice of data

fbatch++; //unacceptable batch counter
if (batch>0) //counter
count++; //counter value increase as batch value is accepted
}

ftempfail= FailPerTemp(ftemp);
fpresfail= FailPerPres(fpres);
ftimefail= FailPerTime(ftime);
fbatchfail= FailPerBatch (fbatch);

infile.close(); //done with infile

}

int FailPerTemp (double failtemp)
{
double calcftemp;
calcftemp=failtemp/25*100;
}

int FailPerPres (double failpres)
{double calcfpres;
calcfpres=failpres/25*100;
}

int FailPerTime (double failtime)
{double calcftime;
calcftime=failtime/25*100;
}

int FailPerBatch (double failbatch)
{double calcfbatch;
calcfbatch=failbatch/25*100;
}


[/Attempt Code]
1
2
3
4
5
6
7
8
int FailPerTemp (double failtemp)
{
double calcftemp;
calcftemp=failtemp/25*100;
}

// ... and the rest

You should read the tutorial on functions http://www.cplusplus.com/doc/tutorial/functions/

int FailPerTemp (double failtemp); means that it the function takes in a double as a parameter and returns an int. But none of your functions return anything, much less a double value.

You probably want something like this:
1
2
3
4
5
6
7
8
9
double FailPerTemp(double failtemp); // function prototype

// ...

double FailPerTemp (double failtemp)
{
    double calcftemp = failtemp/25.0*100.0;
    return calcftemp;
}
Thanks for the link! i'll check it out.

Would the same concept apply if I want to use all failed values(based on the counter) in one function for their respective percentages?
Not exactly sure what you mean, but a function can only return one value. So you can't return multiple values. Otherwise, yes, a function will work as intended as long as you return what is needed (or make the type of the function "void" if you don't want to return anything).
Topic archived. No new replies allowed.