我有這樣的程式
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
float x, k;
int choose;
_Bool valid;
do {
valid = 1;
printf("\nChoose variant: \n1 - count of nums\n2 - float number\n3 - e:\nYour choose: ");
scanf(" %d", &choose);
while(isdigit(choose) == 0) {
printf("Please, choose number, not letter (number between 1 to 3): ");
fflush(stdin);
scanf(" %d", &choose);
}
while(choose > 3 || choose < 0) {
printf("Please, choose correct option (number between 1 to 3): ");
fflush(stdin);
scanf(" %d", &choose);
}
if(choose == 1 || choose == 2 || choose == 3) valid = 0;
} while (valid);
return 0;
}
我有這樣的程式。我想讓用戶選擇:1、2 或 3。我正在嘗試檢查字符以及其他數字。我希望程式回圈,直到我輸入正確的。但它不起作用,我已經嘗試了其他方法來解決這個問題,但它對我不起作用。我有一個想法,這里沒有清潔,也許是這樣?
我還可以設定一個條件,使 scanf 等于一——這意味著已經輸入了一個字符。但我希望如果用戶輸入“1gf”,例如,那么條件也將起作用,而不是從 1 繼續。
非常感謝您提前
uj5u.com熱心網友回復:
程式可能會掃描一個字符以進行測驗,然后將其轉換為整數,而不是檢查數字條目,如以下代碼片段所示。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
float x, k;
int choice;
char choose;
int valid = 1;
do
{
printf("\nChoose variant: \n1 - count of nums\n2 - float number\n3 - e:\nYour choose: ");
scanf(" %c", &choose);
if((choose > '9'))
{
printf("Please, choose number, not letter (number between 1 to 3): ");
}
if((choose < '1' || (choose > '3')))
{
printf("Please, choose correct option (number between 1 to 3): ");
}
if(choose == '1' || choose == '2' || choose == '3') valid = 0;
}
while (valid);
choice = choose - '0';
printf("The choice is %d\n", choice);
return 0;
}
以下是需要注意的幾點。
- 選擇變數被定義為一個字符而不是一個整數。
- 對字符范圍進行測驗以確定條目是否對此代碼有效。
- 然后簡單地通過從輸入的字符值中減去零字符的整數值來確定選擇值。
在終端上對此進行測驗會產生以下示例輸出。
@Dev:~/C_Programs/Console/Choice/bin/Release$ ./Choice
Choose variant:
1 - count of nums
2 - float number
3 - e:
Your choose: e
Please, choose number, not letter (number between 1 to 3): Please, choose correct option (number between 1 to 3):
Choose variant:
1 - count of nums
2 - float number
3 - e:
Your choose: 4
Please, choose correct option (number between 1 to 3):
Choose variant:
1 - count of nums
2 - float number
3 - e:
Your choose: 2
The choice is 2
當然,可以通過多種方式輸入和檢查條目。這只是一種方式。試一試,看看它是否符合您的專案精神。
uj5u.com熱心網友回復:
你不能在標準 C 中做到這一點,你需要使用一些額外的庫。例如在 Linux ncurses 中。
#include <ncurses.h>
#include <string.h>
char *readstr(char *buff, size_t size, const char *allowed)
{
size_t cpos = 0;
int c;
while ((c = getch()) != ERR && c != '\n' && (cpos 1) < size)
{
if(strchr(allowed, c)) buff[cpos ] = c;
else addstr("\b \b");
}
buff[cpos] = 0;
return c == ERR ? NULL : buff;
}
int main (void)
{
char number[7];
cbreak();
echo();
initscr();
if(readstr(number, sizeof(number), "0123456789"))
{
printf("you have entered: `%s`\n", number);
}
else
{
printf("ERROR!!!!\n");
}
return 0;
}
uj5u.com熱心網友回復:
一個掃描集%1[123]將掃描一個字符,即 1、2 或 3。
另一個掃描集%*[^\n]將掃描并丟棄所有內容,直到換行。
#include <stdio.h>
int main ( void) {
char choice[2] = "";
int scanned = 0;
do {
printf("\nChoose variant: \n"
"\t1 - count of nums\n"
"\t2 - float number\n"
"\t3 - e:\n"
"Choose: ");
fflush ( stdout);
if ( 1 != ( scanned = scanf(" %1[123]", choice))) {
if ( EOF == scanned) {
fprintf ( stderr, "EOF scanf\n");
return 0;
}
printf("---Please, choose correct option ( 1, 2 or 3)---\n");
}
scanf ( "%*[^\n]"); // scan and discard up to a newline
} while ( 1 != scanned);
switch ( choice[0]) {
case '1':
printf ( "count of nums\n");
break;
case '2':
printf ( "float number\n");
break;
case '3':
printf ( "e:\n");
break;
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/528604.html
標籤:C循环条件语句符号
