loop is not working

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 3944 Views - Last Post: 13 October 2010 - 08:51 AM Rate Topic: -----

#1 Phillyfries   User is offline

  • New D.I.C Head

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

loop is not working

Posted 11 October 2010 - 07:11 AM

My program is ignoring information in the loop. Any suggestions?

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstdlib>
#include <cmath>

using namespace std;

    const int NUMBER_GAMES = 10000;
    int dice1 = 1 + rand() % 6; // 1st dice roll
    int dice2 = 1 + rand() % 6; // 2nd dice roll
    int dice3 = 1 + rand() % 6; // 3rd dice roll
    int dice4 = 1 + rand() % 6; // 4th dice roll
    
int main()
{
    //variables
    int numberWins = 0;
    int numberLosses = 0;
    int point = 0;
    int dice1, dice2, dice3, dice4;
    int roll = dice1 + dice2;
    int sum = roll; // compute sum of die values
    int newRoll = dice3 + dice4;
    

    // conditions of the game
    for (int roll = 1; roll <= 10000; roll++)

         cout << "Start the roll: \n";
         sum = roll;
     if ((dice1 + dice2) == 7 || ( dice1 + dice2) == 11)
      {
        numberWins = numberWins + 1;
        cout << "You won !" << endl;
      }
      if ((dice1 + dice2) == 2 || ( dice1 + dice2) == 3|| ( dice1 + dice2) == 12)
      {
         numberLosses = numberLosses +1;
         cout << "You lose !" << endl;
      }
      // Establish the Point
      else if((dice1 + dice2) == 4 || ( dice1 + dice2) == 5|| ( dice1 + dice2) == 6 || ( dice1 + dice2) == 8||( dice1 + dice2) == 9 ||( dice1 + dice2) == 10)
        {
           roll = point;
           cout << "This is your point" <<endl;
           do
           {
            newRoll = dice3 + dice4;
           }
           while (newRoll != 7 && newRoll != point);

           if ( newRoll == 7)
           {
                numberLosses = numberLosses +1;
                cout << "You did not make your point. You lost" <<endl;
           }
           else if ( newRoll == point)
           {
                numberWins = numberWins + 1;
                cout << "You made your point. You win" <<endl;
           }
        }

        return 0;
}
    int probability ()
    {
        double winningPercentage;
        int numberWins;
        winningPercentage = numberWins/NUMBER_GAMES;
        system("pause");
        return 0;
    }


This post has been edited by JackOfAllTrades: 11 October 2010 - 07:16 AM
Reason for edit:: Fixed code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: loop is not working

#2 Alex6788   User is offline

  • kitties == adorable


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

Re: loop is not working

Posted 11 October 2010 - 07:13 AM

The code tags are wrong, Like this :code:

This post has been edited by Alex6788: 11 October 2010 - 07:15 AM

Was This Post Helpful? 2
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: loop is not working

Posted 11 October 2010 - 07:17 AM

Please be more specific. You have multiple loops, for one thing.
Was This Post Helpful? 1
  • +
  • -

#4 Phillyfries   User is offline

  • New D.I.C Head

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

Re: loop is not working

Posted 11 October 2010 - 07:20 AM

The program repeats "start the roll" 10000 times.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: loop is not working

Posted 11 October 2010 - 07:25 AM

View PostPhillyfries, on 11 October 2010 - 01:20 PM, said:

The program repeats "start the roll" 10000 times.



// conditions of the game
    for (int roll = 1; roll <= 10000; roll++)


You need to add {} otherwise you will only execute the next line multiple times.

You also have quite a few uninitialized memory variables in your code.

Jim
Was This Post Helpful? 1
  • +
  • -

#6 Phillyfries   User is offline

  • New D.I.C Head

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

Re: loop is not working

Posted 11 October 2010 - 07:46 AM

I added the {}. I still the same problem (The program repeats "start the roll" 10000 times. )
Was This Post Helpful? 0
  • +
  • -

#7 Alex6788   User is offline

  • kitties == adorable


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

Re: loop is not working

Posted 11 October 2010 - 07:51 AM

In your header files you declare cstdlib twice

#include <iostream>
#include <cstdlib> // Here
#include <ctime>
#include <cstdlib>  // Here declared twice
#include <cmath>



Hope that helped.

This post has been edited by Alex6788: 11 October 2010 - 07:52 AM

Was This Post Helpful? 1
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: loop is not working

Posted 11 October 2010 - 07:55 AM

Please post the code where you added the {}.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 Phillyfries   User is offline

  • New D.I.C Head

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

Re: loop is not working

Posted 11 October 2010 - 07:58 AM

// conditions of the game
    for (int roll = 1; roll <= 10000; roll++)
      {
         cout << "Start the roll: \n";
         sum = roll;
      }


Was This Post Helpful? 0
  • +
  • -

#10 Mohanddo   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 07-July 09

Re: loop is not working

Posted 11 October 2010 - 08:01 AM

You have already declared roll, why are you declaring it again for the loop?
Was This Post Helpful? 1
  • +
  • -

#11 Phillyfries   User is offline

  • New D.I.C Head

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

Re: loop is not working

Posted 11 October 2010 - 08:08 AM

Ok. Now how do I start the roll process?
Thanks
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: loop is not working

Posted 11 October 2010 - 08:20 AM

You probably want your {} like:
for (int roll = 1; roll <= 10000; roll++)
{

         cout << "Start the roll: \n";
         sum = roll;
     if ((dice1 + dice2) == 7 || ( dice1 + dice2) == 11)
      {
        numberWins = numberWins + 1;
        cout << "You won !" << endl;
      }
      if ((dice1 + dice2) == 2 || ( dice1 + dice2) == 3|| ( dice1 + dice2) == 12)
      {
         numberLosses = numberLosses +1;
         cout << "You lose !" << endl;
      }
      // Establish the Point
      else if((dice1 + dice2) == 4 || ( dice1 + dice2) == 5|| ( dice1 + dice2) == 6 || ( dice1 + dice2) == 8||( dice1 + dice2) == 9 ||( dice1 + dice2) == 10)
        {
           roll = point;
           cout << "This is your point" <<endl;
           do
           {
            newRoll = dice3 + dice4;
           }
           while (newRoll != 7 && newRoll != point);

           if ( newRoll == 7)
           {
                numberLosses = numberLosses +1;
                cout << "You did not make your point. You lost" <<endl;
           }
           else if ( newRoll == point)
           {
                numberWins = numberWins + 1;
                cout << "You made your point. You win" <<endl;
           }
        }
}// end of for



Also look here
  int dice1, dice2, dice3, dice4;
    int roll = dice1 + dice2;



in this snippet you are using your variables before you initialize them. You should always initialize your variables when defining them.

   int dice1 = 0;
   int dice2 = 0;
   int dice3 = 0; 
   int dice4 = 0;
   int roll = dice1 + dice2;



Jim
Was This Post Helpful? 1
  • +
  • -

#13 aaa111   User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: loop is not working

Posted 11 October 2010 - 08:21 AM

You have to put {} within the portion which you want to repeat,i guess you want to repeat the 30-63 line 10000 times,also none of your if else if chain working at all.
Also if you want dice1...dice4 values to change randomly within this 10000 iteration you have to generate the random number within that {} brace.If you don't want to produce same random number follow this tutorial:
http://www.dreaminco...random-numbers/

This post has been edited by aaa111: 11 October 2010 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: loop is not working

Posted 11 October 2010 - 08:31 AM

    const int NUMBER_GAMES = 10000;
    int dice1 = 1 + rand() % 6; // 1st dice roll
    int dice2 = 1 + rand() % 6; // 2nd dice roll
    int dice3 = 1 + rand() % 6; // 3rd dice roll
    int dice4 = 1 + rand() % 6; // 4th dice roll
    
int main()
{
    //variables
    int numberWins = 0;
    int numberLosses = 0;
    int point = 0;
    int dice1, dice2, dice3, dice4;
    int roll = dice1 + dice2;
    int sum = roll; // compute sum of die values
    int newRoll = dice3 + dice4;
    



In the above snippet you have GLOBAL variables hidden by your local variables(they have the same name). I would remove the GLOBALS and stick with local variables.

Jim
Was This Post Helpful? 2
  • +
  • -

#15 Phillyfries   User is offline

  • New D.I.C Head

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

Re: loop is not working

Posted 11 October 2010 - 08:58 AM

Added the srand. Not sure about comment re lines 30-63. Amended code below. The "start the roll" 10000 times is still there.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

    const int NUMBER_GAMES = 10000;
    int dice1 = 1 + rand() % 6; // 1st dice roll
    int dice2 = 1 + rand() % 6; // 2nd dice roll
    int dice3 = 1 + rand() % 6; // 3rd dice roll
    int dice4 = 1 + rand() % 6; // 4th dice roll

int main()
{
    //variables
    int numberWins = 0;
    int numberLosses = 0;
    int point = 0;
    int dice1 =0, dice2 =0, dice3 =0, dice4 =0;
    int roll = dice1 + dice2;
    int sum = roll; // compute sum of die values
    int newRoll = dice3 + dice4;


    // conditions of the game
    for (int roll = 1; roll <= 10000; roll++)
    {
         srand(time(0));
         cout << "Start the roll: \n";
         sum = dice1 + dice2;
         cout << "The roll was " << sum << ' ' << endl;

     if ((dice1 + dice2) == 7 || ( dice1 + dice2) == 11)
      {
        numberWins = numberWins + 1;
        cout << "You won !" << endl;
      }
      if ((dice1 + dice2) == 2 || ( dice1 + dice2) == 3|| ( dice1 + dice2) == 12)
      {
         numberLosses = numberLosses +1;
         cout << "You lose !" << endl;
      }
      // Establish the Point
      else if((dice1 + dice2) == 4 || ( dice1 + dice2) == 5|| ( dice1 + dice2) == 6 || ( dice1 + dice2) == 8||( dice1 + dice2) == 9 ||( dice1 + dice2) == 10)
        {
           roll = point;
           cout << "This is your point" <<endl;
           do
           {
            newRoll = dice3 + dice4;
           }
           while (newRoll != 7 && newRoll != point);

           if ( newRoll == 7)
           {
                numberLosses = numberLosses +1;
                cout << "You did not make your point. You lost" <<endl;
           }
           else if ( newRoll == point)
           {
                numberWins = numberWins + 1;
                cout << "You made your point. You win" <<endl;
           }
        }

 }       return 0;
}
    int probability ()
    {
        double winningPercentage;
        int numberWins;
        winningPercentage = numberWins/NUMBER_GAMES;
        system("pause");
        return 0;
    }


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2