-1

I'm trying to make a dice program in C++. In that program, I have a function called "bool_if" (Don't ask me why I named some things the way I did, I made this program for me and I don't want to change variable/function names). I want bool_if() to check if any elements in the array rolled_die[] is the same. Sorry if that doesn't make sense, here's the source code (I commented in part of the source code for what I want):

#include <cstdlib> 
#include <ctime>
#include <iostream>
#include <unistd.h>
#include <fstream>

using namespace std;

void clear()
{
  cout<<"\033[2J\033[1;1H";
}

bool rolled_die[5];

void dice() {
  if (die[0] == die[1] || die[0] == die[2] || die[0] == die[3] || die[0] == die[4] || die[0] == die[5]) {
    rolled_die[0] = true
  }
  else if (die[1] == die[2] || die[1] == die[3] || die[1] == die[4] || die[1] == die[5]) {
    rolled_die[1] = true
  }
  else if (die[2] == die[3] || die[2] == die[4] || die[2] == die[5]) {
    rolled_die[2] = true
  }
  else if (die[3] == die[4] || die[3] == die[5]) {
    rolled_die[3] = true
  }
  else if (die[4] == die[5]) {
    rolled_die[4] = true
  }
}

int finished_score = 0

void bool_if {
  if (rolled_die[0] == true || rolled_die[1] == true || rolled_die[2] == true || rolled_die[3] == true || rolled_die[4] == true) {
    finished_score++;
  }
  /* In this part, I need to check if rolled_die[0] is equal to rolled_die[1], or rolled_die[2], etc all the way up to rolled_die[5]. */


int main()
{
  srand(time(0));

  int die[6] = { (rand()%6)+1,(rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1 };
  int bet;

  ifstream a_file ("Score.txt");
  if ( !a_file.is_open() ) {
    clear();
    cout<<"Score file could not be loaded.  Maybe file doesn't exist?\n";
    return 0;
  }

  int score;
  a_file>> score;

  clear();
  cout<<"How much would you like to bet?\nRemember, you only have $"<< score <<"!\nEnter your bet: ";
  cin>> bet;

  if ( bet > score ) {
    clear();
    cout<<"You bet more money then you have!\n";
    return 0;
  }

  clear();
  cout<<"Rolling...\n";
  usleep(0.1 * 1000000);
  cout<<"First die: " << die[0] <<"\n";
  usleep(1.3 * 1000000);
  cout<<"Second die: " << die[1] <<"\n";
  usleep(0.3 * 1000000);
  cout<<"Third die: " << die[2] <<"\n";
  usleep(2 * 1000000);
  cout<<"Fourth die: " << die[3] <<"\n";
  usleep(0.3 *1000000);
  cout<<"Fifth die: " << die[4] <<"\n";
  usleep(1.2 * 1000000);
  cout<<"Sixth die: " << die[5] <<"\n";
  cout<<"\n\nPress enter to see how you fared.";
  cin.get();
  clear();

  return 0;
}

Also please tell the function dice() doesn't work. I want dice() to check for any pairs.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
Predictability
  • 2,919
  • 4
  • 16
  • 12
  • 1
    See http://stackoverflow.com/questions/6248044/c-comparing-two-vectors – Joe Nov 19 '12 at 21:54
  • Any reason you're not just using two for loops? – Brendan Long Nov 19 '12 at 21:55
  • 6
    `I don't want to change variable/function names` Then you're too inflexible for _my_ time! – Lightness Races in Orbit Nov 19 '12 at 21:57
  • I realize I don't understand what you attempting to do what so ever. should your die be fair? – andre Nov 19 '12 at 22:08
  • 2
    You lost me around the time you said 'don't ask me why I named some things the way I did' – john Nov 19 '12 at 22:12
  • @John I thought some people would ask why I named some variables or functions the way I did (Like bool_if), and I just wanted to say that I didn't want to explain why I did that because I just want an answer for my question. – Predictability Nov 19 '12 at 22:22
  • 2
    @Predictability I answered a question just this morning for someone who had a logic error in his code. His confused thinking was demonstrated by the poor variable names he'd chosen. To me your code (and perhaps your thinking) has exactly the same problem. Choosing good names is important because it forces you to think clearly about the problem you are trying to solve and the way you intend to solve it. – john Nov 19 '12 at 22:30
  • I think if you remove all the "else" in your dice(), the function works. – digit plumber Nov 20 '12 at 00:56
  • 1
    @Predictability, while you may have made this program for you, the moment you ask someone else to read your code your code is no longer "just for you". – Yakk - Adam Nevraumont Nov 20 '12 at 18:10

2 Answers2

1
int die[6] = { (rand()%6)+1,(rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1 };

Replace that with this:

int die[6] = {1, 2, 3, 4, 5, 6};
std::random_shuffle(die, die+6);

The Code below runs just fine for me: http://ideone.com/F4Eefj

andre
  • 7,018
  • 4
  • 43
  • 75
1

You can dramatically simplify this code by using for loops. For example, this compares every index of the array to every index after that one (which appears to be what you're doing manually):

#include <iostream>

int main(int argc, char** argv) {
    int die[] = { 4, 5, 6, 4, 5, 3 };
    const size_t LENGTH = 6;

    for(size_t i = 0; i < LENGTH; ++i) {
       for(size_t j = i + 1; j < LENGTH; ++j) {
           if(die[i] == die[j]) {
               std::cout << "Found a pair at (" << i << ", " << j << ")" << std::endl;
           }
       }
    }

    return 0;
}

Your dice() function seems to do this, but you're not calling it anywhere.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • When I try to compile I get this error: dice.cpp:12:3: error: expected unqualified-id before ‘for’ dice.cpp:12:19: error: ‘i’ does not name a type dice.cpp:12:26: error: ‘i’ does not name a type dice.cpp:23:6: error: expected constructor, destructor, or type conversion before ‘(’ token – Predictability Nov 22 '12 at 02:29
  • @Predictability It works for me. I updated my answer with the complete code I used to compile (I basically just wrapped it in `main()`, adding an `#include` for `cout` and declared `die`. – Brendan Long Nov 22 '12 at 18:17
  • Maybe you're re-using `i` or there's an error of some other kind right before the first `for` loop? You won't be able to just copy and paste this code, you need to understand what it's doing and add it to your code. – Brendan Long Nov 22 '12 at 18:19