![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 17
Rep Power: 0
![]() |
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 knownCode: 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 |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2005
Posts: 17
Rep Power: 0
![]() |
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);
} |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2005
Posts: 17
Rep Power: 0
![]() |
So, why is it not appending any words I enter?
Last edited by Blighttdm; Nov 18th, 2005 at 6:05 AM. |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3
![]() |
This code isn't gonna work, since you don't terminate the string with a 0 character ('\0').
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Oct 2005
Posts: 17
Rep Power: 0
![]() |
So how whould I go about fixing this because I'm lost.
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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 */ |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Oct 2005
Posts: 17
Rep Power: 0
![]() |
Firstly thanks,
Second, I just want to understand how these corrections work. fgets(input,sizeof(input),stdin); 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--; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|