int main(void)
{
int i,j=0,k; //initialization
char equation[100]; //input is a string (I think?)
int data[3]; //want only 3 numbers to be harvested
printf("Enter an equation: ");
fgets(equation, 100, stdin); //not so sure about fgets()
for (i = 0; i < equation[100]+1; i++) { //main loop which combs through
//"equation" array and attempts
//to find int values and store
while (j <= 2) { //them in "data" array
if (isdigit(equation[i])) {
data[j] = equation[i]
j++;
}
}
if (j == 2) break;
}
for (k = 0; k <= 2; k++) { //this is just to print the results
printf("%d\n", data[k]);
}
return 0;
}
Hello!這是我在C語言中的入門級程序,我試圖梳理一個數組,並將這些數字取出並分配給另一個數組,然後我可以訪問和處理這些數組。嵌套for/while循環和數組,從數組中濾除數字
但是,每當我運行這個我得到0 0 0作爲我的三個元素在我的「數據」數組。
我不確定我是否對自己的邏輯或數組語法犯了錯誤,因爲我是數組的新手。
在此先感謝! :)
你不想要的0陣列中? – Mozzie
那麼,除非輸入字符串是0 0 0!我總是得到0 0 0而不是實際的字符串:( – ZumbaLover69
這個「for(i = 0; i