The program works up until i need to print the total resistance of the elastic bands using a function. I dont even know if the function is right because i have to return the value to the main and i do not know how to display to my out file. It is a homework assignment and here is the part im stuck.
my setupbands.txt contains the following:
yellow 5.0 6.0
green 6.5 7.5
red 10.5 12.0
blue 15.0 18.0
black 22.0 25.0
inputbands.txt
green
red
black
stop
"Call the total resistance function to find the average resistance for the bands selected. Do so by first adding the lows and highs separately, then dividing by 2.0. Do not find the average for each band. Return the average to the main and write it in the main using format the following width XXX.D."
As i said everything works up until getting the total resistance to display.
Any help is appreciated and i hope i gave enough information.
CODE
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
enum bandtype {YELLOW, GREEN, RED, BLUE, BLACK};
struct bandrecord
{
string bandcolor;
float low, high;
bool is_used;
};
void setup (ifstream &, bandrecord[]);
void getbands (ifstream &, bandrecord[]);
void printbands (ofstream&, const bandrecord[]);
float totalresistance (const bandrecord[]);
bandtype str_to_color(const string &);
int main ( void )
{
ifstream setupdata;
ifstream inputbands;
ofstream out;
bandrecord mylist[BLACK + 1];
setupdata.open("setup.txt");
if(!setupdata)
{
cout<<"Failure to open file"<<endl;
return 1;
}
inputbands.open("inputbands.txt");
if(!inputbands)
{
cout<<"Failure to open file"<<endl;
return 1;
}
out.open("out.txt");
if(! out)
{
cout<<"Failure to open out.txt"<<endl;
return 1;
}//open files
out<<"Band Color"<<" "<<"High Weight"<<" "<<"Low Weight\n"<<endl;
setup(setupdata,mylist);
getbands(inputbands, mylist);
printbands(out,mylist);
totalresistance (mylist);
out<<"\nTotal Resistance"<<" "<<endl;
cout<<"End of Program\n";
return 0;
}
void setup (ifstream & setup2, bandrecord mylist[])
{
for(bandtype band=YELLOW; band<=BLACK; band=bandtype(band +1))
{
setup2>>mylist[band].bandcolor>>mylist[band].low>>mylist[band].high;
mylist[band].is_used=false;
}
}
void getbands (ifstream & setup2, bandrecord mylist[])
{
string colorname;
setup2>>colorname;
while(colorname != "stop")
{
bandtype index = str_to_color (colorname);
mylist [index].is_used = true;
setup2>>colorname;
}
}
bandtype str_to_color(const string & name)
{
bandtype temp;
switch (name[0])
{
case 'b': switch (name[2])
{
case 'u': temp=BLUE;
break;
case 'a': temp=BLACK;
break;
}
case 'y': temp=YELLOW;
break;
case 'g': temp=GREEN;
break;
case 'r': temp=RED;
break;
}
return temp;
}
void printbands (ofstream& out, const bandrecord mylist[])
{
for(bandtype band=YELLOW; band<=BLACK; band=bandtype(band +1))
if(mylist[band].is_used)
out<<mylist[band].bandcolor<<setw(15)<<mylist[band].high<<setw(15)<<mylist[band].low<<endl;
}
float totalresistance (const bandrecord mylist[])
{
float lowtotal, hightotal;
for(bandtype band=YELLOW; band<=BLACK; band=bandtype(band +1))
if(mylist[band].is_used)
{
lowtotal=lowtotal+mylist[band].low;
hightotal=hightotal+mylist[band].high;
}
return ((hightotal+lowtotal)/2.0);
}