char command[6];
int command_num=0;
scanf("%s %d", command, &command_num);
如果 command 是“top”,我只想輸入 command,而不是 command_num。但是當我只輸入命令時,輸入是繼續。我該如何解決這個問題?
uj5u.com熱心網友回復:
您的描述基本上是偽代碼,只需將其翻譯為C:
char command[LARGE_ENOUGH]; // probably no reason to allocate just 6 bytes
if(scanf("%s", command) != 1) // read only command and check result
{
/* handle error - abort program etc */
}
if(strcmp(command, "top") != 0) // if command wasn't top
{
if(scanf("%d", &command_top) != 1)
{
/* handle error - abort program etc */
}
}
如果需要,將這一切都放在一個while回圈中。
但是,更好的解決方案可能是將輸入作為字串并接受字串中的數字作為命令。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/417163.html
標籤:
