Thread: Why doesn't this script work?

  1. #1
    Registered User
    Join Date
    Dec 2003

    Unhappy Why doesn't this script work?

    Something is wrong with it.....
    It gives me the following error:

    c:\c__~1\island.cpp: In function `int main()':
    c:\c__~1\island.cpp:31: `yes' undeclared (first use this function)
    c:\c__~1\island.cpp:31: (Each undeclared identifier is reported only once
    c:\c__~1\island.cpp:31: for each function it appears in.)
    c:\c__~1\island.cpp:35: `y' undeclared (first use this function)
    c:\c__~1\island.cpp:39: `ya' undeclared (first use this function)
    c:\c__~1\island.cpp:43: `yup' undeclared (first use this function)
    and this is the script...:

    #include <iostream>
    int main()
     int slot1;
     int slot2;
     int slot3;
     int slot4;
     int slot5;
     char endr[20];
     char sure[20];
     cout<<"Please enter the top 5 things you would like to have if you were on an island alone"<<endl;
     cout<<"Type the first and most important thing"<<endl;
     cout<<"Type the second thing"<<endl;
     cout<<"Type the third thing"<<endl;
     cout<<"Type the fourth thing"<<endl;
     cout<<"Type the fifth thing and the one that you would least like to have"<<endl;
     cout<<"You liked the following in order"<<endl;
     cout<<"1. "<<slot1 <<endl;
     cout<<"2. "<<slot2 <<endl;
     cout<<"3. "<<slot3 <<endl;
     cout<<"4. "<<slot4 <<endl;
     cout<<"5. "<<slot5 <<endl;
     cout<<"Am I right?";
     cin.getline(endr, 20, '\n');
      cout<<"I knew it!";
     else if(endr==y)
      cout<<"I knew it!";
     else if(endr==ya)
      cout<<"I knew it!";
     else if(endr==yup)
      cout<<"I knew it!";
      cout<<"Are you sure?";
      cout<<"Sure....whatever you say...";
      cout<<"I knew it!";
     return 0;

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    You can't compare strings using the equality operator. Also, you must enclose strings with double quotes, " ".

    You could try the following, but it wouldn't work:
    if (endr=="yes")
    This won't work because string names resolve to the address or location of the string. So, that statement is comparing the addresses of the strings, which would rarely, if ever, return the desired result. What you need to do is compare, one by one, each character of the strings to check it is the same. Fortunately, this is built into the standard in some library, stdlib.h I think. Personally, I use Windows, so I use the windows version.

    if (!lstrcmpi(endr,"yes")) //Windows
    if (!strcmpi(endr,"yes")) //Standard C++
    Look up those functions, but quickly:
    lstrcmpi is case-insensitive and returns 0 if the strings are equal. It can also tell you which one is larger, but in your case that is not necessary.

    Replace all of your string equality tests with lstrcmpi or strcmpi, and the program should compile fine.

    You can't compare strings using the equality operator.
    Sorry, this is not entirely correct. If you use the string CLASS, as opposed to just a buffer and pointer, you can use the equality operator.
    Last edited by bennyandthejets; 12-21-2003 at 01:10 AM.
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ

  3. #3
    Registered User
    Join Date
    Apr 2003

    I've seen the code for this program in several of your posts, and the bottom line is you are unable to write much code with the proper syntax. You need to start with Hello World type programs that are no more than 4 or 5 lines long until you get the syntax right. Then you have to study about things like char arrays before using them. C++ is a very strict language and you aren't going to be able to throw code together and get it to work unless you know what you're doing.

    Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2009, 12:57 AM
  2. using expect script in C program
    By nitinmhetre in forum Linux Programming
    Replies: 1
    Last Post: 12-21-2006, 08:25 AM
  3. Script in games
    By Shakti in forum Game Programming
    Replies: 7
    Last Post: 09-27-2006, 12:27 AM
  4. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  5. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM