1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Greetings Guest!!

    In order to combat SPAM on the forums, all users are required to have a minimum of 2 posts before they can submit links in any post or thread.

    Dismiss Notice

(OT) C++ Question

Discussion in 'UO Siege Perilous' started by Vaelix, Jan 14, 2011.

  1. Vaelix

    Vaelix Guest

    Since I have no Real Friends to ask who are intelligent :)thumbdown:), I figured I'd ask here.. :pancakes:

    Heres the deal, basically Im trying to generate a random number, then use that to determine what "Pet" That character would get, Im also trying to make it so that it invokes an outside function [Outside of main()] to do this.

    Something like this.. *Note* This is a console application (CMD) nothing too extensive. (Trying to keep it simple)

    int pet()
    {

    srand(time(0))
    int randomNumber = rand(); // I use randomNumber because i might need
    int petNumber = (randomNumber % 5) + 1; // it outside of the use for petNumber.

    *Ifs 1-5*

    return petNumber;
    }

    I have 5 If conditionals, all which point to a specific console output based on which # is rolled.

    (Example..)

    if (pet == 1){
    cout << "tiger\n";
    }

    if (pet == 2){
    cout << "wolf\n";
    }


    Pretty much, if i compile the source, it will crash when i reference the pet, which I do near the end with..

    Cout << pet() << endl;

    In debugging mode, (Using Bloodshed Dev C++) Im getting a segmentation fault, access violation when I go to access the function. (Bear with me as Im just starting to get into this again, been busy, and not in school for it sadly).

    My biggest frustration here is that, if I move the Ifs out of the pet function and into main(), the program will stop crashing, but, will no longer apply an animal to the number that is generated, instead it will roll a separate random number when my compiler gets to Cout << pet() << endl; At which time, it will simple output a number instead of hitting one of the Ifs..


    (If this doesnt make any sense, and you are actually interested in helping me.. Shoot me a PM :thumbsup:)

    (Otherwise... :cursing:)
     
  2. Lorddog

    Lorddog Crazed Zealot
    Stratics Veteran Stratics Legend

    Joined:
    Oct 25, 2004
    Messages:
    3,115
    Likes Received:
    298
    shouldnt u be putting pet into a variable and then checking your 5 if's to see if they match.
    it looks like each if is calling into the pet function and getting a different random number
     
  3. Vaelix

    Vaelix Guest

    Why would this cause an Access Violation then?

    Edit on the original post.. This is the proper line that I have

    int petNumber = (randomNumber % 5) + 1;

    ( randomNumber and not srand )
     
  4. Mook Chessy

    Mook Chessy Certifiable
    Stratics Veteran

    Joined:
    Oct 16, 2007
    Messages:
    1,934
    Likes Received:
    21
    Please repost in English...


    :)
     
  5. Critical Gaming

    Critical Gaming Lore Master
    Stratics Veteran

    Joined:
    Mar 5, 2009
    Messages:
    1,035
    Likes Received:
    170
    lol, free shards.

    Your best bet is probably the runuo boards. Chances are your question is already answered there. If not, it wouldn't hurt to ask there.
     
  6. Vaelix

    Vaelix Guest

    *Note*

    This has nothing to do with UO or any UO related programs.

    Im trying to learn C++ so I started trying to write code.. :rant2:


    Pretty much im just trying make a function to generate a random number, then store it to be called with pet() while also referencing my Ifs to see what kinda pet the character would get, as dog pointed out there is a better way.. I'm just not "Seasoned" enough to know it..

    And by "Get" i mean like.. Spawn or hatch or something.. You get a random animal companion basically, and I want this function to define which by giving a random number (of 1-5) then checking against 1-5.
     
  7. Lorddog

    Lorddog Crazed Zealot
    Stratics Veteran Stratics Legend

    Joined:
    Oct 25, 2004
    Messages:
    3,115
    Likes Received:
    298
    int pet()
    {

    srand(time(0))
    int randomNumber = rand(); // I use randomNumber because i might need
    int petNumber = (randomNumber % 5) + 1; // it outside of the use for petNumber.

    *Ifs 1-5*

    return petNumber;
    }

    I have 5 If conditionals, all which point to a specific console output based on which # is rolled.

    (Example..)
    int myPet = pet();
    if (myPet == 1){
    cout << "tiger\n";
    }

    if (myPet == 2){
    cout << "wolf\n";
    }
     
  8. Urin

    Urin Journeyman
    Stratics Veteran

    Joined:
    Jan 6, 2009
    Messages:
    122
    Likes Received:
    0
    The problem with your original code is that there is a nested call to "cout." Since the first call would have locked the output stream, the second call creates the access violation.

    The way to see this is to consider the line "cout << pet() << endl;" and what it tries to become within pet(). That is, assuming wolf was picked: "cout << cout << "wolf\n" << endl;" The red cout is the cause. Basically the callee is trying to acquire a lock on the output stream after it's already been aqcuired by the caller.

    The simplest way to fix this, and conform to your desired structure, would be to just change the order to "pet(); cout << endl;"
     
  9. Vaelix

    Vaelix Guest

    Snap... Thats pretty obvious and I hate myself thank you lol. :thumbsup:

    Also.. I'm using this thanks to Raptor :heart:
    This was actually the kind of "result" i was looking for, I just had no clue how to do it so i tried compensating by using the Random number vs Ifs

    void chooseanimal()
    {
    string animals[] = {"tiger","wolf","aardvark","flamingo","platypus "};
    srand(time(0));
    cout<<animals[rand()%(sizeof(animals)/sizeof(string*))]<<endl;
    }

    int main( int argc, const char* argv[])
    {
    chooseanimal();
    return 0;
    }
     
  10. OldAsTheHills

    OldAsTheHills Lore Master
    Stratics Veteran

    Joined:
    May 13, 2008
    Messages:
    1,050
    Likes Received:
    21
    You should not use a method name as a variable in Main method.
    Instead of using all those if statements use a switch statement...with cases.
    Try building a Class with methods.

    OldAsTheHills.