-1

在我的第三个函数中,我在 const char* it = s; 处遇到语法错误 当我尝试编译时。我的朋友让这个程序在他的编译器上工作,但是我使用的是 Visual Studio,它不会编译。任何和所有的帮助将不胜感激!

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>


//Declaring program functions
char concStrings(); 
int compStrings();
int lowerCase();
int birthday();

//Main function begins the program execution
int main (void)
{
concStrings( );
compStrings();
lowerCase();
birthday();

}
//end main function

//concatenate function
char concStrings ( )
{
char string1[20];
char string2[20];
char string3[50] = "";;

printf("Enter in two strings:");
gets(string1);
gets(string2);

strcat(string3, string1);
strcat(string3, "-");
strcat(string3, string2);

printf("The new string is: %s\n", string3);

}

// compare function
int compStrings() 
{
char string1[20];
char string2[20];
int result;

printf ( "Enter two strings: ");
//  scanf( "%s%s", string1, string2 );
gets(string1);
gets(string2);

result = strcmp ( string1, string2 );

if (result>0)
printf ("\"%s\"is greater than \"%s\"\n", string1, string2 );
else if (result == 0 )
printf ( "\"%s\" is equal to \"%s\"\n", string1, string2 );
else
printf ( "\"%s\" is less than \"%s\"\n", string1, string2);
return 0;
}

//lowercase function
int lowerCase()
{
char s[300];
int x;
int counted = 0;
int inword = 0;

printf ("Enter a sentence:");
//  scanf("%s", s);
gets(s);

const char* it = s;

printf ("\nThe line in lowercase is:\n");

for (x = 0; s[x] !='\0'; x++)
printf ("%c", tolower(s[x]));

do
{ 
switch(*it) 
{ 
  case '\0': case ' ': case '\t': case '\n': case '\r':
    if (inword)
   {
     inword = 0; counted++;
    }
    break; 
  default: 
    inword = 1;
 } 
 }
while(*it++);

printf("\nThere are %i words in that sentence.\n", counted);

return 0;
}

int birthday()
{
}
4

2 回答 2

1

C89(MSVC 遵循 C89 标准)禁止在块中混合声明和语句:

gets(s);
const char* it = s;

您的it对象声明是在gets函数调用之后,C89 不允许这样做。

此外gets,最新的 C 标准中删除了函数,在所有 C 程序中应不惜一切代价避免使用。

于 2013-04-14T20:34:53.763 回答
0

你在变量声明块的开头做。对于老 c。

改成

int lowerCase()
{
char s[300];
int x;
int counted = 0;
int inword = 0;
const char* it;

printf ("Enter a sentence:");
//  scanf("%s", s);
gets(s);

it = s;

或者

rename progname.c progname.cpp
于 2013-04-14T21:19:58.377 回答