• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
NUCLEAR RABBIT

C Do/While Loop problem

20 posts in this topic

Hello, I am learning C at the moment and I just made a very simple program to get started with the basics, but I keep getting an error with my while statement and have no idea why! Can anyone please help me see what I am doing wrong?

[source]
#include <stdio.h>
#include <time.h>

int CheckNumbers(int, int);

int main()
{
// Generates random number between 1-10
srand(time(NULL));
int comp_num = (rand() % 10 +1);

// Gets the users number
int player_num = 0;

do
{
printf("Enter in a number (1-10): ");
scanf("%d", &player_num);

int result = CheckNumbers(player_num, comp_num);

// determines if the numbers are the same
if(result == 1)
{
printf("The number you entered is the same as the computers.\n");
getchar();
}
else
{
printf("You and the computers numbers are different.\n");
printf("computer number: %d & your number is: %d\n\n", comp_num, player_num);
printf("Try again!\n\n");
getchar();
}
}while(result != 1);


return 0;
}
///////////////////////////
//
// FUNCTION DEFINITIONS
//
///////////////////////////
int CheckNumbers(int num, int comp_num)
{
if(num == comp_num)
return 1;
else
return 0;
}[/source]
0

Share this post


Link to post
Share on other sites
You'll have to move the declaration of "result" to outside the loop.

EDIT: Ah, I see you found the answer while I posted. My connection seems to be slow for some reason. Edited by shuma-gorath
0

Share this post


Link to post
Share on other sites
I tend to use a "manual break" pattern when I don't want to lift the variable out of the loop. Some people might argue that this is a code smell, but I prioritize "narrowest variable scoping" and "use the least number of variables that work" in this case.

[code]
for(;;) // this syntax loops indefinitely, so the only way out is a break, return, or exception.
{
// stuff
int result = whatever;

if (result == x)
break;
}
[/code] Edited by Nypyren
0

Share this post


Link to post
Share on other sites
[quote name='Nypyren' timestamp='1354308092' post='5005836']
I tend to use a "manual break" pattern when I don't want to lift the variable out of the loop. Some people might argue that this is a code smell, but I prioritize "narrowest variable scoping" and "use the least number of variables that work" in this case.

[code]
for(;;) // this syntax loops indefinitely, so the only way out is a break, return, or exception.
{
// stuff
int result = whatever;

if (result == x)
break;
}
[/code]
[/quote]

I sometime do the same thing, but without "for". I consider this a bit safer approach as it doesn't hit you so bad when you forget the break:

[code]
{
// stuff
int result = whatever;
if (result == x)
// other stuff
}
[/code]
0

Share this post


Link to post
Share on other sites
[quote name='Nypyren' timestamp='1354308092' post='5005836']
I tend to use a "manual break" pattern when I don't want to lift the variable out of the loop. Some people might argue that this is a code smell, but I prioritize "narrowest variable scoping" and "use the least number of variables that work" in this case.

[code]
for(;;) // this syntax loops indefinitely, so the only way out is a break, return, or exception.
{
// stuff
int result = whatever;

if (result == x)
break;
}
[/code]
[/quote]

[code]
for(int result; result != x; ) // this syntax loops indefinitely, so the only way out is a break, return, or exception.
{
// stuff
result = whatever;
}
[/code]

?
0

Share this post


Link to post
Share on other sites
[quote name='Khatharr' timestamp='1354581055' post='5006858']
[code]
for(int result; result != x; ) // this syntax loops indefinitely, so the only way out is a break, return, or exception.
{
// stuff
result = whatever;
}
[/code]

