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

for line 121 you have double shirt_price=0, uniform_cost=0;if(feet <= 6). What is the if doing at the end of the line? This will have an impact on all of your other if statements.

um... yes I do see that there but that is actually a new error... ive been having trouble with the statement for much longer than that has been there. See I updated the first statement from if (feet <= 6) to the line you see below it line 122. if you pull that out of there, my mistake, you will see that the calculations are not correct regardless of it deletion.

line 126:else if(6 < feet && inches <= 4 && Weight <= 260) the 6 < feet need to be switched around.

NathanOliver is right.. that if(feet <=6) will negate your remaining if statement if feet is not less than or equal to 6 from the start. you need to remove it.

line 85: needs a space between loc and > if ( loc>= 0) I can't remember if c++ will read that correctly or not. I know inf c# it will flag it as an error.

line 151: int cycle = 0;// starts the cycle for the number of red_shirts given
this is declared but never used. It will not have any effect on your program.

Perhaps it is an issue with the passing of values? The space in between loc and the "<" made no difference... The more I look at it I wonder if its not that the if-statement isnt working but that my variable isnt passing back the correct value? I have been assuming my if-statment was written incorrectly. What could be other possible solutions??

Have you steped through your code to see what the values are? You could also put in cout statements to print out the values in the function to see if the data is correct.

yep.. stepping through your code is the best approach. use your watch window to keep an eye on your variables.

what compiler/ide are you using? I would like to load this code and step through it. it's hard to try and think through it without testing it.

I'm using code::blocks for now because that what our teacher wanted us to use but I'm assuming that you could use any c++ compiler right? Please feel free to run it and take a look- ive been working out kinks for a week now and I'm getting pretty sick of my program... :/

ok.. I had a chance to get back to this and looking at your output it's working as expected.

Your first condition says if(feet <= 6)... this condition will always return true because based on your output you have no heights above 6 feet and you are using an OR (||) clause. An OR clause executes if one side OR the other side of the statement is true. Perhaps you meant to use an AND (&&) statement? AND executes the code block only if BOTH sides of the equation are true.

Without knowing what the business rules/conditions are I can't give you a concrete 'if logic' answer, which I shouldn't, because it's your homework and you have to struggle like we all do/did! :)

hope this helps!

Thanks! Ive been working on this for awhile I guess that obvious error escaped my attention! Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.