CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Oct 2009
    Posts
    7

    Functions for Die

    So i have been trying to make a program that uses a function to roll a dice. I made the program so that the dice roll is within main but I'd like to make the dice roll a function all in itself so that i can use it for multiple things. This is what i have so far in the file attached and below copied and pasted:

    #include <iostream>
    #include <ctime> // for time function
    #include <cstdlib> // for rand and srand functions
    using namespace std;

    int target, roll;
    long count;

    int rollDie(int target, int roll) {
    long x;
    while(roll != target){
    roll = rand() &#37; 6 + 1;
    x++;
    }
    return x;
    }


    int main(){
    srand((int)time(0));
    cout << "Enter target (ctrl-z to exit): ";
    cin >> target;

    while (cin) {
    //display stats
    cout << endl << "target: " << target << endl;
    cout << "number of rolls: " << rollDie() << endl;

    //prompt and read next target
    cout << "enter target (ctrl-z to exit): ";
    cin >> target;
    }
    //exit
    cout << endl << "Exiting.." << endl;
    return 0;
    }
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured