0

頭がおかしくなったので、基本的な質問があります。関数を特定の文字列に書き込むにはどうすればよいですか? たとえば、while ループを作成していてプログラムを終了させたい場合、入力を求められたときに「end」と入力するとプログラム自体が終了するようにするにはどうすればよいでしょうか。

編集:「end」と入力して関数を終了する方法はかなり簡単にわかりましたが、何らかの理由で、書いた文の数に応じて、プログラムが繰り返し続けます。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
int i;
char buf[10];
printf("Command? ");
scanf("%s", buf);

while(buf != "end")
{
    if(strcmp(buf, "end")== 0){
        break;
    }

    switch( buf[i] ){
    //Where the cases will inevitably go
    default:
    puts("I'm sorry, but that's not a command.\n");
    break;
    }
    printf("Command? ");
    scanf("%s", buf);
}
puts("End of Program.");
getch();
}
4

1 に答える 1

1
char *myInputString = NULL;
while (1) {
    /* read in myInputString from user input, and test... */
    if (strcmp(myInputString, "foo") == 0)
        break;
}

return EXIT_SUCCESS;
于 2012-10-12T23:53:03.777 に答える