我目前正在編寫一個程序的主要功能(到目前爲止的裸機功能),目前爲止我只包含了「結束」命令來結束程序。只要輸入的內容不是命令,就會輸出一條錯誤消息。但是,現在我在循環中輸入了更多的命令,它似乎沒有認識到其他任何東西。我正在編寫一個程序,在提示用戶輸入係數和指數後創建多項式。我的程序如何不能識別另一個命令?
該命令是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
}
'獲取(buf)** **即使是手冊頁也不要使用'gets' **。而且你的緩衝區對於你的預期輸入來說太小了。 –
您的交換機的病例報告在哪裏? –
爲什麼這麼皺眉?它總是似乎工作。對於我的交換機,我嘗試在我的打印後將選擇分配給我的scanf語句,但它始終處於無限循環狀態。我真的不知道要把什麼東西放到交換機上去處理各種情況,因爲它不能成爲buf。 – user1713221