Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Feb 24th, 2005, 8:34 PM   #1
AlphaXero
Newbie
 
Join Date: Feb 2005
Posts: 8
Rep Power: 0 AlphaXero is on a distinguished road
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");
     }
}
AlphaXero is offline   Reply With Quote
Old Feb 24th, 2005, 8:54 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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 8:56 PM.
Mjordan2nd is offline   Reply With Quote
Old Feb 24th, 2005, 8:56 PM   #3
AlphaXero
Newbie
 
Join Date: Feb 2005
Posts: 8
Rep Power: 0 AlphaXero is on a distinguished road
umm, again i m kinda new so how do i make it an array?
AlphaXero is offline   Reply With Quote
Old Feb 24th, 2005, 8:58 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Old Feb 24th, 2005, 9:02 PM   #5
AlphaXero
Newbie
 
Join Date: Feb 2005
Posts: 8
Rep Power: 0 AlphaXero is on a distinguished road
I changed it to do that, but it still gives me the same error
AlphaXero is offline   Reply With Quote
Old Feb 24th, 2005, 9:06 PM   #6
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 5 uman is on a distinguished road
did you change the if statement to what Machiavelli said?
uman is offline   Reply With Quote
Old Feb 24th, 2005, 9:08 PM   #7
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Old Feb 24th, 2005, 9:08 PM   #8
AlphaXero
Newbie
 
Join Date: Feb 2005
Posts: 8
Rep Power: 0 AlphaXero is on a distinguished road
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 9:12 PM.
AlphaXero is offline   Reply With Quote
Old Feb 24th, 2005, 9:15 PM   #9
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Look to the post right after your initial one. Don't forget to add string.h as one of your included header files.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 24th, 2005, 9:18 PM   #10
AlphaXero
Newbie
 
Join Date: Feb 2005
Posts: 8
Rep Power: 0 AlphaXero is on a distinguished road
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)
AlphaXero 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 12:04 PM.

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