(OT) C++ Question

  • Hail Guest!
    We're looking for Community Content Contribuitors to Stratics. If you would like to write articles, fan fiction, do guild or shard event recaps, it's simple. Find out how in this thread: Community Contributions
  • Greetings Guest, Having Login Issues? Check this thread!
  • Hail Guest!,
    Please take a moment to read this post reminding you all of the importance of Account Security.
  • Hail Guest!
    Please read the new announcement concerning the upcoming addition to Stratics. You can find the announcement Here!
V

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:)
 

Lorddog

Crazed Zealot
Stratics Veteran
Stratics Legend
Oct 25, 2004
3,212
346
10,431
www.lorddog.com
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
 
V

Vaelix

Guest
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
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 )
 

Critical Gaming

Lore Master
Stratics Veteran
Stratics Legend
Mar 5, 2009
1,177
275
2,681
35
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.
 
V

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.
 

Lorddog

Crazed Zealot
Stratics Veteran
Stratics Legend
Oct 25, 2004
3,212
346
10,431
www.lorddog.com
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";
}
 

Urin

Journeyman
Stratics Veteran
Stratics Legend
Jan 6, 2009
122
0
131
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;"
 
V

Vaelix

Guest
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;"
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;
}
 

OldAsTheHills

Lore Master
Stratics Veteran
Stratics Legend
May 13, 2008
1,050
21
2,681
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:)
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.