This error is bugging me!

This is a discussion on This error is bugging me! within the C++ Programming forums, part of the General Programming Boards category; The error is marked as a comment and in italics. Code: #include <stdio.h> #include <stdlib.h> #include <time.h> void initialize_number_generator(void); void ...

  1. #1
    ct28au
    Guest

    Question This error is bugging me!

    The error is marked as a comment and in italics.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    void initialize_number_generator(void);
    void choose_new_secret_number(void);
    main()
    {
      char answer;
      int guess, secret_number, num_guesses = 0;
      printf("I have a number between 1 and 1000");
      printf("Can you guess my number?");
      printf("Please type your first guess");
      do {
        choose_new_secret_number();
        num_guesses++;
        printf("Enter guess: ");
        scanf("%d", &guess);
        if (guess == secret_number) {
          printf("You won in %d guesses!\n\n", num_guesses);
          return 0;
        } else if (guess < secret_number)
          printf("Excellent! You guessed the number!");
        printf("Would you like to play again (y/n)?");
        scanf(" %c", answer);
        printf("\n");
      } while (answer == 'y' || answer == 'Y');
      return 0;
    } // end of main
    void initialize_number_generator(void)
    {
    srand((unsigned) time(null)); // 'null' undeclared [first use this function]
    } // end of initialize_number_generator
    void choose_new_secret_number(int secret_number)
    {
      secret_number = rand() % 1000 + 1;
    } // end of choose_new_secret_number

  2. #2
    Unregistered
    Guest
    In C++ a null value is used by the constant NULL. Capitalize it and the bug will go away, possibly to show different one.

  3. #3
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    You should change your #includes to the following:

    <stdio>
    <stdlib>
    <time>
    <math>

    For all the standard includes the .h has been eliminated in C++. The .h extensions refer to the C style includes which could give you errors.

    I added the math header becuase I beleive that the srand() fxn is located there and that is what seems to be giving you the problems.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    u don't need the math header for srand()

    but, u return a comment saying that u guessed the right number if the guess is less than the secret number

    also, if u use the new c++ headers, write using namespace std right after the list of headers.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    60

    int&

    1) I'd recommend to call choose_new_secret_number() with an int& parameter, don't forget to inform the compiler before you use the function with this
    declaration:
    void choose_new_secret_number(int& secret_number);
    2) If I guessed a 4 and the secret number was 5 the output would be
    "Excellent ..." because of <else if(guess<secret_number)>
    3) Write NULL instead of null
    4) Don't forget to call the Initialize-function, !(declaration==execution);
    5) good prog, though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bugging ... Compile Time Error
    By keats in forum C Programming
    Replies: 7
    Last Post: 03-25-2007, 05:54 AM
  2. Can Anyone Help!! This Is Bugging Me
    By ProgrammingDlux in forum C++ Programming
    Replies: 4
    Last Post: 01-25-2002, 12:21 PM
  3. Pointer Riddle that is bugging me
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-30-2001, 12:11 AM

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