Thread: Validating an input to be a number for guessing game

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Those last two lines aren't meant to be part of the last else.
    Maybe so, but have you looked at the code in your posts?
    Can you see how people might think otherwise?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Don't mix spaces and tabs when indenting. That's why those two lines look like they are indented differently in the code tags than they do in your editor. If you stick to only spaces or only tabs, then the code will lineup better in the forum (spaces are best for the forum, but I prefer tabs for my own coding).

    >> does anyone know a good way to set the console prompt size in the code of the program right from the start?
    That's not part of standard C++. You'll have to look into console programming for your platform. I think adrianxw might have a tutorial on windows console programming if you google it.

    For displaying the high scores in a certain format, there are different options. You could use io manipulators from iomanip. setfill and setw might help, but I'm not sure about that.

  3. #18
    Registered User
    Join Date
    May 2007
    Posts
    77
    I do see that, sorry. Sometimes, though, those spaces/tabs were put in automatically when I hit enter, such as when I did an if statement and had to use brackets. I went back and fixed it in VS.

    Anyway, I found adrianxw's page, and adapted a way to suit my needs for expanding the window, but in order to do it, I need to include "windows.h" in my globals. However, when I do that, it negates the "limits" include and I get the following errors regarding my character prevention statement:
    Code:
    1>.\GuessIt.cpp(68) : warning C4003: not enough actual parameters for macro 'max'
    1>.\GuessIt.cpp(68) : error C2589: '(' : illegal token on right side of '::'
    1>.\GuessIt.cpp(68) : error C2143: syntax error : missing ')' before '::'
    1>.\GuessIt.cpp(68) : error C2059: syntax error : ')'
    Where otherwise, if "windows.h" wasn't in the includes, it would compile and run just fine.
    This is the section it's referring to:
    Code:
    if (!(cin >> guess) || (cin.get() != '\n')) {
        cin.clear(); // clear fail state
        cin.ignore(numeric_limits<int>::max(), '\n'); // ignore bad input
        cout << "\nIllegal guess.  Your guess must be a whole number.\n";
        --tries;
    }
    Anyone have any idea why two headers would conflict like that?

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Try adding this line:
    Code:
    #define NO_MIN_MAX
    I think if you put it before windows.h it should work.

    The reason it conflicts is that the writers of windows.h did a bad thing and defined macros named min and max. A macro is just a text replacement that doesn't obey the rules of C++. There is code inside <limits> that uses min and max, and it is getting replaced with the macro. The #define above is supposed to stop windows.h from defining that macro so that your code will compile.

  5. #20
    Registered User
    Join Date
    May 2007
    Posts
    77

    Unhappy

    Nope, I tried before and after, it still came up with the same errors...
    I also came up with the possibility of making a separate file, and they both compiled ok, but I don't know how to call a function from an outside file. Say I have the function resize() in resize.cpp. I try to include resize.cpp, but I can't call resize().
    Last edited by Molokai; 11-03-2007 at 04:38 PM.

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Create a header file and inside that put a prototype for all your functions that use windows.h. Then create a cpp file and put the function definitions in that. Any code that needs to call those functions should #include the header. Then add that cpp file to your project/makefile/command line so that it compiles with the other cpp file(s).

    Actually, try NOMINMAX instead of NO_MIN_MAX.
    Last edited by Daved; 11-03-2007 at 05:05 PM.

  7. #22
    Registered User
    Join Date
    May 2007
    Posts
    77
    prototype? Function definitions? Unfortunately, I'm still a bit of a noob, could you explain?

    And still nothing, before or after...

    Edit: Haha, I had completely forgotten that, while I was trying to experiment with "windows.h" and it's location, I had put in in one of the other included headers. After I got rid of that, it worked just fine.

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A prototype is the function declaration. If your function definition (where the body of the function is defined) comes after the code that calls it, then you need a prototype to tell the compiler what the function looks like. So for you code you could have done this:
    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    // other stuff ...
    
    int game(); // prototype/declaration for game
    int menu(); // prototype/declaration for menu
    
    int main(){
    
        cout << "Welcome to Guess It, the number guessing game.\n";
    	menu();
    	return 0;
    }
    
    int menu(){ // definition/implementation for menu
      // implementation here.
    }
    
    int game (){ // definition/implementation for game
      // implementation here.
    }

  9. #24
    Registered User
    Join Date
    May 2007
    Posts
    77
    OK, I finally got it. I initially had <windows.h> ahead of <limits>. That, combined with putting the "NOMINMAX" before it, made it as though the <windows.h> wasn't even being included. I then moved <windows.h> and "NOMINMAX" at the end of the includes/global section, and it worked perfectly. I now have a program that automatically starts up at twice the height as normal! Now, to the high scores list...

  10. #25
    Registered User
    Join Date
    May 2007
    Posts
    77
    Alright, now for one last thing specifically for this program. How do I prevent the generated number from being a decimal? Or is that built into the random generator somehow? The only reason I ask is every once in a while, I guess a few numbers and think I have it pinned down, only to get a "You guessed to low" message at first and then have it tell me that I got it. Could it be something other than a decimal problem?

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's not a decimal problem. Your random variable is an int, so it can't hold a decimal. Also, rand() returns an integer and &#37; on an int returns an int, so you have no worries there.

    The problem is in your if/else if/else code that checks the guess. It is outputting "too low" when it shouldn't. Just take another look at that logic and you should see how to change it so that it doesn't output that message when you guess right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-26-2006, 12:14 AM
  4. Guess the number game
    By Ninestar in forum C Programming
    Replies: 12
    Last Post: 12-08-2005, 11:30 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM