あなたはここで多くのことを間違っています:
#include <iostream>
using namespace std;
int check_q(char* argv[])
{
float q, qa; // you never assign `q` a value, so the following comparisons make no sense
if(atoi(argv[1]) == q) // you never check argc in main to determine if argv[whatever] is valid. if the array isn't long enough, this will invoke undefined behavior.
{
qa = atof(argv[2]); // you're assigning a value to `qa` declared in this function, leaving the one declared in main unchanged. probably not what you intended
}
// and so on
return qa;
}
int main(int argc, char *argv[])
{
float qa = 0;
check_q(argv); // this function never changes the value of `qa` that's declared in main...
cout << qa << endl; // ... so this will always print 0
return 0;
}
おそらく、次の行に沿って何かをしたいと思うでしょう:
#include <iostream>
#include <string>
#include <vector>
float check_q(const std::vector<std::string>& args)
{
if(args[1] == "-q")
{
return ::atof(args[2].c_str());
}
else
{
return 0.0f; // or some other default
}
}
int main(int argc, char *argv[])
{
const std::vector<std::string> args(argv, argv+argc);
if(args.size() >= 3) // argv[0] is usually the name of the executable
{
std::cout << check_q(argv) << std::endl;
}
else
{
std::cout << "not enough args" << std::endl;
}
}
もう少し経験を積んだら、 GNU getoptやboost::program_optionsなどのライブラリを使用したくなるでしょう。