2016-11-17 77 views
1

我寫了這個程序爲學校的錯誤的工作,但是我不斷收到C++相關的錯誤(顯然)C程序不涉及到C++

#include <stdio.h> 
    #define int NUM_OF_CHARS 51 

    void switch (char *c) 
    { 
     //Little Letters 
     if (((*c)>=97) && ((*c)<=122)) 
      (*c)-=32; 
     //Capital Letters 
     if ((c>=65) && (c<=90)) 
      (*c)+=32; 
     //*c>=5 
     if ((c>=53) && (c<=57)) 
      (*c)=56; 
     //*c<5 
     if ((c>=48) && (c<=52)) 
      (*c)=48; 
    }*/ 

    int main() { 

     char string[51]; 
     printf("PLease Enter a String \n"); 
     scanf("%s", string); 
     printf("%s => ", string); 

     int i=0; 
     char s[51]; 

     while((string[i]!= "\0") && (i < NUM_OF_CHARS)) 
      { 
      s[i]=switch (string[i]); 
      i++; 
       } 
     printf("%s", s); 
     return 0;*/ 

    } 

我越來越方案和宏的名稱,如/雜散XXX錯誤必須確定。

我對C很陌生,所以如果你能指出我在這段代碼中的錯誤是什麼,我會很感激。謝謝!!

+1

你使用c編譯器還是C++編譯器? – user463035818

+0

我正在使用C lion – tamir

+2

'#define int NUM_OF_CHARS 51'應該是'#define NUM_OF_CHARS 51' – George

回答

1

有很多問題。您可能需要下面的程序。代碼編譯和工作,但它仍然是絕對可怕的,但它尊重你的意圖。

試着讓它變得更好。

順便說一句,string變量中包含的字符也發生了變化,這是用意嗎?

#include <stdio.h> 
#define NUM_OF_CHARS 51 // removed "int" 

char switchcase (char *c) // << we need to return a char not void 
{       // << name changed to switchcase 
    // all c changed to (*c), BTW: *c without() would be OK too 

    //Little Letters 
    if (((*c) >= 97) && ((*c) <= 122)) 
    (*c) -= 32; 
    //Capital Letters 
    else if (((*c) >= 65) && ((*c) <= 90)) 
    (*c) += 32; 
    //*c>=5 
    else if (((*c) >= 53) && ((*c) <= 57)) 
    (*c) = 56; 
    //*c<5 
    else if (((*c) >= 48) && ((*c) <= 52)) 
    (*c) = 48; 

    return *c; 
} 

int main() { 

    char string[51]; 
    printf("PLease Enter a String \n"); 
    scanf("%s", string); 
    printf("%s => ", string); 

    int i = 0; 
    char s[51]; 

    while ((string[i] != '\0') && (i < NUM_OF_CHARS)) 
    { 
    s[i] = switchcase(&string[i]); 
    i++;   //^ & was missing here 
    } 

    s[i] = '\0';     // << you forgot the zero terminator 
    printf("%s", s); 
    return 0;      // << removed stray "*/" 
} 
+0

數字應該被字符文字替換。 –

+0

@ThomasMatthews肯定,但正如我在答案中寫的那樣,代碼仍然非常糟糕,還有很多需要改進的地方。 –

0

開關是保留字,不能用作函數名稱。

+1

這個和一些'* /'不應該是 – user463035818

+0

將它們關閉,仍然沒有運行(不斷收到這些錯誤) – tamir

1
#define int NUM_OF_CHARS 51 

#define NUM_OF_CHARS 51 

也取代它,你已經使用

void switch (char *c) 

,因爲開關是一個關鍵字,你不能把它作爲一個函數名。