2017-05-15 79 views
0

我使用代碼塊16.01時,我調試此代碼它顯示我一個正確的輸出,但是當我運行它顯示不正確的輸出!如何解決這個問題?C:調試和運行 - 不同的輸出

int main() 
{ 
    char ch[100],var[100],val[100],tempVa[100]; 
    int i = 0,j=0,count=0; 
    while (1) 
    { 
     puts("Enter the expression (or (end) to exit):"); 
     gets(ch); 
     if (strcmp(ch, "end") == 0 || strcmp(ch, "END") == 0) 
      exit(-1); 
     else if(2 == sscanf(ch,"%s = %s", var, val)) 
     { i = 0; 
      printf("Variable is : %s\t Value Before evaluating : %s\n",var, val); 
      while (i<=strlen(val)) 
      { 
       while (val[i]!='-'&&val[i]!='%'&&val[i]!='/'&&val[i]!='*'&&val[i]!='+'&&i<strlen(val)) 
        tempVa[j++]=val[i++]; 

       i++; 
       for (count=0; count<strlen(tempVa); count++) 
        printf("%c", tempVa[count]); 
       for (count=strlen(tempVa); count>=0; count--) 
        tempVa[count]='\0'; 
       j=0; 
      } 
     } 
     else 
      printf("Invalid!"); 
    } 
    return 0; 
} 

Smaple輸入:哈桑= Merna +穆罕默德+艾哈邁德

調試輸出Debug

運行輸出 Run

從沒有那些垃圾來哪裏?

+0

您的圖片無效。你能以文本格式寫出輸出嗎? –

+1

提示:在將內容複製到其中後,您需要空終止'tempVa'。在調試中,它可能在啓動時被清零,但在發佈時,它只是當時內存中的任何內容。 – Joe

+0

@ErikW 運行:https://i.stack.imgur.com/mmR5e.png 調試:MernaMohamedAhmed –

回答

1

我測試你的代碼和它的作品 編輯:

  1. 你應該導入文件string.h庫(反正,你應該始終解決您有任何警告)。
  2. 使用return -1,這就是爲什麼main是一個int函數。
  3. 像@joe在評論中說的,你應該總是用'\ 0'來終止你的字符串。

代碼:

#include <stdio.h> 
#include <string.h> // edit 

int main() 
{ 
    char ch[100], var[100], val[100], tempVa[100]; 
    int i = 0, j = 0, count = 0; 
    while (1) 
    { 
     puts("\nEnter the expression (or (end) to exit):"); 
     gets(ch); 
     if (strcmp(ch, "end") == 0 || strcmp(ch, "END") == 0) 
      return -1; // edit 
     else if(2 == sscanf(ch, "%s = %s", var, val)) 
     { 
      i = 0; 
      printf("Variable is : %s\t Value Before evaluating : %s\n", var, val); 
      while (i <= strlen(val)) 
      { 
       while (val[i] != '-' && val[i] != '%' && val[i] != '/' && val[i] != '*' && val[i] != '+' && i < strlen(val)) 
        tempVa[j++] = val[i++]; 

       i++; 
       for (count = 0; count < strlen(tempVa); count++) 
        printf("%c", tempVa[count]); 
       for (count = strlen(tempVa); count >= 0; count--) 
        tempVa[count] = '\0'; 
       j = 0; 
      } 
     } 
     else 
      printf("Invalid!"); 
    } 
    return 0; 
} 

樣品運行:

輸入表達式(或(結束)退出):哈桑= Merna +穆罕默德+艾哈邁德
變量是:哈桑價值在評估之前:Merna + Mohamed + Ahmed MernaMohamedAhmed
輸入表達式(或(結束)退出):

相關問題