Thread: Question about compilicated If Then's

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    67

    Question Question about compilicated If Then's

    I am trying to do a text adventure game but I am having a problem. I can't seem to get my if thens to take text like North. Let me show you what I mean.

    Code:
    #include <iostream.h>
    int main();
    {
        int directions;
    cout<<"What do you want to do?";
    cin>>directions;
    if (directions==north)
    {
       whatever
    }
    I think that made sense...but someone please tell me how to make it so my if thens let me enter text for the conditon...anyone?

  2. #2
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    In the code that you included above, you declared the variable 'directions' as an integer. When the compiler reaches the expression (directions == north), it's probably going to be looking for a variable called north (which doesn't exist).
    For text such as "north" you need to use strings. For selecting from a list of options, I'd suggest that you use a single character for each option e.g. 'N' for north. Also, have you considered using a switch vice nested if...then statements?
    "Logic is the art of going wrong with confidence."
    Morris Kline

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    67
    Thanks for the help. I am still a newb at this.

  4. #4
    drdroid
    Guest

    Post reply

    you could also use this function....

    Code:
    #include <iostream.h>
    #include <string.h>
    char dir[30];
    cin >> dir;
    if(!strcmp(dir,"North"))
    {
    cout << "Follow Up";
    }
    else
    {
    cout << "Wrong!";
    }

  5. #5
    drdroid
    Guest

    Post !

    Take out the ! before the strcmp and it will work, sorry for the mishap.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    23

    Re: reply

    Originally posted by drdroid
    you could also use this function....

    Code:
    #include <iostream.h>
    #include <string.h>
    char dir[30];
    cin >> dir;
    if(strcmp(dir,"North"))
    {
    cout << "Follow Up";
    }
    else
    {
    cout << "Wrong!";
    }
    that would probably be the most effictive or if you just want to change your current code try this

    Code:
    #include <iostream.h>
    int main();
    {
        char directions;  //can't you use a char* for a string of unknown length?  I'm so out of practice....
    
        cout<<"What do you want to do?";
    
        cin>>directions;                                //takes only 1 char, "N" for ex.
    
    ***here you will want to clear the buffer, I don't remember how to do that***
    
    if ( [directions=="N"] || [directions == "n")
    {
       whatever
    }
    This space for Rent.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    char ToLower(char);
    
    int main()
    {
    	char directions;					//The character which will hold the direction
     	cout<<"What do you want to do?";	
     	cin>>directions;					//Get the user's direction (N) for north, etc
      	cin.ignore(1,'\n');					//Flush the input buffer
      	switch (ToLower(directions))
      	{
      	case 'n':
    		cout<<"You pressed north.";
    		break;
    	case 's':
    		cout<<"You pressed south.";
    		break;
    	case 'w':
    		cout<<"You pressed west.";
    		break;
    	case 'e':
    		cout<<"You pressed east.";
    		break;
    	}
    	getch();
    	return 0;
    }
    
    char ToLower(char InChar)
    {
    	if((InChar >= 'A') && (InChar <= 'Z'))
    	{
    		InChar += ('a' - 'A');
    	}
    	return InChar;
    }
    Last edited by XSquared; 08-16-2002 at 11:48 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM