pir
19-11-2004, 07:22
Hi,
Ich würde gerne meinem Programm 2 Argumente übergeben, diese auf 0 oder 1 überprüfen und dann weiter damit arbeiten.
Mein Problem ist, dass es mit Zahlen funkt, aber sobald ich das Programm mit char Argumenten starte, "funktioniert" es auch, sollte aber nicht.
Also genauer gesagt
programm 0/1 0/1 funkt so wie es soll
programm m h (zB) funkt auch sollte aber nicht
programm 100 200 funkt nicht, so wie es sein soll
int first_time=0;
int hw_init=0;
int main(int argc, char* argv[]){
if (argc != 3){
printf("\nThis program has to have two arguments at any given time\n");
exit (200);
}else{ // check if the arguments are valid, and save them
hw_init = atoi(argv[1]);// actually save the arguments first, to be able to check them
first_time = atoi(argv[2]);
printf("hw_init: %i\n",hw_init);
printf("first_time: %i\n",first_time);
if( !(((hw_init==0) || (hw_init==1)) && ((first_time==0) || (first_time==1))) ){
printf("The first argument may only be 1 or 0. The same applies to the second argument!\n");
exit (201);
}
}
}
hat jemand vielleicht einen Rat für mich was mir fehlt/ was ich ander machen sollte/könnte. Wieso ergeben atoi(char) = 0 ??
Danke Pir
Ich würde gerne meinem Programm 2 Argumente übergeben, diese auf 0 oder 1 überprüfen und dann weiter damit arbeiten.
Mein Problem ist, dass es mit Zahlen funkt, aber sobald ich das Programm mit char Argumenten starte, "funktioniert" es auch, sollte aber nicht.
Also genauer gesagt
programm 0/1 0/1 funkt so wie es soll
programm m h (zB) funkt auch sollte aber nicht
programm 100 200 funkt nicht, so wie es sein soll
int first_time=0;
int hw_init=0;
int main(int argc, char* argv[]){
if (argc != 3){
printf("\nThis program has to have two arguments at any given time\n");
exit (200);
}else{ // check if the arguments are valid, and save them
hw_init = atoi(argv[1]);// actually save the arguments first, to be able to check them
first_time = atoi(argv[2]);
printf("hw_init: %i\n",hw_init);
printf("first_time: %i\n",first_time);
if( !(((hw_init==0) || (hw_init==1)) && ((first_time==0) || (first_time==1))) ){
printf("The first argument may only be 1 or 0. The same applies to the second argument!\n");
exit (201);
}
}
}
hat jemand vielleicht einen Rat für mich was mir fehlt/ was ich ander machen sollte/könnte. Wieso ergeben atoi(char) = 0 ??
Danke Pir