2016-11-17 42 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) 

,因为开关是一个关键字,你不能把它作为一个函数名。