Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16943

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
In need of some clarity... Posted by StephenC on 29 May 2007 at 1:42 PM
Hey everyone. Okay, I've been digging through a multiplicity of books, watching videos, and reading beginner tutorials on websites. I think that perhaps one of the most confusing aspects of C++ for me is one man's definition of "beginner" when compared to another's. Also, when people say "you can do this but it isn't really used anymore" and then they fail to give an example of what WOULD be used to substitute for what can be done but "isn't anymore," it leaves much to be desired. I'm approaching learning C++ from the perspective of game development, by the way. I'm starting a game dev. program at a local community college in January, so I'm trying to get into this stuff before I HAVE to.

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?

Example:

#include <iostream>

using namespace std;

int main()
{
int number = 0;

beginning:

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;
}

else
{
cout << "\nYou LOSE!!! Try again...\n" << endl;
system("pause");
system("cls");
goto beginning;
}

system("pause");

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.
Report
Re: In need of some clarity... Posted by MT2002 on 29 May 2007 at 4:39 PM

: 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?
:
: Example:
:
: #include <iostream>
:
: using namespace std;
:
: int main()
: {
: int number = 0;
:
: beginning:
:
: 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;
: }
:
: else
: {
: cout << "\nYou LOSE!!! Try again...\n" << endl;
: system("pause");
: system("cls");
: goto beginning;
: }
:
: system("pause");
:
: 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
alot.

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.

COLOR [attr]

  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
value.


So...
system ("color fc");

Will produce red text on a white background.

With some compiliers, you can also use settextcolor()
from conio.h, Or the System API.

Whatever you choose, note there is no ANSI compatible way;
ie it is system dependent.
Report
Re: In need of some clarity... Posted by MT2002 on 29 May 2007 at 4:49 PM

Heres a new version of your code. It doesnt have goto, and
flashes the colors when the player wins

#include <iostream>
#include <conio.h>			// NON PORTABLE

using namespace std;

int main()
{
int number = 0;
bool bDone=false;

//beginning:

	do {

		cout << "Please enter an integer under 100. If you guess the right range,"
		<< " you win: " ;

		cin >> number;

		if ((number >= 56) && (number <= 78))
		{

			cout << "\nYou Win!!!\n" << endl;

			// Cheap method :) -- blink text until player presses a key. NON PORTABLE
			while (!kbhit()) {
				for (int i=0;i<5;i++) system("COLOR 1f");
				for (int i=0;i<5;i++) system("COLOR f1");
			}

			bDone=true;
		}

		else
		{
			cout << "\nYou LOSE!!! Try again...\n" << endl;
			system("pause");
			system("cls");
		}
	}while (!bDone);

	system("pause");

return 0;
}
Report
Re: In need of some clarity... Posted by StephenC on 31 May 2007 at 7:27 AM
Gotcha. Thanks for the information!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.