Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   String error in if statement (http://www.programmingforums.org/showthread.php?t=7050)

Blighttdm Nov 17th, 2005 10:10 PM

String error in if statement
 
The Problem:
As I try to compile a small C program I get the following error messages:
:

ghp11.c:25: error: syntax error before '{' token
ghp11.c: At top level:
ghp11.c:30: error: syntax error before '(' token
ghp11.c:30: error: `len' undeclared here (not in a function)
ghp11.c:30: error: syntax error before '==' token
ghp11.c:33: warning: data definition has no type or storage class
ghp11.c:34: error: syntax error before '}' token
ghp11.c:33: error: storage size of `input' isn't known

These errors occur in the second

Code:

:

if(input[len]=='y')
 {
  input[len]='i';
  input[len+1]='e';
  input[len+2]='s';
 }

elseif(input[len]=='s')
 {
  input[len+1]='e';
  input[len+2]='s';
 }

elseif(((input[len-1]=='c')&&(input[len]=='h'))) //line 30
 {
  input[len+1]='e';
  input[len+2]='s';
 }                                              //line 34

elseif((input[len-1]=='s')&&(input[len]=='h'))
 {
  input[len+1]='e';
  input[len+2]='s';
 }


Now I know a couple of reasons it may not work but I'm not sure and my manual gives me little advice. Could it be:
-Len can't be altered like that because it was a result of strlen?
-I din't initialize a variable properly?
-Some string syntax?
-General syntax that I missed?
-I missed some big idea and messed up big time?

As always thank you for your help,
Blight

Mjordan2nd Nov 17th, 2005 10:19 PM

Shouldn't else and if be seperated? Also, could you show us your decleration for input? Actually, it would be easier if you just put your whole code up so we can compile and fix the errors ourselves.

1.) Len should be able to be altered, though I don't see you altering len anywhere.

2.) This may be possible for input, perhaps.

3.) Other than the else if part, I don't see any syntactical errors right off the bat.

4.) Again, else-if

5.) No, you seem to have the idea down, so I don't think this is it.

Blighttdm Nov 17th, 2005 10:31 PM

Well here it is, I was just following the guidelines and not posting the whole thing. Hope it helps.
:

/*********************************
*********************************/

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

int main (void)
{
 char input[30];
 int len;

 printf("Please enter a noun(up to 28 characters) to pluarize:");
 scanf("%s",input);
 
 len=strlen(input);

 if(input[len]=='y')
 {
  input[len]='i';
  input[len+1]='e';
  input[len+2]='s';
 }

elseif(input[len]=='s')
 {
  input[len+1]='e';
  input[len+2]='s';
 }

elseif(((input[len-1]=='c')&&(input[len]=='h')))
 {
  input[len+1]='e';
  input[len+2]='s';
 }

elseif((input[len-1]=='s')&&(input[len]=='h'))
 {
  input[len+1]='e';
  input[len+2]='s';
 }


else
{
input[len+1]='s';
}

printf("\n%s is the plural.",input);

return(0);
}


Infinite Recursion Nov 17th, 2005 10:41 PM

Change "elseif" to "else if" ... it will compile then.
The code needs some work as the plural for "gun" is not "gun" as your program suggests.

Blighttdm Nov 18th, 2005 5:51 AM

So, why is it not appending any words I enter?

Polyphemus_ Nov 18th, 2005 6:10 AM

This code isn't gonna work, since you don't terminate the string with a 0 character ('\0').

DaWei Nov 18th, 2005 6:16 AM

You are not using scanf in such a way as to prevent overrunning the buffer, which can lead to improper operation and crashing of your program. Your buffer, with a length of 30, can hold a 29-character string, maximum, since your string will require a terminating null if you intend to pass it to a C-string function such as printf or strlen. When you overwrite the terminating null that IS there with your plural ending, you're bound to get odd results, if not a memory violation, because you're not terminating your addition.

Blighttdm Nov 18th, 2005 4:53 PM

So how whould I go about fixing this because I'm lost.

jim mcnamara Nov 18th, 2005 5:14 PM

Here - I fixed the first block - you can duplicate it down into the other if blocks.
:

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

int main (void)
{
 char input[30]={0x0}; /* initialize the string */
 int len=0;

 printf("Please enter a noun(up to 28 characters) to pluarize:");
 fgets(input,sizeof(input),stdin);  /* prevent user enter LARGE word to crash
                                          this program */
 
 len=strlen(input);  /* add an if statment to be sure your word is okay length */
 len--;                /* characters start counting from zero, not one
                            so the last character of the string is len-1 --
                              len-- decrements len */
 if(input[len]=='y')
 {
  input[len]='i';
  input[len+1]='e';
  input[len+2]='s';
 }
  /* it works just fine up to here */


Blighttdm Nov 18th, 2005 5:22 PM

Firstly thanks,
Second, I just want to understand how these corrections work.
:

fgets(input,sizeof(input),stdin);
I know gets is used for strings but why use it in a single string?
Also stdin, I have no idea what that does, does it involve strings or some type of typical error as a result of gets?
:

len--;
Why do I have to decrement len? Is it for appending reasons?


All times are GMT -5. The time now is 10:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC