使用atof时的输出奇怪(optarg)

0 c++ math ascii atof

编辑::已解决 - 这是由于误解了getOpt函数的使用.我在这里引用了man中的材料,堆栈溢出(http://linux.die.net/man/3/getopt)和GNU网站上的getOpt文档:gnu.org/software/libc/manual/html_node/示例-Getopt.html感谢Bill Lynch和Remyabel引用前面提到的源材料.

当我运行这个程序使用-f变量来运行"足球"命令以及使用-c时似乎存在一个问题.但是,我主要关注的是现在只需要一个工作.

放入输入:

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

     Gives out::
     pC = 0
     pC = 32738
     pY = -1052776240
     T = 32738
     I = 1
Run Code Online (Sandbox Code Playgroud)

现在,这些变量应该只是吐出我输入的内容,因为我正在使用的唯一转换(如下所示)是X = atof(optarg); 我怀疑这可能与ASCII值有关,尽管我几乎完全无能为力.

#
#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
Run Code Online (Sandbox Code Playgroud)

// c =='f'行用于指定要运行的计算集,因此,需要在程序开头检查//最重要的变量.{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;
}
Run Code Online (Sandbox Code Playgroud)

Bil*_*nch 6

我认真推荐正确缩进代码.如果你这样做,你会看到:

if(c == 'f'){
    if (c == 'p')
    ...
}
Run Code Online (Sandbox Code Playgroud)

显然c是不会等于'f''p'在同一时间.

  • @Whatamia:你有一个变量`c`.首先测试该变量是否等于"f".如果那是__true__,那么你比较一下,看看你刚刚验证的`c`是否有''f'的值,现在的值为''p'. (2认同)