Function problems......

Ok, I am working with making my own custom functions, and I am trying to follow everything I know about programming, but my books provide so liitle support about this.....

Here's my code:
#include
#include
#include
#include
#include
#include
#include
#include

int getMonsterNumber(int cur_level) {
int monster = random(cur_level);
return monster;
}

main() {
randomize();

int level;
int monsterID;

printf ("Input a level less than 10: ");
scanf ("%d
", level);

monsterID = getMonsterNumber(level);

printf ("Your monsterID could be: %d
", monsterID);
delay(1000);
}


Even though I have a bunch of unnessecary includes, that is for later on, when I will need them, and probably add more to that list.

Comments

  • not sure what you are trying to achieve here... care to clarify?

    : Ok, I am working with making my own custom functions, and I am trying to follow everything I know about programming, but my books provide so liitle support about this.....
    :
    : Here's my code:
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    :
    : int getMonsterNumber(int cur_level) {
    : int monster = random(cur_level);
    : return monster;
    : }
    :
    : main() {
    : randomize();
    :
    : int level;
    : int monsterID;
    :
    : printf ("Input a level less than 10: ");
    : scanf ("%d
    ", level);
    :
    : monsterID = getMonsterNumber(level);
    :
    : printf ("Your monsterID could be: %d
    ", monsterID);
    : delay(1000);
    : }
    :
    :
    : Even though I have a bunch of unnessecary includes, that is for later on, when I will need them, and probably add more to that list.
    :

  • : Ok, I am working with making my own custom functions, and I am trying to follow everything I know about programming, but my books provide so liitle support about this.....
    :
    : Here's my code:
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    :
    : int getMonsterNumber(int cur_level) {
    : int monster = random(cur_level);
    : return monster;
    : }
    :
    : main() {
    : randomize();
    :
    : int level;
    : int monsterID;
    :
    : printf ("Input a level less than 10: ");
    : scanf ("%d
    ", level);
    :
    : monsterID = getMonsterNumber(level);
    :
    : printf ("Your monsterID could be: %d
    ", monsterID);
    : delay(1000);
    : }
    :
    :
    : Even though I have a bunch of unnessecary includes, that is for later on, when I will need them, and probably add more to that list.
    :

    wha do you wanna do?

  • : : Ok, I am working with making my own custom functions, and I am trying to follow everything I know about programming, but my books provide so liitle support about this.....
    : :
    : : Here's my code:
    : : #include
    : : #include
    : : #include
    : : #include
    : : #include
    : : #include
    : : #include
    : : #include
    : :
    : : int getMonsterNumber(int cur_level) {
    : : int monster = random(cur_level);
    : : return monster;
    : : }
    : :
    : : main() {
    : : randomize();
    : :
    : : int level;
    : : int monsterID;
    : :
    : : printf ("Input a level less than 10: ");
    : : scanf ("%d
    ", level);
    : :
    : : monsterID = getMonsterNumber(level);
    : :
    : : printf ("Your monsterID could be: %d
    ", monsterID);
    : : delay(1000);
    : : }
    : :
    : :
    : : Even though I have a bunch of unnessecary includes, that is for later on, when I will need them, and probably add more to that list.

    Sorry I didn't say, had to post in a hurry :) I want to use the function getMonsterNumber to generate a random monster ID between 0 and the user's current level. It just screws up everytime I run it, ie it wont accept inputs, it just disregards all of my keystrokes until I type in 'exit', its not even giving me a warning/error. Am I using functions wrong?
  • : : : Ok, I am working with making my own custom functions, and I am trying to follow everything I know about programming, but my books provide so liitle support about this.....
    : : :
    : : : Here's my code:
    : : : #include
    : : : #include
    : : : #include
    : : : #include
    : : : #include
    : : : #include
    : : : #include
    : : : #include
    : : :
    : : : int getMonsterNumber(int cur_level) {
    : : : int monster = random(cur_level);
    : : : return monster;
    : : : }
    : : :
    : : : main() {
    : : : randomize();
    : : :
    : : : int level;
    : : : int monsterID;
    : : :
    : : : printf ("Input a level less than 10: ");
    : : : scanf ("%d
    ", [red][b]&[/b][/red]level);
    : : :
    : : : monsterID = getMonsterNumber(level);
    : : :
    : : : printf ("Your monsterID could be: %d
    ", monsterID);
    : : : delay(1000);
    : : : }
    : : :
    : : :
    : : : Even though I have a bunch of unnessecary includes, that is for later on, when I will need them, and probably add more to that list.
    :
    : Sorry I didn't say, had to post in a hurry :) I want to use the function getMonsterNumber to generate a random monster ID between 0 and the user's current level. It just screws up everytime I run it, ie it wont accept inputs, it just disregards all of my keystrokes until I type in 'exit', its not even giving me a warning/error. Am I using functions wrong?
    :
    [blue]Yes. See RED...[/blue]
  • [b][red]This message was edited by Selatos at 2002-10-18 13:5:7[/red][/b][hr]
    ok, it works now, just doesnt return the value correctly. Random generates a number, and then it returns a huge number near '12345'.




  • : [b][red]This message was edited by Selatos at 2002-10-18 13:5:7[/red][/b][hr]
    : ok, it works now, just doesnt return the value correctly. Random generates a number, and then it returns a huge number near '12345'.
    :
    :
    :
    :
    :

    I'm not familiar with the random() call but iostream also has a rand() function which can be scaled using the modulus operator - %. For example, if you wish to limit your return values try using

    returnValue = 1 + rand() % 10; // add 1 because modulus returns 0-9

    according to the value you placed in your last message '12345'... the above line of code would cause returnValue to equal 6 (1+5)

    the only draw back to using rand() that I am aware of is that while it will appear to generate random numbers, its actually generating psuedo-random numbers. This means that the same sequence of results will be produced everytime you run the program. To prevent this you can 'seed' the srand(time(0)); where time(0) is the seed and is based on the PC's clock and for this you will need to include ctime. Hope this helps.
    -Mel



  • The interesting part is that when I printf monster, it shows a number between 0 and level, but when I return it, and printf after returning to main, it makes up 12803, to be specific. I havent found anything in my books about this either.....
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories