Thread: Need a quick check

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

    Need a quick check

    Hey guys,

    Im pretty new to c ++ and was just wonderiing if anyone could quuckly correct my conversion function I have and tell me how it should be as it is not working.

    Thanks kindly

    Code:
    //<-Libs----||
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <windows.h>
    #include <algorithm>
    #include <string>
    #include <cctype>
    //--Files-->>
    #include "Classes.h"
    #include <stdio.h>
    #include <ctype.h>
    //---------<<
    
    using namespace std;
    
    string playerInName;        // Declares variables.
    string playerInAge;
    string playerInOrigin;
    string playerInPet;
    string helpAnswer;
    
    int petChoice;
    
    int main()
    {
        cout << "Welcome to the C++ Adventure Game Version 1.\nBy Darryl Mitchell \n\n";        // Introduces the game
        cout << "Please enter your name: ";                                                     // Takes details from the
    
        cin >>   playerInName;                                                                  // player and stores them
    
        cout<< "Now your age: ";                                                                // for later use.
    
        cin >>   playerInAge;
    
        cout << "Lastly; please enter your place of origin: ";
    
        cin >>   playerInOrigin;
    
        cout << "\nNo great adventurer is complete without their trusty companion!\nWhich do you have?  A Dog, Cat or an Eagle? ";
    
        cin >>   playerInPet;
    //------------------ ISSUE -------------------------------------//
        string data = playerInPet;
        transform(data.begin(), data.end(),
        data.begin(), ::tolower);
    //--------------------------------------------------------------//
        while((playerInPet != "cat") && (playerInPet !="dog")&& (playerInPet != "eagle"))
            {
                cout << "Ahahah dont be foolin' me! There are no such companions! \nPlease re-validate your companion!\n";
                cin  >>  playerInPet;
    
                if(playerInPet == "cat")
                {petChoice = 1;}
    
                if(playerInPet == "dog")
                {petChoice = 2;}
    
                if(playerInPet == "eagle")
                {petChoice = 3;}
            }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How is it not working?

    Code:
                if(playerInPet == "cat")
                {petChoice = 1;}
    Absolutely horrid formatting.
    Use this
    Code:
                if(playerInPet == "cat")
                {
                   petChoice = 1;
                }
    Or perhaps
    Code:
                if(playerInPet == "cat") petChoice = 1;
                // or
                if(playerInPet == "cat")
                      petChoice = 1;
    Never put braces on the same line as the code itself. If your function gets too long, split it up.

    Now, further, you probably want to do the translation EVERY time, not just when the answer the user gave is incorrect the first time round.

    And why are all your variables globals?

    --
    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.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Well, as I said im pretty new to this and alot of it I have been teaching myself so thats why my formatting is terrible !



    Um when I compile it and enter CAT or CaT it does not convert it to lower case.

    Yeah I would like that, but I am not sure how to make it work, and I was wondering if I can I dunno put the translation function somewhere else have have some way of calling it to where I need it without riting it all out again, is that possible?

    as for yhe variables im not sure.. I did a bit of VB coding prior to starting C ++ so a combination of uncertainty and habit
    Thanks for the input
    Last edited by Aliaks; 06-04-2009 at 05:59 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You convert data to lowercase, not playerInPet.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Changed the argument but still doesnt work
    Last edited by Aliaks; 06-04-2009 at 06:12 AM.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your logic is still messed up. You prompt for new input, but that is not converted to lower-case. Why don't you rather loop getting input until it is satisfactory, and only then continue with the rest (setting the petChoice value).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Hmmm.. isn't that pretty much what ive done?

    I just need the input of the pet turned to lowercase so then i can check it against the while statement.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You only convert input to lowercase first time it is entered. Within the loop when you ask it to be reentered, you don't convert that. Also your code wouldn't set the petChoice variable if the user entered a correct thing the first time.

    You could restructure the logic a bit. A do...while loop is good for getting validated input. You can also recognize whether something valid was entered by seeing whether the petChoice variable has been set to anything other than 0 within the loop. No need to recheck the long and complicated condition.

    Code:
        cout << "\nNo great adventurer is complete without their trusty companion!\nWhich do you have?  A Dog, Cat or an Eagle? ";
        petChoice = 0;
        do {
            cin >> playerInPet;
            transform(playerInPet.begin(), playerInPet.end(), playerInPet.begin(), ::tolower);
            if (playerInPet == "cat") {
                petChoice = 1;
            }
            else if (playerInPet == "dog") {
                ...
            }
            else {
                cout << "Ahahah dont be foolin' me! There are no such companions! \nPlease re-validate your companion!\n";
            }
        } while (petChoice == 0);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  2. Check application visibility
    By 3saul in forum Linux Programming
    Replies: 2
    Last Post: 02-13-2006, 05:13 PM
  3. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  4. spell check in C using a dictionary file
    By goron350 in forum C Programming
    Replies: 10
    Last Post: 11-25-2004, 06:44 PM
  5. MFC: How do you check if a directory exists before creating?
    By BrianK in forum Windows Programming
    Replies: 5
    Last Post: 07-06-2004, 01:09 AM