-1

In my third function I am getting a syntax error at const char* it = s; when I try to compile. My friend got this program to work on his compiler, however I am using Visual Studio and it will not compile. Any and all help would be MUCH appreciated!

#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()
{
}
DJ87
  • 11
  • 1
  • 1
  • 2

2 Answers2

1

C89 (MSVC follows C89 Standard) forbids the mix of declarations and statements in a block:

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

Your declaration of it object is after gets function call and this is not allowed by C89.

Moreover gets function has been removed in latest C Standard and should be avoided at all costs in all C programs.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Sorry, I am just following the sample code given to me by my professor. So it would be better to use this? scanf("%s", s); Would I have to move it somewhere else? Sorry I am obviously new. – DJ87 Apr 14 '13 at 20:49
0

you do at the beginning of a block of variable declaration. for old c.

change to

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;

or

rename progname.c progname.cpp
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • I actually got it a few minutes after my last post, but thank you very much for your help. – DJ87 Apr 14 '13 at 23:12