Thread: Novice needs a little help.

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    Novice needs a little help.

    Hey all;

    I been working on a basic game and I have run into a slight problem.

    I am using a while statement to select a companion and if it does not equal the 3 options it will ask them to re-enter it.

    The problem I am having is that it will only loop saying re enter even if i type a correct asnwer when I have more than one option in the validation. e.g. Cat or Dog or Eagle does nto work, Cat does.

    Another issue was I have a conversion function to convert the input itno lowercase. I have been reading a book learning the fundelmentals of c + + but im not 100% sure how I can define the function properly so I can just call it for certain inputs rather than writing the whole code.

    Any help is much appreiciated.
    Last edited by Aliaks; 06-02-2009 at 06:36 AM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    should be &&..
    Code:
    .
    .
    
    while( (playerInPet != "cat") && (playerInPet !="dog")&& (playerInPet != "eagle"))
    .
    .

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Ahhh

    I did not know that, I have only seen singular while statements, Thanks!

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Well apon changing the while arg, it ran well enough the only problem is... 1st time is 100% to fail and the second iteration is 100% regardless of the argument (even if I add something that does not contain cat, dog, eagle, etc).

    Maybe its my structure?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Why do you have this on your source code?
    Code:
    while(playerInPet != "cat" or "dog" or "eagle")
    Like it was said above, replace it by this and it will work:
    Code:
    while( (playerInPet != "cat") && (playerInPet !="dog")&& (playerInPet != "eagle"))
    After replacing that, it's supposed to work fine.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Sorry I did change it in my source code I just didnt re link it... ill update now.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    You removed the transform statement. Basically if you enter "Dog" now, it will fail as you're not transforming the string to lower-case.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Oh well; iill re put it in, I didnt realise I had removed that.

    Either way, with or without it, it fails 1st attemp regardless of what I put in (if I put character in the input it fails and freezes) and the second time it works 100% regardless of what I put in.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    See your while loop? You wrote this:
    Code:
    while ((playerInPet != "cat") && (playerInPet != "dog") && (playerInPet != "eagle"));
    Notice how you added a ; in the end of it. Basically you're ending the while loop there, which is why it doesn't work how you want it to. Just remove the ;

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Ahhh I see.

    Ok cool, thanks for you help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-17-2005, 10:00 PM
  2. Novice trying to learn C++
    By dead in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2003, 09:25 PM
  3. Rtfm
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-13-2003, 05:19 PM
  4. help for a C novice
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 05-02-2002, 09:49 PM
  5. Please help a novice
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2002, 10:53 PM