0

Edit:: Resolved- This was due to a misunderstanding of the use of the getOpt function. I referenced the materials in the man here, on stack overflow (http://linux.die.net/man/3/getopt) and the getOpt documentation on GNU's website here: gnu.org/software/libc/manual/html_node/Example-of-Getopt.html Thanks to Bill Lynch and Remyabel for referencing source materials previously mentioned.

there seems to be an issue when I run this program using the -f variable to run the "Football" Commands, alongside using -c, However, I'm primarily concerned on getting just one to work for now.

Placing in the input:

-f -p 16 -a 25 -y 267 -t 1 -i 2

     Gives out::
     pC = 0
     pC = 32738
     pY = -1052776240
     T = 32738
     I = 1

Now, these variables should just be spitting out exactly what I put in, as the only conversion I'm using ( as seen below) is X = atof(optarg); I suspect this may have something to do with the ASCII values, though I'm almost entirely clueless.

#
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <time.h>
#include <stdlib.h>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
    srand(time(NULL));
    double r =  (6 + ( std::rand() % ( 8 - 6 + 1 ) )) / 10;
    int c;
    int pA;
    int pY;
    int T;
    int I;
    int pC;

    double mass;
    double bMass;
    double dist;
    double velo;
    double Cr = .001;
    double k = .18;
    double g = 9.8;
    double CFdraft;
    double Pair;
    double Proll;
    double Psec;
    double timeTravel = 0.0;
    double Et;
    double E;
    double Eavg = 0;
    int x = 0;
    double cT;
    double cC;
    double cY;
    double cI;
    double PasserRating;

    while ((c = getopt (argc, argv, "c:m:b:v:d:f:p:a:y:t:i:")) != -1)
    {
        if (c == 'f') // There seems to be some kind of misunderstanding with what this is doing

// The c=='f' line is there to designate which set of calculations to run, so, that needs to be the //foremost variable to be checked at the beginning of the program. { if (c == 'p') { pC = atof(optarg);

                }
            if (c == 'a')
                {
                    pA = atof(optarg);

                }
            if (c == 'y')
                {
                    pY = atof(optarg);

                }
            if (c == 't')
                {
                    T = atof(optarg);

                }
            if (c == 'i')
                {
                    I = atof(optarg);

                }
            cout << "pC " << pC << endl;
            cout << "pC " << pA << endl;
            cout << "pY " << pY << endl;
            cout << "T " << T << endl;
            cout << "I " << I << endl;
            //Calculations
            cC = ((pC / pA) - 0 / 30) * 5;
            cY = ((pY / pA) - 3) * 0.25;
            cT = ((T / pA) * 20);
            cI = ((2.375) - (I / pA) * 25);

            if (cC <= 0)
                {
                    cC = 0;
                }
            if (cC >= 2.375)
                {
                    cC = 2.375;
                }
            if (cY <= 0)
                {
                    cY = 0;
                }
            if (cY >= 2.375)
                {
                    cY = 2.375;
                }
            if (cT <= 0)
                {
                    cT = 0;
                }
            if (cT >= 2.375)
                {
                    cT = 2.375;
                }
            if (cI <= 0)
                {
                    cI = 0;
                }
            if (cI >= 2.375)
                {
                    cI = 2.375;
                }
            PasserRating = (((cC + cY + cT + cI) / 6) * 100);
            string strPR = "Poor";

            if (PasserRating <= 85)
            {
                strPR = "poor";
            }
            if (PasserRating > 85)
            {
                strPR = "mediocre";
            }
            if (PasserRating > 90)
            {
                strPR = "good ";
            }
            if (PasserRating > 95)
            {
                strPR = "great ";
            }
            cout << strPR << " " << PasserRating << endl;
        }
        if (c == 'c')
        {
            if (c == 'm')
            {
                mass = atof(optarg);

            }
            if (c == 'b')
            {
                bMass = atof(optarg);

            }
            if (c == 'd')
            {
                dist = atof(optarg);

            }
            if (c == 'v')
            {
                velo = atof(optarg);
            }
            timeTravel = (dist * 1000) / velo;
            cout << "time:" << timeTravel << endl;
            cout << "mass " << mass << endl;
            cout << "bMass " << bMass << endl;
            cout << "dist " << dist << endl;
            cout << "velo " << velo << endl;

            for (x = 0; x < (10); x ++)
            {
                CFdraft = r;
                Pair = k * CFdraft * (pow(velo, 3));
                Proll = Cr * g * (mass + bMass) * velo;
                Psec = Pair + Proll;
                Et = (Psec * timeTravel);
                E = E + Et;
                Eavg = E / timeTravel;
            }

            cout << Eavg << " KJ" << endl;
        }

    }
    return 0;
}

2 Answers2

6

I seriously recommend properly indenting your code. If you did, you would see this:

if(c == 'f'){
    if (c == 'p')
    ...
}

Clearly c is not going to be equal to 'f' and 'p' at the same time.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Doesn't it go through the rest of the flags following that condition? I thought c was merely a placeholder for the current char –  Oct 28 '14 at 17:10
  • @whatamia Please read the [man page](http://linux.die.net/man/3/getopt) for a proper example of how to use `getopt`. –  Oct 28 '14 at 17:10
  • 2
    @Whatamia: You have a variable `c`. You first test if that variable is equal to `'f'`. If that is __true__, you then compare it to see if `c`, which you just verified had a value of `'f'`, now has a value of `'p'`. – Bill Lynch Oct 28 '14 at 17:11
  • @BillLynch So, would I need to establish another variable as a placeholder for the opposite flags? –  Oct 28 '14 at 17:14
  • @Whatamia: I think you need to read some documentation on how `getopt()` works. I think you don't fully understand what that function call returns. I'd recommend reading this for example: http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html – Bill Lynch Oct 28 '14 at 17:18
3

You never execute your parsing code - everything is inside if(c == 'f') condition, which is obviously true only for the first time you run the loop... So you just get random values from the memory.

Freddie Chopin
  • 8,440
  • 2
  • 28
  • 58