Thread: How Would I Do This

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    114

    How Would I Do This

    How Would i Get It To Lets SAy
    Code:
    while(game == 1)
    {
         cin >> input;
         if(input == "use")
         {
               if(check == "torch")
              {
                    Code here
              }
               else
              {
                      code here
               }
          }
         if(input == "take")
          {
       
                if(check == "barrel")
                {
                       Code here
                 }
                 else if(check == "cheese")
                 {
                        code here
                 }
                 else
                 {
                        code here
                  }
           }
    }
    check is the result of seeing wat is typed after the "take" input is typed
    so if the user types take barrel how would i get the program to find out they typed barrel after it


    How would i get it to check wat they typed after the take?
    Last edited by Nathan the noob; 02-06-2009 at 12:53 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to provide more context, i.e., the bigger picture of what you are trying to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    K i gave a example

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you should study classes rather than making multiple switch statements.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    An Example of a class?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're asking "how do I remove a wheel nut" when you should be asking "how do I change a tyre" (example borrowed from matsp).
    What is it you're trying to do really?

    Give an example of what the user types in, what the program should output, and what you need to have changed in your variables as a result.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Ok Lets Say The Program Tells The User Their is a barrel and a torch in a room

    And The user wants to take the torch so he types take torch
    i want to make it so a action occurs when he types this cause when i have
    input == "take torch" it doesnt work cause their is a space.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nathan the noob
    input == "take torch" it doesnt work cause their is a space.
    That is probably because you are not reading the string the way you want to. For example, maybe you should use std::getline() to read in the string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Can u give me a example of getline in use>?

  10. #10
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    i think cin will stop reading after space... that's why you can not set input "take torch" since cin will only retrieve "take".

    what is input variable type? is it an array of char?
    my suggestion is: when it comes to you to compare inputted string, use strcmp().
    you can not explicitly compare string with "==" since it is NULL terminated. am i right?
    if it is an array of char,try this:
    Code:
    int main()
    {
    	char input[20];
    	gets(input);
    	if (strcmp(input,"get torch")==0){
    		//do something
    	}
    }
    if it is a string, as laserlight suggestion, you can use std::getline() and string compare.

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Code:
    else if(strcmp(input,"take torch")==0)
             {
                      cout <<"You pick up The Torch Off The Barrel As You Hear The Foot Steps Getting Closer";
                      getchar();
                      cout <<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                      
             }
    Doesnt work

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Code:
    std::string theAction = "";
    
    std::cout << "Enter command: ";
    std::getline( std::cin, theAction );
    
    if ( theAction == "get torch" ) {
    // do somthing
    }
    Ideally you need to read up on classes. Never use gets() as its evil. Since you are using
    C++ you should avoid char arrays and use std::string as mentioned.
    Double Helix STL

  13. #13
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Thanks =D

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If we are assuming that you are writing a text-adventure game, perhaps you should consider a more flexible structure as well. For example, each room can have a list of "loose objects" (that is, anything that can potentially be picked up/taken).

    Each "action" (e.g "take", "drop" or "go") should have it's own function. In the function for "take" you compare the user-given string ("torch") with the list of items in the room, and if it's a match, remove it from the list of items in the room, and add it to the user's list of objects.

    Writing a complete text-adventure can be quite complicated, but it's very good practice for learning programming - much of which is useful for both game and general programming.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    That sounds complicated im still noob =D im just getting to no the for loops :P and yes making text games is a good learning experience and its kinda fun =D
    Last edited by Nathan the noob; 02-07-2009 at 06:37 PM.

Popular pages Recent additions subscribe to a feed