gpt4 book ai didi

c - 错误C2371,重新定义;不同的基本类型

转载 作者:行者123 更新时间:2023-11-30 18:40:05 25 4
gpt4 key购买 nike

我收到函数的错误代码 c2371;分开的,最长的和最短的。我认为这与输入参数有关。

error C2371: 'seperate' : redefinition; different basic types
error C2371: 'shortest' : redefinition; different basic types
error C2371: 'longest' : redefinition; different basic types

代码:

#include <stdio.h>

int main(void)
{
char msg[41];
int selector = 0;

printf("Alinur Caglayan - 18050127622 - Lab Work 5 ");
printf("Please enter a string message with maximum of 40 characters: ");
gets(msg);

printf("Please select one of the following functions:\n1) Longest Word Function");
printf("\n2) Shortest Word Function\n3) Separate Words Function\n4) Exit: ");
scanf("%d", &selector);

if(selector == 1)
{
longest(msg);
}
else if(selector == 2)
{
shortest();
}
else if(selector == 3)
{
seperate();
}
else if(selector == 4)
{
return 0;
}
else
{
printf("\nPlease enter a valid integer!\n");
scanf("%d", selector);
}
}

char longest(msg)
{
char *temp = msg, *word = msg, *max = msg;
int increment = 0, incrementmax = 0;

do {
if(!(*temp & ~32))
{
if(increment > incrementmax)
{
incrementmax = increment;
max = word;
}
increment = 0;
}
else if(!increment++)
{
word = temp;
}
}
while(*temp++ != 0);

for(temp = max; (*temp & ~32); )
{
printf("%c", *temp++);
}
}

char shortest(msg)
{
char *temp = msg, *word = msg, *max = msg;
int increment = 0, incrementmax = 0;

do {

if(!(*temp & ~32))
{

if(increment > incrementmax)
{
incrementmax = increment;
max = word;
}
increment = 0;
}
else if(!increment++)
{
word = temp;
}
}
while(*temp++ != 0);

for(temp = max; (*temp & ~32); )
{
printf("%c", *temp++);
}
}

char seperate(msg)
{

char *temp = msg;
int i;

printf("Alinur Caglayan - 18050127622 - Lab Work 5 ");
printf("Please enter a string message with maximum of 40 characters: ");
gets(msg);

for(i=0; i < 41; i++)
{
if(*temp & ~32)
{
printf("\n");
}
else
{
printf("%c", temp);
}
system("pause");
}
}

最佳答案

有很多错误。

您应该在 main 之前声明您的函数,如下所示:

char shortest(char msg[41]);
char longest(char msg[41]);

或者如果您不想声明它们,您可以在 main 之前定义它们...

你还有一个:

scanf("%d", selector);

虽然应该是:

scanf("%d", &selector);

所有的函数也应该返回一个字符。

已编辑:另一件事是您定义的函数如下:

char longest(msg) {
...
}

但是您必须指定参数的类型,如下所示。

char longest(char msg[41]) {
...
}

关于c - 错误C2371,重新定义;不同的基本类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27216332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com