Stuck! (easy problem)

This is a discussion on Stuck! (easy problem) within the C++ Programming forums, part of the General Programming Boards category; Why doesnt this work? Its giving me a headick! Code: #include <conio.h> #include <cstdlib> #include <iostream> #include <stdio.h> #include <time.h> ...

  1. #1
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51

    Stuck! (easy problem)

    Why doesnt this work?

    Its giving me a headick!

    Code:
    #include <conio.h>
    #include <cstdlib>
    #include <iostream>
    #include <stdio.h>
    #include <time.h>
    
    using namespace std;
    
    double random_number;
    double number_generator()
    {
    	srand( time(NULL) );
    	random_number = rand()%10+1;
    	return random_number;	
    }
    
    int main()
    {
    	cout << random_number;
    	getch();
    	return 0;
    }

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    random_number is never defined in main();

    before outputing it do: random_number = number_generator();

    and seed rand in main as well.

    edit:: follow one of the other suggestions as well and don't use global variables.
    Last edited by axon; 09-29-2004 at 10:58 AM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Its giving me a headick!
    *giggle*

    Ok. Do srand() at the top of main(), not in number_generator(). Next, you aren't ever calling number_generator(). That would be why random_number isn't getting a random number. Then, your 'random number' generator is trying to operate on a double, when rand() only deals with ints. And you shouldn't be using the global variable.
    Code:
    #include <iostream>
    #include <ctime>
    #include <conio.h>
    
    using namespace std;
    
    int number_generator()
    {
    	return rand()%10+1;
    }
    
    int main()
    {
    	srand(time(NULL));
    	cout << number_generator();
    	getch();
    	return 0;
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    It's because your not actully calling the function try something like this
    Code:
    double number_generator()
    {
      double random_number = rand()%10+1;//This does not need to be a global
      return random_number;
    }
    
    int main(void)
    {
      srand(time(NULL));  
      cout<<number_generator();//this will print out what number_generator returns
      return 0;
    }
    Woop?

  5. #5
    Interested Newbie
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Thanks all. I'm new to c++ so it's really nice to have you guys here .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. cant see solution, should be easy array problem.
    By jinn in forum C Programming
    Replies: 4
    Last Post: 10-30-2007, 05:45 PM
  3. Somewhat easy problem
    By legendcrafter in forum C Programming
    Replies: 4
    Last Post: 10-27-2007, 03:29 PM
  4. Easy Problem
    By legendcrafter in forum C Programming
    Replies: 2
    Last Post: 10-17-2007, 05:44 PM
  5. Easy(?) Problem.
    By Scorpion-ice in forum C++ Programming
    Replies: 5
    Last Post: 10-17-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21