Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 17th, 2005, 10:10 PM   #1
Blighttdm
Newbie
 
Blighttdm's Avatar
 
Join Date: Oct 2005
Posts: 17
Rep Power: 0 Blighttdm is on a distinguished road
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
Blighttdm is offline   Reply With Quote
Old Nov 17th, 2005, 10:19 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Nov 17th, 2005, 10:31 PM   #3
Blighttdm
Newbie
 
Blighttdm's Avatar
 
Join Date: Oct 2005
Posts: 17
Rep Power: 0 Blighttdm is on a distinguished road
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);
}
Blighttdm is offline   Reply With Quote
Old Nov 17th, 2005, 10:41 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,466
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Nov 18th, 2005, 5:51 AM   #5
Blighttdm
Newbie
 
Blighttdm's Avatar
 
Join Date: Oct 2005
Posts: 17
Rep Power: 0 Blighttdm is on a distinguished road
So, why is it not appending any words I enter?

Last edited by Blighttdm; Nov 18th, 2005 at 6:05 AM.
Blighttdm is offline   Reply With Quote
Old Nov 18th, 2005, 6:10 AM   #6
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3 Polyphemus_ is on a distinguished road
This code isn't gonna work, since you don't terminate the string with a 0 character ('\0').
Polyphemus_ is offline   Reply With Quote
Old Nov 18th, 2005, 6:16 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 18th, 2005, 4:53 PM   #8
Blighttdm
Newbie
 
Blighttdm's Avatar
 
Join Date: Oct 2005
Posts: 17
Rep Power: 0 Blighttdm is on a distinguished road
So how whould I go about fixing this because I'm lost.
Blighttdm is offline   Reply With Quote
Old Nov 18th, 2005, 5:14 PM   #9
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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 */
jim mcnamara is offline   Reply With Quote
Old Nov 18th, 2005, 5:22 PM   #10
Blighttdm
Newbie
 
Blighttdm's Avatar
 
Join Date: Oct 2005
Posts: 17
Rep Power: 0 Blighttdm is on a distinguished road
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?
Blighttdm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:38 PM.

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