The problem I noticed as soon as I looked at your source code snipped was the use of "return 0;" in a while loop. Soon as Windows sees this command it will jump from the while loop, and return the value 0 to whoever called it, or if its in the main() it would simply return back to Windows passing it the value 0.

Whats actually happening with the variable "rollOrHold" as theres no test on the users input. Furthermore, in your snippet of code, whats actually modifying the scores, your testing the scores but nothing is modifying it, or your loop isnt even generating the random number - looking at that loop it would be an endless one since the scores would never reach 100.

Re: While loop

Posted 01 January 2011 - 02:20 PM

Phillyfries, on 09 October 2010 - 11:04 AM, said:

Softrix
I need an example of the "test on the users input".
Thanks

Sorry for the late reply.

Basically in your code your reading the keyboard and asking the player if he/she wishes to roll or hold in variable "rollOrHold" but you have not tested in your code to see what the player has entered. This can be achieved using IF statements or a Switch statement depending on your preference =)

I've provided a simple example of using the switch statement to test the players selection - in my code its an endless loop since the scores are not adjusted but it serves as an example to your question.

Re: While loop

the easiest fix to me is to just take out the condition for the while loop. You are already doing your checking within the loop itself.

while(true)
{
//code
}

@Softrix, good example but here's some tips: avoid global variables whenever possible, and you don't need a break statement within your default case.

@ ImaSexy, using global variables for scores is usually a good idea since you may need to test on this in other areas when not coding using a class; fine if it were in a class then you could use a member function to return the score. Phillyfries asked how to test on the users input and I supplied an example using a switch statement - I left the extra break in there to make it a bit more readable for him (i.e. start and end of each case) so I don't understand why you decided to bring this to my attention.. for a start its ignored by the compiler anyway... a bit knit picking on your part =)

Re: While loop

Posted 02 January 2011 - 06:57 AM

Softrix, on 02 January 2011 - 08:22 PM, said:

using global variables for scores is usually a good idea since you may need to test on this in other areas

Then pass the score around as an argument as required.
This is no justification for using a global.
As so wisely said by ImaSexy"avoid global variables whenever possible"
There are many better ways to " test on this in other areas" than using a global (in fact using a global makes it more likely any such testing will be unreliable - you can't trust globals, anything might change them while you are working with them, that's part of the reason they are a bad idea).
Since we can, and in my opinion, should, avoid using a global in this can then we shouldn't use one.

Softrix, on 02 January 2011 - 08:22 PM, said:

I left the extra break in there to make it a bit more readable for him

Seems odd that incorrect code would be more readable.
For those of us who know it's a splinter sticking up that our brains get caught on stopping the reading flow.
For a new programmer it is teaching them something that is wrong and if they are a student may lose them marks.
I agree it is not a big issue but I don't think your defence is compelling. I wouldn't have mentioned it except I was responding to the global issue and thought I might as well give my valueless opinion

Re: While loop

Posted 02 January 2011 - 07:43 AM

I don't disagree with the global variables point your both making when it comes to working with much larger projects but in this case we are talking about a small console application with probably no more than a couple of functions (if any) and in this case its safe (in my opinion) to use the global's the way I've done in the example.

I am a programmer and have been for over 25 years, and yes global variables are a bad idea on applications I develop for clients, the difference here is the projects I develop are MFC/.NET etc and they are thousands of lines of code and this one is small and a console, and for what I provided served its purpose; he asked for a demonstration on how he can check the response of the players input. With regards to the switch statement, as I said before it was left in purposely and although is present and in your eyes "wrong" its ignored by the compiler anyway, and I stand by my comment about knit picking.

Re: While loop

using global variables for scores is usually a good idea since you may need to test on this in other areas

this statement is completely false, it is never a "good idea" to use global variables like this, at best it can be considered a "last resort"

Quote

if it were in a class then you could use a member function to return the score.

If if were in a class there would also be no point of making the instance global, I don't see the relevance.

Quote

but in this case we are talking about a small console application with probably no more than a couple of functions (if any) and in this case its safe (in my opinion) to use the global's

So it's ok to practice bad coding when your learning? I think this would be the "most" important time to get into good programming habits

Quote

a bit knit picking on your part =)

It may have been, but call me crazy I like to give a tip once in a blue moon when I see code that is not correct. If you can't handle taking a tip such as the one I have given, whats your reason of being on the forum in the first place?

Re: While loop

Posted 02 January 2011 - 12:22 PM

Bad Coding? lol

Your joking right?

First off, I have not in any time on any of the boards I've helped in the past been told I've provided bad coding because some 20 year old still at school has told me so. I've probably forgotten more than you know, and on that note I was coding while you were still being potty trained so before making comments about bad coding I suggest you read my previous posts correctly.

At what point did I say anything about making a instance of a class global? All I said was that if I was using a class I'd use a member function to return the score value rather than using the global score variables. With regards to being here on the forum, I was here to offer some advice to those needing some. I don't get here often with workloads but its always good to pop here and the other sites I moderate or contribute to. I will take any advice and listen when you have a good reason and point, I don't claim to know everything, and probably never will in this ever changing technology world.

Re: While loop

Posted 02 January 2011 - 12:45 PM

Quote

Bad Coding? lol

that was a general statement, I guess in this case it would be "bad practice". But I gave a 1 line comment about global variables and it turned into a explosion of arguments and that is not what this thread is about, its about helping the OP

Quote

I've probably forgotten more than you know and on that note I was coding while you were still being potty trained

is this some weird way of bragging about your intelligence? I must of triggered a defense mechanism in mentioning global variables

This argument is pointless; the point is that "global variables are bad practice when learning", we all agree, including you. I am not bashing your intelligence because I am sure you know how to use them properly but the OP may not. Lets direct our focus away from this argument and back to the original question that was asked in the first place