2012-09-21 29 views
-1

爲什麼這不起作用?C - break語句後遞增不工作

short pStringCt = 0; 
const char* constInBuf = ibuf; 
int j; 
for (j = 0; j < i; j++) 
{ 
    /*Detect when there's a space in the buffer and grab word*/ 
    if (ibuf[j] == ' ' || 
     ibuf[j] == '|' || 
     j == i - 1) 
     { 
      int k; 
      for (k = j - 1; k >= 0; k--){ 

       if (k == 0 || ibuf[k] == ' ' || ibuf[k] == '|') 
       { 
        if (pStringCt > 0){ 
         pString[pStringCt] = strndup(constInBuf + k + 1, j - k + 1); 
         printf("-pString at %d is: %s\n", pStringCt, pString[pStringCt]); 
         pStringCt += 1; 
        } 
        if (pStringCt == 0){ 
         pString[pStringCt] = strndup(constInBuf+k, j-k + 1); 
         printf("-----pString at %d is: %s\n", pStringCt, pString[pStringCt]); 
         pStringCt+=1; 
        } 
        printf("%d is pStringCt\n", pStringCt); 
        break; 
       } 
      } 
     } 

pStringCt的值在break語句後沒有遞增。換句話說,pStringCt在方法開始時爲0。然後,在兩個與它有關的陳述之後,它增加到一個。但在break語句之後,它重置爲零...

+0

你有沒有想過調試你的程序? –

+0

我已經做了。 printf語句總是在遞增之前給我0,在遞增之後給出1。但他們從來沒有從1開始,並回報2等。 –

+0

請粘貼整個代碼,並給我們輸出您當前的代碼。我看到有很多的打印那裏,你可以簡單地調試你的代碼,通過打印值在break語句之後的第一個語句之前。 – oyss

回答

1

所以我使用scanf來獲取我的字符串,它通過我的方法迭代了我的字符串兩次,這使方法的返回值搞砸了。抱歉沒有粘貼所有的代碼,但我只是回答了我自己的問題...對不起...

P.S.現在使用getline()。奇蹟般有效。