[英]taking string with spaces does not work in my function
我想讓我的 function 輸入帶有任何空格的字符串。 但是我的輸入行無法以某種方式工作,並且在沒有輸入我的情況下打印1
。 我什至試過fgets
並gets
function 來避免這種情況,但結果是一樣的。 誰能幫我? 第 16 行是輸入部分。
double setup_tree(char object[20], char parentobject[20]) {
double total;
int i;
double val;
char input[100];
char answer[101];
char names[20][20];
int numcounts[20];
int objectnumber;
if (parentobject[0] == ' ') {
printf("Define %s\n", object);
} else {
printf("Define %s in %s\n", object, parentobject);
}
scanf("%[^\n]%*c", input);
printf("1");
remove_more_than_one_space(answer, input);
objectnumber = define_typeof_description(answer);
if (objectnumber == 0) {
//printf("0lalol ");
sscanf(answer, "%lf", total);
return total;
}
total = 0;
parse_the_answer(answer, names, numcounts, objectnumber);
printf("1lalol");
for (i = 0; i < objectnumber; i++) {
val = setup_tree(names[i], object);
total += (numcounts[i]) * val;
}
return total;
}
您的問題很可能來自scanf()
讀取先前調用scanf()
留在stdin
中的待處理換行符。
您可以使用scanf(" %[^\n]%*c", input);
來避免這種情況。
另請注意,您應該使用scanf(" %99[^\n]%*c", input);
防止無效輸入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.