![]() |
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 '{' tokenCode: :
if(input[len]=='y')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 |
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. |
Well here it is, I was just following the guidelines and not posting the whole thing. Hope it helps.
:
/********************************* |
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. |
So, why is it not appending any words I enter?
|
This code isn't gonna work, since you don't terminate the string with a 0 character ('\0').
|
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.
|
So how whould I go about fixing this because I'm lost.
|
Here - I fixed the first block - you can duplicate it down into the other if blocks.
:
#include <stdio.h> |
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--; |
| 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