I have been working on a program for class- yes this is homework. I have completed the program, it runs- yay newbie me- but it does not run correctly. I am working with functions, wether referenced or not, and I have passed several values to a function inorder to calculate the cost of a shirt based on height and weight. However, my if statement is not going through. It would appear that the statement take the first value given and then ignores the rest leaving my future calcuations based on the price of said shirt in error. The correctness of the whole program rests on this if statement issue... I am also struggling with a repeated output at the end of my loop, but that is of much less consequence. If someone could look it over and help me see where I have screwed it up I would be most grateful! Its not much of a trade off, my gratitude for the use of your brain... but nonetheless I will still be grateful! Here is my code and at the bottom I have included the info you need for the text file input. Call it whatever you like- the program takes the filename through a cin :)
/*
"cs150_Prj3.cpp
"Programmer: Kellen Berry"
"Date: 10/28/2012"
"CRN 14086"
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <string>
double shirt_cost(double,int ,int , double , int);
double shirt_cost_red(double ,int ,int , double , int);
double cost_total_uniform(double, int);
double avg_player_weight(double, int);
void program_info();
using namespace std;
int main()
{
string player_num ,fname, lname, POS, year, where_from;
char dash, where_from_ltr;
int feet, inches, red_shirts, total_players;
double Weight,jersey_cost, avg_weight, uniform_cost;
int count = -0;//number of mnames in the file
int looper = 0;
int cycle = 0; //number of red shirts
int loc ; // first letter in the where_from variable
int regular_cost=0, regular_cost_red=0;
string filename; // name of the input file
ifstream inFile; //input file stream variable
ofstream outFile;
cout << "Enter the name of the input file: " ;
cin >> filename;
inFile.open(filename.c_str () );
if (!inFile)
{
cout << "bummer file name \n\n";
return 1;
}
cout << filename << " was opened" << endl<<endl;
// ignore the first line
inFile.ignore ( 200,'\n');
outFile.open("filename.out", ios::out);
if (outFile.fail())
{
cout << "The output file could not be created" << endl;
return(1);
}
while ( !inFile.eof())
{
// assigns all the values to the variables based on the file read in
inFile >>player_num >> fname >> lname >> POS >> year >> feet >> dash >> inches >> Weight >> where_from_ltr;
getline ( inFile, where_from ); // created the where_fromstring correctly
where_from = where_from_ltr + where_from;
cout << "NO " << player_num << endl;
cout << "Name " << fname <<" " << lname << endl;
cout << "POS " << POS << endl;
cout << "Class " << year << endl;
cout << "Height " << feet << dash << inches << endl;
cout << "Weight " << Weight << endl;
cout << "Home " << where_from << endl;
string lnames (lname);
cout << "The are " << lnames.size() << " letters in your last name.\n\n";
string NO (player_num);
cout << "There are " << NO.size() << " numbers on your jersey.\n\n";
jersey_cost = (NO.size() * 5.0) + (lnames.size() * 2.0);
loc = year.find ("RS"); // this is the switch statement that determines which function to use for shirt costs
if ( loc>= 0)
{
cycle++;// starts the cycle for the number of red_shirts given
regular_cost_red=shirt_cost_red(Weight,feet,inches, jersey_cost, count);
uniform_cost = regular_cost_red + jersey_cost;
}
else if
{
regular_cost=shirt_cost(Weight,feet,inches, jersey_cost, count);
uniform_cost = regular_cost + jersey_cost;
}
count++;
}
inFile.close();
outFile.close();
red_shirts = cycle;
cout << "\nThat would be all the players in this file and below are a few totals and averages" << endl<< endl;
cout << "\t “\ODU Football 2012”\ "<< endl << endl;
cout << "There were " << count << " names in the file. " << endl;
cout<<"The were " << red_shirts <<" red shirts in the file."<<endl;
avg_player_weight(Weight,count);
cost_total_uniform(uniform_cost,count);
cout<<"this is the shirt price im returning for the regular cost variable"<< regular_cost
<< "and the regular_cost_red variable returns " << regular_cost_red << endl;
program_info();
return 0;
}
// This function is the one I am having issues with- the if statement is not working
double shirt_cost(double Weight,int feet,int inches, double jersey_cost, int count )
{
double shirt_price=0, uniform_cost=0;if(feet <= 6)
if(feet <= 6 || (feet == 6 && inches == 0))
{
shirt_price=200.00;
}
else if(6 < feet && inches <= 4 && Weight <= 260)
{
shirt_price=225.00;
}
else if(Weight > 260 || (feet > 6 && inches > 4))
{
shirt_price=255.00;
}
shirt_price = shirt_price * 4.0;
uniform_cost = shirt_price + jersey_cost;
cout << setw (14)<<"Shirt cost \t$" << shirt_price << endl <<endl;
cout << setw (14)<<"Jersey cost \t$" << jersey_cost << endl;
cout << setw (14)<<"\t" << "_ _ _ _ _" << endl;
cout << setw (14)<<"Uniform cost \t$" << uniform_cost << endl << endl;
int calculaated= shirt_price;
return calculaated;
}
// Same for this function- the if statement is not working
double shirt_cost_red(double Weight,int feet,int inches, double jersey_cost, int count)
{
double shirt_price=0, uniform_cost=0;
int cycle = 0;// starts the cycle for the number of red_shirts given
if(feet <= 6 || (feet == 6 && inches == 0))
{
shirt_price=200.00;
}
else if(6 < feet && inches <= 4 && Weight <= 260)
{
shirt_price=225.00;
}
else if(Weight > 260 || (feet > 6 && inches > 4))
{
shirt_price=255.00;
}
shirt_price = shirt_price * 2.0;
uniform_cost = shirt_price + jersey_cost;
cout << setw (14)<<"Shirt cost \t$" << shirt_price << endl << endl;
cout << setw (14)<<"Jersey cost \t$" << jersey_cost << endl;
cout << setw (14)<<"\t" << "_ _ _ _ _" << endl;
cout << setw (14)<<"Uniform cost \t$" << uniform_cost << endl << endl;
int calculaated= shirt_price;
return calculaated;
}
double cost_total_uniform(double uniform_cost, int count)
{
int total_uniform_cost = 0;
int looper = 0;
while (looper <= count)
{
total_uniform_cost = total_uniform_cost + uniform_cost;
looper++;
}
cout << "This is how much all the uniforms for the team will cost $" << total_uniform_cost << endl;
return total_uniform_cost;
}
double avg_player_weight(double Weight, int count)
{
double total_weight=0;
double avg_weight=0;
int looper =0;
while(looper <= count)
{
total_weight = total_weight + Weight;// to setup weight of the whole team, this needs to be an array loops
looper++;
}
cout<<"The total weight of players processed is " << total_weight<<"."<<endl;
avg_weight = total_weight/ count;
cout<< "The average weight of all the players is " << avg_weight <<" pounds." << endl;
}
void program_info()
{
cout << endl<<endl;
cout<< "cs150_wk7_B.cpp"<<endl;
cout << "Programmer: Kellen Berry" <<endl;
cout << "Date: 10/18/2012" <<endl;
cout << "CRN 14086" <<endl <<endl;
}
THE TEXT FILE:
NO NAME POS CLASS HEIGHT WEIGHT Hometown/High School/Last College
60 Josh Mann OL SO 6-4 300 Virginia Beach, Va./Ocean Lakes
64 Ricky Segers KP FR 5-11 185 Glen Allen, Va./Henrico
70 Brandon Carr OL RS_SR 6-2 305 Chesapeake, Va./Western Branch/Fork Union Military Academy
53 Calvert Cook LB FR 6-0 250 Norfolk, Va./Booker T. Washington
51 Michael Colbert DE RS_SR 6-1 230 Fayetteville, N.C./E.E. Smith
22 T.J. Cowart CB RS_JR 5-9 190 e Virginia Beach, Va./Ocean Lakes
1 Jakwail Bailey WR SO 5-11 185 Haddonfield, N.J./Paul VI
25 Andre Simmons S JR 6-0 205 Lorton, Va./South County/Vanderbilt
34 Johnel Anderson RB FR 5-8 180 Sicklerville, N.J./Paul VI
Thank you all in advance hugs