How Would I Do This

This is a discussion on How Would I Do This within the C++ Programming forums, part of the General Programming Boards category; How Would i Get It To Lets SAy Code: while(game == 1) { cin >> input; if(input == "use") { ...

  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 11:53 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    You might want to provide more context, i.e., the bigger picture of what you are trying to do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    839
    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,304
    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    90
    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,475
    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.
    I'm just trying to be a better person - My Name Is Earl

  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 05:37 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21