While loop

Trouble tripping the loop

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1597 Views - Last Post: 02 January 2011 - 02:42 PM Rate Topic: -----

#1 Phillyfries  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 31
  • Joined: 09-October 10

While loop

Posted 09 October 2010 - 10:21 AM

My while loop is not being tripped so that the programcan be executed. Any suggestions?
Thanks

 while ((humanTotalScore < 100) && (computerTotalScore < 100))

    {
          cout << "Play the game. Human goes first: \n";
          cout << "Press r to Roll Again or h to Hold. "<< endl;
          cin >> rollOrHold;

        if ((humanTotalScore > 100) && (computerTotalScore > 100))
        {
           cout <<  human << "It is a tie!";
           break;
        }

        else if ((humanTotalScore >100) && (computerTotalScore < 99))
        {
           cout <<  human << ", you win!";
           break;
        }

        else if ((humanTotalScore<=99) && (computerTotalScore>=100));
        {
           cout << "Computer won" << human << ", you lost.";
           break;
        }

        return 0;
    }

This post has been edited by no2pencil: 09 October 2010 - 11:07 AM
Reason for edit:: Added Code Tags


Is This A Good Question/Topic? 0
  • +

Replies To: While loop

#2 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: While loop

Posted 09 October 2010 - 10:57 AM

Please post code in code tags like these :code:
Was This Post Helpful? 2
  • +
  • -

#3 Phillyfries  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 31
  • Joined: 09-October 10

Re: While loop

Posted 09 October 2010 - 11:04 AM

 while (rollOrHold == 'R')
    {
        //srand (time(NULL)); //random number generator section
        currentRoll = diceRoll();
        sum = sum + currentRoll;


        if (currentRoll == 1)
        {
        cout << "A roll of 1 means your turn ends." << endl;
        break;
        }
    else
        {
        humanScore = humanScore + currentRoll;
        cout << "You rolled a: " << currentRoll<< "\n";
        cout << "Your total score is: " << humanTotalScore << endl;
        }
    }

Was This Post Helpful? 0
  • +
  • -

#4 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 231
  • Joined: 07-July 10

Re: While loop

Posted 09 October 2010 - 11:04 AM

Ninja'd

Maybe I'm seeing this wrong but:

while ((humanTotalScore < 100) && (computerTotalScore < 100))
{
    cout << "Play the game. Human goes first: \n";
    cout << "Press r to Roll Again or h to Hold. "<< endl;
    cin >> rollOrHold;

    // How can this condition be true if the while condition is also true? (both humanTotalScore and computerTotalScore are smaller than 100)
    if ((humanTotalScore > 100) && (computerTotalScore > 100))
    {
        cout << human << "It is a tie!";
        break;
    }
    else if ((humanTotalScore >100) && (computerTotalScore < 99))
    {
        cout << human << ", you win!";
        break;
    }
    else if ((humanTotalScore<=99) && (computerTotalScore>=100));
    {
        cout << "Computer won" << human << ", you lost.";
        break;
    }

    // Because of this only one iteration of the loop will ever be executed, if any
    return 0;
}

This post has been edited by Coding in the name of: 09 October 2010 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#5 Phillyfries  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 31
  • Joined: 09-October 10

Re: While loop

Posted 09 October 2010 - 11:07 AM

  while ((humanTotalScore < 100) && (computerTotalScore < 100))

    {
          cout << "Play the game. Human goes first: \n";
          cout << "Press r to Roll Again or h to Hold. "<< endl;
          cin >> rollOrHold;

        if ((humanTotalScore > 100) && (computerTotalScore > 100))
        {
           cout <<  human << "It is a tie!";
           break;
        }

        else if ((humanTotalScore >100) && (computerTotalScore < 99))
        {
           cout <<  human << ", you win!";
           break;
        }

        else if ((humanTotalScore<=99) && (computerTotalScore>=100));
        {
           cout << "Computer won" << human << ", you lost.";
           break;
        }

        return 0;
    }

}

Was This Post Helpful? 0
  • +
  • -

#6 Softrix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-October 10

Re: While loop

Posted 09 October 2010 - 11:24 AM

View PostPhillyfries, on 09 October 2010 - 10:07 AM, said:

  while ((humanTotalScore < 100) && (computerTotalScore < 100))

    {
          cout << "Play the game. Human goes first: \n";
          cout << "Press r to Roll Again or h to Hold. "<< endl;
          cin >> rollOrHold;

        if ((humanTotalScore > 100) && (computerTotalScore > 100))
        {
           cout <<  human << "It is a tie!";
           break;
        }

        else if ((humanTotalScore >100) && (computerTotalScore < 99))
        {
           cout <<  human << ", you win!";
           break;
        }

        else if ((humanTotalScore<=99) && (computerTotalScore>=100));
        {
           cout << "Computer won" << human << ", you lost.";
           break;
        }

        return 0;
    }

}


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.
Was This Post Helpful? 0
  • +
  • -

#7 Phillyfries  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 31
  • Joined: 09-October 10

Re: While loop

Posted 09 October 2010 - 12:04 PM

Softrix
I need an example of the "test on the users input".
Thanks
Was This Post Helpful? 0
  • +
  • -

#8 Softrix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-October 10

Re: While loop

Posted 01 January 2011 - 02:20 PM

View PostPhillyfries, 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.

Hope this helps.

#include <iostream>
using namespace std;

// scores
int g_HumanScore = 0, g_ComputerScore = 0;

// for users option (hold/roll).
char g_Option;	

int main()
{

	// loop until one of us gets to 100 or more!
	while(g_HumanScore < 100 || g_ComputerScore < 100)
	{
		cout<<"Press r to Roll Again or h to Hold. ";
		cin>>g_Option;
		
		// we have h or r
		switch(g_Option)
		{

			case	'h':
			case	'H':
				// put code here for hold

			break;

			case	'r':
			case	'R':
				// put code here for rolling

			break;

			// default is run when none of the specified cases is met above.
			default:
				cout<<"Invalid imput, use r or h"<<endl;
			break;

		}
		
	}

	return	0;	// be kind to windows.
}

Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: While loop

Posted 01 January 2011 - 08:01 PM

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.

This post has been edited by ImaSexy: 01 January 2011 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#10 Softrix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-October 10

Re: While loop

Posted 02 January 2011 - 04:22 AM

View PostImaSexy, on 01 January 2011 - 07:01 PM, said:

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 =)
Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: While loop

Posted 02 January 2011 - 06:57 AM

View PostSoftrix, 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.


View PostSoftrix, 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 :)
Was This Post Helpful? 2
  • +
  • -

#12 Softrix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-October 10

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.
Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: While loop

Posted 02 January 2011 - 10:50 AM

Quote

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?

This post has been edited by ImaSexy: 02 January 2011 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

#14 Softrix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-October 10

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.
Was This Post Helpful? 0
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

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

This post has been edited by ImaSexy: 02 January 2011 - 12:48 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2