0

私は現在、プログラムのメイン関数(これまでのところベアボーン関数)を作成していますが、これまでは、プログラムを終了するための「end」コマンドのみを含めてきました。コマンド以外のものを入力すると、エラーメッセージが出力されます。ただし、ループ内でさらに多くのコマンドを入力しているので、他に何も認識していないようです。ユーザーに係数と指数の入力を求めた後、多項式を作成するプログラムを書いています。

コマンドはadc(係数の追加コマンド)であり、スペースの後に、係数を表す整数と、指数を表す別の整数を持つ別のスペースを追加することになっています。

例:adc 4 5出力:4x ^ 5

int main(void){
    char buf[5]; //Creates string array
    unsigned int choice;
    printf("Command? "); // Prompts user for command
    fflush(stdout);
    gets(buf); //Scans the input

    while(strncmp(buf, "end", 3) != 0) //Loop that goes through each case, so long as the command isn't "end".
    {
        switch( choice ){
        //Where the other cases will inevitably go
            if((strcmp(buf,"adc %d %d"))== 0){
            }
        break;
            default:
              printf("I'm sorry, but that's not a command.\n"); //Prints error message if input is not recognized command
    fflush(stdout);
              break;
        }
        printf("Command? "); //Recycles user prompt
        fflush(stdout);
        gets(buf);
    }
    puts("End of Program."); //Message displayed when program ends
}
4

1 に答える 1

1

strcmp(buf,"adc %d %d")次のようなフォーマット文字列を使用して、特定の種類の入力をテストすることはできません。strcmp使用が文字通り入力する場合にのみ、文字列の同等性を通知します。 "adc %d %d"、その後に2つの整数が続きません。 adc

入力文字列を手動で解析する必要があります。空白文字をトークン化し、最初のトークンをstrcmpたとえばと照合してadcから、数値を個別に解析します。

caseのステートメントに気づきませんswitch。どこもswitch使用していないので、を削除できるようです。choice

また、使用せず、代わりgetsに使用してください。fgets

于 2012-10-13T20:39:31.347 に答える