C程序不能处理与c++相关的错误

C program not working with errors related to C++

本文关键字:错误 c++ 程序 不能 处理      更新时间:2023-10-16

我为学校写了这个程序,但是我总是得到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]!= "") && (i < NUM_OF_CHARS))
           {
            s[i]=switch (string[i]);
            i++;
               }
        printf("%s", s);
        return 0;*/
    }

程序中出现/stray xxx等错误,必须识别宏名

我对C有点陌生,所以如果你能指出我在这段代码中的错误,我会很感激。谢谢! !

#define int NUM_OF_CHARS 51

代替
#define NUM_OF_CHARS 51

还有,你用过

void switch (char *c)

由于switch是关键字,所以不能用作函数名

有很多问题。您可能需要下面的程序。代码可以编译和工作,但它仍然是绝对可怕的,但它尊重你的意图。

努力做得更好。

顺便说一句,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] != '') && (i < NUM_OF_CHARS))
  {
    s[i] = switchcase(&string[i]);
    i++;            //^ & was missing here
  }
  s[i] = '';                   // << you forgot the zero terminator
  printf("%s", s);
  return 0;                      // << removed stray "*/"
}

switch为保留字,不能用作函数名