Thread: Hey guys help with HiLo program

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Hey guys help with HiLo program

    Hey guys im writing a simply HiLo game.

    I cant get my do while statement to work correctly. I want it to stop running the program after you get the right answer. Right now everything works properly but it continues even after you get the right answer. Im using microsoft visual 2010. Also let me know if you see anything that is messy or I could have done easier. Mind that I am still new. I am reteaching my self after 1 semester of learning.

    Also is there a way i can change the setting and have the program stay open without using the cin.clear(); cin.ignore(255, '\n'); cin.get(); at the end?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <ctime>    //for time
    #include <cstdlib>  //for rand()
    
    using namespace std;
    
    int nNum;
    int nCheck;
    int nTry;
    
    unsigned int GetRandomNumber(int nLow, int nHigh)
    {
    	srand(time(0));
    	nNum = (rand() % (nHigh - nLow +1) + nLow);
    	return nNum;
    }
    
    
    bool Check()
    { 
    	if (nCheck == nNum)
    	{	
    		cout << "Correct!" << endl;
    		cout << "It took you " << nTry << " trys." << endl;
    		return 1;
    	}
    
    	else if (nCheck < nNum)
    	{	
    		cout << "Too low try Again." << endl << endl;
    		return 0;
    	}
    
    	else if (nCheck > nNum)
    	{	
    		cout << "Too high try again." << endl << endl;
    		return 0;
    	}	
    }
    
    int main()
    {
    	nNum = GetRandomNumber(1, 100);
    	
    	do 
    	{
    		for (nTry = 0; nTry < 10; nTry++)
    		{
    			cout << "You have " << 10 - nTry << " trys to guess a number 1-100: ";
    			cin >> nCheck;	
    			Check();
    		}
    	}
    	while (Check() == 0);
    
    	cin.clear();
    	cin.ignore(255, '\n');
    	cin.get();
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (nTry = 0; nTry < 10; nTry++)
    ..
    > while (Check() == 0);
    Use only one loop.

    A loop that performs a number of reties
    - until a limit is reached
    - OR Check() returns true.

    Your inner loop cannot exit at present, since you don't check the result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Quote Originally Posted by Salem View Post
    > for (nTry = 0; nTry < 10; nTry++)
    ..
    > while (Check() == 0);
    Use only one loop.

    A loop that performs a number of reties
    - until a limit is reached
    - OR Check() returns true.

    Your inner loop cannot exit at present, since you don't check the result.
    Ahh I see! I gave it a try with this and it is still continuing.

    Code:
    //other	
    
    if (nCheck == nNum)
    	{	
    		cout << "Correct!" << endl;
    		cout << "It took you " << nTry << " trys." << endl;
    		return nEnd = 1;
    	}
    
    	else if (nCheck < nNum)
    	{	
    		cout << "Too low try Again." << endl << endl;
    		return nEnd = 0;
    	}
    
    	else if (nCheck > nNum)
    	{	
    		cout << "Too high try again." << endl << endl;
    		return nEnd = 0;
    	}	
    
    //other 
    
    do
    	{
    		nTry++;
    		cout << "You have " << 10 - nTry << " trys to guess a number 1-100: ";
    		cin >> nCheck;	
    		Check();
    	}
    	while (nTry < 10 || nEnd == 0);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about

    while ( nTry < 10 && Check() != 1 )

    Using your first version.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Ahhh worked perfectly thanks. Any reason that works and the OR doesnt?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because the value of nEnd is irrelevant while nTry < 10

    You're going round the loop 10 times whether you like it or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. hey guys! im in italy!
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-17-2004, 01:44 AM
  3. Hey Guys
    By valar_king in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-13-2004, 06:54 PM
  4. Replies: 34
    Last Post: 11-26-2001, 01:17 PM
  5. Hey Guys
    By D4050 in forum C Programming
    Replies: 0
    Last Post: 10-01-2001, 06:20 AM