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);
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
}
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.
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).