Hey guys help with HiLo program

This is a discussion on Hey guys help with HiLo program within the C++ Programming forums, part of the General Programming Boards category; Hey guys im writing a simply HiLo game. I cant get my do while statement to work correctly. I want ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01: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, 02:44 AM
  3. Hey Guys
    By valar_king in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-13-2004, 07: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, 07:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21