> 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?
> 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.
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.
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:
Where otherwise, if "windows.h" wasn't in the includes, it would compile and run just fine.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 : ')'
This is the section it's referring to:
Anyone have any idea why two headers would conflict like that?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; }
Try adding this line:I think if you put it before windows.h it should work.Code:#define NO_MIN_MAX
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.
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.
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.
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.
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. }
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...
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?
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 % 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.