: In my simple little "game" below, the "player" has to guess a number
: less than 100. You can read the logic and see exactly which numbers
: yield success and which do not. My question is, if the person gets
: it wrong, I've got it set up for the program to take them back to a
: spot I've labeled "beginning". Of all I've read and watched, I've
: yet to see anyone note anything about goto other than "this isn't
: used much anymore." If that's the case, then if I were to use it,
: would it be "looked down upon" as something ancient? If so, what the
: heck should I use instead of goto? I've tried creating a more
: complex application -- a virtual soda machine -- and in the
: application, I have a lot of switches with cases that end with a
: goto to send the person back to a specific point to re-enter some
: type of data. Is there a more "correct" or beneficial way of
: achieving goto?
: #include <iostream>
: using namespace std;
: int main()
: int number = 0;
: cout << "Please enter an integer under 100. If you guess the
: right range,"
: << " you win: " ;
: cin >> number;
: if ((number >= 56) && (number <= 78))
: system("COLOR 1f");
: cout << "\nYou Win!!!\n" << endl;
: cout << "\nYou LOSE!!! Try again...\n" << endl;
: goto beginning;
: return 0;
: One more quick question... how would I go about making the console
: change colors every second while sitting on a cout of something like
: "YOU WIN!!"?
: Thanks for your time.
Im writing a large game (at 25000+ lines), and have yet to use a goto :)
Useually if you design your structure around routines and classes,
there is no need for goto.
The reason goto is looked down upon , is that it produces Spaghetti code.
This makes programs *much* harder to maintain, and increases complexity
As for changing colors, there are alot of ways to do it.
As you are already using system(), Im assuming portability
isnt a concern.
If so, just use the command console's color command:
Sets the default console foreground and background colors.
attr Specifies color attribute of console output
Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground. Each digit
can be any of the following values:
0 = Black 8 = Gray
1 = Blue 9 = Light Blue
2 = Green A = Light Green
3 = Aqua B = Light Aqua
4 = Red C = Light Red
5 = Purple D = Light Purple
6 = Yellow E = Light Yellow
7 = White F = Bright White
If no argument is given, this command restores the color to what it was
when CMD.EXE started. This value either comes from the current console
window, the /T command line switch or from the DefaultColor registry
system ("color fc");
Will produce red text on a white background.
With some compiliers, you can also use settextcolor()
, Or the System API.
Whatever you choose, note there is no ANSI compatible way;
ie it is system dependent.