Inputting an integer array.

Hello,

I'm working on a Yatzee game, and I'm currently stuck trying to take in which dice the user wants to re-roll.

cout << "How many dice would you like to re-roll?" << endl;
int r;
cin >> r;
cout << "Which dice would you like to re-roll? (ex. 1,4,5..)" << endl;
vector<int> rerolls;
cin >> rerolls;

Obviously that doesn't work, otherwise I wouldn't be posting ;). But I guess I know how I could make a for loop saying: for(i=0; i> rerolls[i];}; But as simple as that would be, I'm interested in figuring out different ways to do things, such as taking all the numbers in at once.

If anyone can provide assistance, I would greatly appreciate it! Thank you..

Comments

  • you just have a slight syntax error, this should do the trick.
    for(int x=0, x> y;

    now here you need to call or first make a rerolls functions that will take integer y and give you a new value for that dye. the for function allows a loop that allows for reuse of code.

    Hello,
    :
    : I'm working on a Yatzee game, and I'm currently stuck trying to take
    : in which dice the user wants to re-roll.
    :
    : cout << "How many dice would you like to re-roll?" << endl;
    : int r;
    : cin >> r;
    : cout << "Which dice would you like to re-roll? (ex. 1,4,5..)" <<
    : endl;
    : vector<int> rerolls;
    : cin >> rerolls;
    :
    : Obviously that doesn't work, otherwise I wouldn't be posting ;).
    : But I guess I know how I could make a for loop saying: for(i=0; i> rerolls[i];}; But as
    : simple as that would be, I'm interested in figuring out different
    : ways to do things, such as taking all the numbers in at once.
    :
    : If anyone can provide assistance, I would greatly appreciate it!
    : Thank you..


  • "But as simple as that would be, I'm interested in figuring out different ways to do things, such as taking all the numbers in at once."

    Here's a short program that illustrates multiple entry for dice rerolls:

    [code]
    /*****************************************/
    /* written in Dev-C++ 4.9.9.2 by Ed Hall */
    /* project set up as console C++ */
    /*****************************************/

    #include
    #include
    #include // added to use time() for random seed

    using namespace std;

    int main(int argc, char *argv[])
    {
    srand(time(0)); // sets random number generator to different seeds
    char choices[11]; // string to be used for reroll choices
    int die[5], choice[5]; // dice and "to change" variables
    int i, j; // i used in for loops, j used for sscanf()

    printf("Initial values for dice:

    "); // initialize routine
    for (i=0;i<5;i++){
    die[i]=(rand()%6)+1; // sets each die to random number (1-6)
    printf(" Die %d equals %d.
    ", i+1, die[i]); // prints initialized values
    }

    printf("
    Enter dice to reroll
    (example: 1 3 4): ");
    fgets(choices, 10, stdin); // accepts input of dice values to reroll - limited to 10 characters
    j=sscanf(choices, "%d %d %d %d %d", &choice[0], &choice[1], &choice[2], &choice[3], &choice[4]);
    for (i=0;i<j;i++) // loop to change chosen dice
    if (choice[i]<6 && choice[i]>0) // minor input error checking
    die[choice[i]-1]=(rand()%6)+1;

    printf("
    Final values for dice:

    "); // prints final values
    for (i=0;i<5;i++) // loops through dice
    printf(" Die %d equals %d.
    ", i+1, die[i]);

    printf("


    "); // places a few lines before "pause" message
    system("PAUSE"); // holds console window open, if needed
    return EXIT_SUCCESS; // returns 0 to signify no errors in execution
    }
    [/code]

    Take Care,
    Ed Hall
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories