Thread: Loop is running infinitely.please help...

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    41

    Loop is running infinitely.please help...

    My program is supposed to end when both a and b are 1.
    Please tell me where it is wrong.
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    int roll()
    {
        return rand()%6+1;
    }
    int main()
    {
        srand(time(NULL));
        int a,b;
        a=roll();b=roll();int count=1;
        bool snakeyes=(a==1&&b==1);
        while(!snakeyes)
        {
    	cout<<"values of dice are : "<<a<<" and "<<b<<endl;
    	a=roll();b=roll();
    	++count;
        }
        cout<<"values of dice are : "<<a<<" and "<<b<<" , which means snake eyes!\n";
        cout<<"it took "<<count<<" rolls.\n";
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You never update the value of snakeyes inside the loop. The way you have it now you will either never enter the loop, or you will never exit the loop.

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    Thanks Shakti, i forgot about that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whenever i delete a node and call display loop runs infinitely
    By Naveen Bansal in forum C Programming
    Replies: 1
    Last Post: 09-30-2012, 12:05 AM
  2. loop is running twice
    By Nikitaatwork in forum C Programming
    Replies: 4
    Last Post: 11-08-2011, 06:42 PM
  3. Can't get loop to keep running
    By mytrademark in forum C Programming
    Replies: 2
    Last Post: 11-01-2011, 06:08 PM
  4. Running a program in a loop
    By Robert_Sitter in forum C# Programming
    Replies: 7
    Last Post: 01-27-2006, 10:09 AM
  5. Infinitely looping inclusion!
    By kidburla in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2005, 07:40 PM