ユーザーに2つのパラメーターを渡すか、空白のままにしてほしい。例えば:
./program 50 50
また
./program
使ってみたところint main(int argc, char *argv[])
、最初に変更char *argv[]
するint *argv[]
ことでしたが、うまくいきませんでした。私が欲しいのは、ユーザーに0から100までの2つの整数を入力することです。したがって、2つの整数でない場合は、エラーが発生するはずです。
私は(C#でプログラミングしていたように)型でエラーを出すことを考えていましたが、入力するものが何であれ、argv[1]は常に「char」型になります。
だから私がしたことは
for (int i = 0; i <= 100; i++) {
//printf("%d", i);
if (argv[1] == i) {
argcheck++;
printf("1st one %d\n", i);
}
else if (argv[2] == i) {
argcheck++;
printf("2nd one %d\n", i);
}
これも機能しません。また、コンパイル時に警告が表示されますが、たとえばで変更argv
すると、セグメンテーション違反(コアダンプ)エラーが発生します。atoi(argv[1])
この問題を解決する簡単な方法が必要です。
編集:
だから私はで修正しましatoi()
た、それがセグメンテーション違反を与えていた理由は私がパラメータを持っていないときにnull値でそれを試していたからです。そこで、条件を追加して修正しました。しかし今問題は、値が
./program asd asd
その場合、の出力はatoi(argv[1])
0になります。この値を変更する方法はありますか?