0

C ++プログラムの実行時、つまり./a.outの実行中にユーザー入力を入力したい図:./a.out input1 input2

C++プログラムは次のとおりです。

2つの数字を追加するプログラム

#include<iostream>
using namespace std; 
int main()
{
    int a, b;
    cin >> a >> b;
    int c = a + b;
    cout << "The sum of two numbers is : " << c << "\n";
}

Linuxターミナルで出力ファイルを実行しているときに、実行時にaとbの値を入力するのを手伝ってください。

4

4 に答える 4

2

多くの単純な用途のために、Boost Program.Optionsは、コマンド ライン引数を処理する定型コードの多くを提供します。チュートリアルから:

// Declare the supported options.
po::options_description desc("Allowed options");
desc.add_options()
    ("help", "produce help message")
    ("compression", po::value<int>(), "set compression level")
;

po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);    

if (vm.count("help")) {
    cout << desc << "\n";
    return 1;
}

if (vm.count("compression")) {
    cout << "Compression level was set to " 
 << vm["compression"].as<int>() << ".\n";
} else {
    cout << "Compression level was not set.\n";
}
于 2012-06-11T14:25:18.617 に答える
0
#include <iostream>
#include <cstdlib>

int main(int argc, char *argv[]) {
    using namespace std;
    int a = atoi(argv[1]);
    int b = atoi(argv[2]);
    cout << a+b << endl;
    return 0;
}

コマンドライン引数を取り、それらを出力します。atoi は文字列を int に変換します。

于 2012-06-11T07:43:03.583 に答える