?
[/quote]
That syntax uses `result' uninitialized. I am perfectly happy with the infinite-loop-with-break solution.
0

Share this post


Link to post
Share on other sites
Result being uninitialized and as such potentially x, not even entering the loop is exactly the kind of bug I love so much. "We have a few customers reporting a really weird bug, but we are completely unable to reproduce it and have now wasted several weeks running test scenarios on a bunch of machines and staring at many thousands lines of related and semi-related code to figure it out. Turns out somebody ignored #1 in the coding guidelines: ALWAYS immediately initialize your variables and never justify laziness with 'better performance'."

So if x is unknown, you would have to initialized with something like "result = x+1" to be safe, resulting in awkward code that is more confusing than it has to be..
2

Share this post


Link to post
Share on other sites
[quote name='fae' timestamp='1354526009' post='5006548']
I sometime do the same thing, but without "for". I consider this a bit safer approach as it doesn't hit you so bad when you forget the break:

[code]
{
// stuff
int result = whatever;
if (result == x)
// other stuff
}
[/code]
[/quote]
Am I missing something? Because the way I read that, there's no looping, and if there's no looping, it's purpose is entirely different than the looping version, in which case you don't do the same thing...
2

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1354599792' post='5006978']
[quote name='fae' timestamp='1354526009' post='5006548']
I sometime do the same thing, but without "for". I consider this a bit safer approach as it doesn't hit you so bad when you forget the break:

[code]
{
// stuff
int result = whatever;
if (result == x)
// other stuff
}
[/code]
[/quote]
Am I missing something? Because the way I read that, there's no looping, and if there's no looping, it's purpose is entirely different than the looping version, in which case you don't do the same thing...
[/quote]

No you're absolutely correct. I was just focusing on the scoping part of Nypyren's post. However now that you mentioned I probably misinterpreted Nypyren's reason for the block as the topic of the discussion is loops after all.. :)
0

Share this post


Link to post
Share on other sites
Yeah, wasn't paying attention to the body when I did that. (derp) The var should have an initial value, but I just figure if you're gonna use a for loop that breaks when a var is at a specific value then why not use the for-loop syntax?

Here's an upgraded version:
[source lang="cpp"]void operator,() {
for(int result = initial_val; result != x; ) {
//do stuff
result = whatever;
}
}[/source]

[img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

Anyway, I'll use 'while(true)' sometimes, although MSVC has an annoying warning about it. Suppose it's a matter of preference. Edited by Khatharr
0

Share this post


Link to post
Share on other sites
[quote name='Khatharr' timestamp='1354607178' post='5006998']
Anyway, I'll use 'while(true)' sometimes, although MSVC has an annoying warning about it. Suppose it's a matter of preference.
[/quote]
What does the warning say? If it's because of "true", you can just use 1 (or any value different from zero, really). Or does it literally babysit you saying "this is an infinite loop"?
0

Share this post


Link to post
Share on other sites
[quote name='Bacterius' timestamp='1354616906' post='5007029']
[quote name='Khatharr' timestamp='1354607178' post='5006998']
Anyway, I'll use 'while(true)' sometimes, although MSVC has an annoying warning about it. Suppose it's a matter of preference.
[/quote]
What does the warning say? If it's because of "true", you can just use 1 (or any value different from zero, really). Or does it literally babysit you saying "this is an infinite loop"?
[/quote]

"while (1)" throws the warning as well - [url="http://msdn.microsoft.com/en-us/library/6t66728h%28v=vs.80%29.aspx"]conditional expression is constant[/url]. Personally I prefer to have the warning and just use "for (;;)" instead - the warning is far more useful than being able to do "while (1)".
0

Share this post


Link to post
Share on other sites
I agree that the warning could potentially be useful, but I've probably triggered it a million times and it was always an intentional unconditional loop.

Pity there's no explicit unconditional loop.

"for(;;)" looks like the arachnid version of a revolutionary's wall art. Or maybe it's a walrus?

Do we have an ASCII expert in here?
0

Share this post


Link to post
Share on other sites
[quote name='Khatharr' timestamp='1354657067' post='5007195']
Do we have an ASCII expert in here?
[/quote]
What do you need an ASCII expert for (not claiming to be one)?
0

Share this post


Link to post
Share on other sites
[quote]"for(;;)" looks like the arachnid version of a revolutionary's wall art. Or maybe it's a walrus?[/quote]
Well, I agree that for(;;) looks horrible and is much less readable at a glance than while(true), but if you can just remember that it means "loop indefinitely" then I guess it's fine.
0

Share this post


Link to post
Share on other sites
[quote name='Khatharr' timestamp='1354657067' post='5007195']
I agree that the warning could potentially be useful, but I've probably triggered it a million times and it was always an intentional unconditional loop.
[/quote]

Have a read: http://stackoverflow.com/questions/3490823/why-msvc-generates-warning-c4127-whan-constant-is-used-in-while-c - it outlines the general usefulness of the warning. My general philosophy on this is that if it saves your ass even just once then it's probably worth it.

The one case where I [i]do[/i] agree that it's a pain is "do { ... } while (1)".

[quote name='Khatharr' timestamp='1354657067' post='5007195']
Pity there's no explicit unconditional loop.
[/quote]

According to the link above "for (;;)" is actually what is explicitly defined to be an infinite/unconditional loop, but I haven't cross-checked with the standard so take it with the appropriately sized grain of salt.

[quote name='Khatharr' timestamp='1354657067' post='5007195']
"for(;;)" looks like the arachnid version of a revolutionary's wall art. Or maybe it's a walrus?
[/quote]

Definitely a walrus.
0

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1354669971' post='5007254']
[quote name='Khatharr' timestamp='1354657067' post='5007195']
"for(;;)" looks like the arachnid version of a revolutionary's wall art. Or maybe it's a walrus?
[/quote]
Definitely a walrus.
[/quote]

Or maybe it belongs in [url="http://www.jokecrazy.com/modules.php?name=News&file=article&sid=26"]this collection[/url].
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0