![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 8
Rep Power: 0
![]() |
string help
alright i m a newbie at programming so this and any questions i ask here will more then likely be pretty easy for some of you. anyways, i m attempting to make a text based rpg just to see how things work, ya know just for fun. and i get a error "error: syntax error before string constant." what i am trying to do is be able to enter a word, or set of words and have it call a function, but i dont know what i m doing wrong. Here is my code (also i know you could figure this out, but i am working in C not C++)
start()
{
char str
printf ("you see a door what do you do?\n");
scanf ("%s", str);
if (str "door" == 0)
{
printf ("you get to the door");
}
} |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Well, the most obvious thing I see is that your str variable needs to be an array. There may be some other errors that I didn't catch at first glance, though, and if there are, let us know.
Edit: Ahh. Just found another one. Your if statement is incorrect. If you want to test for the word door, you need to do it like this: if(strcmp(str, "door") == 0)
{
// code
}
__________________
"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 Last edited by Mjordan2nd; Feb 24th, 2005 at 7:56 PM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 8
Rep Power: 0
![]() |
umm, again i m kinda new so how do i make it an array?
|
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
You'll need to declare it as something like this:
char str[50]; /* 50 can be replaced with any reasonable number -- its the maximium length for a string that can be entered*/ For more information on arrays, check this out: http://programmingforums.org/forum/s...ead.php?t=1644
__________________
"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 |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2005
Posts: 8
Rep Power: 0
![]() |
I changed it to do that, but it still gives me the same error
|
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
did you change the if statement to what Machiavelli said?
|
|
|
|
|
|
#7 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
The code compiled for me.
Could you post your entire code? Perhpas there's something wrong with something else. Also, you need to include string.h for the strcmp() function.
__________________
"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 |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Feb 2005
Posts: 8
Rep Power: 0
![]() |
how did he chabne the if statement? and here is my full code
- #include <stdio.h>
start()
{
char str[80];
printf ("you see a door what do you do?\n");
scanf ("%s", str);
if (str "door" == 0)
{
printf ("you get to the door");
}
}
main()
{
printf ( "welcome to my crappy txt rpg.\n");
start()
}Last edited by AlphaXero; Feb 24th, 2005 at 8:12 PM. |
|
|
|
|
|
#9 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Look to the post right after your initial one. Don't forget to add string.h as one of your included header files.
__________________
"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 |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Feb 2005
Posts: 8
Rep Power: 0
![]() |
Thanks it compiled, but i typed in "door" in the program after i ran it and now i run into a different problem. It didnt goto the printf function afterwards it just exited (also i added void before the start() function)
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|