C程序不能处理与c++相关的错误
C program not working with errors related to 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]!= " ") && (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为保留字,不能用作函数名
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'