1st real program in c++

This is a discussion on 1st real program in c++ within the C++ Programming forums, part of the General Programming Boards category; Hi, I am new to c++, and i have been teaching myself from the tutorials listed on this website, and ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    1st real program in c++

    Hi, I am new to c++, and i have been teaching myself from the tutorials listed on this website, and a book that I have. I just finished my first real program whose idea i did not get from a tutorial. It's a program that rolls dice for the boardgame Risk. I know other programs exist for this, i just wanted to try my hand at making my own. What i was wondering was, if anyone wouldnt mind taking a look at the code, or maybe compiling it, and offering some comments, copliments or criticisms. Thanks a lot!

    Here is the code.
    Code:
    /*****************************************************************************
    * This program is designed to facilitate the process of rolling dice in the
    * boardgame Risk.  The program asks the user how many armies should attack, and
    * how many should defend, and how many rounds this should continue for.  There
    * is also a simple dice roll function included in the program.  Written by
    * Nathan Petts 3.7.06
    ******************************************************************************/
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <windows.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    
    int diceroll (int& dicerollrun); //this function rolls y n-sided die x times
    
    int riskroll (int& riskrun);     //this function exectues the process of 
                                     // determening the winner of an attack in risk
    
    void clrscr (void);              // this windows-specific function clears the
                                     // screen. Written by Sunlight.
    
    int main()
    {
        int run = 1; //controlls the loop that keeps the program running
        int input = 0; // menu input
        int dicerollrun; // controlls the loop that keeps the dice roll funct. up
        int riskrun;     // controlls the loop that keeps the riskroll fucnt. up
        
        do {
        cout << "Welcome to RiskCalc. Written by Nathan Petts 3.7.06" << endl;
        cout << "Please make your selection" << endl;
        cout << "1: Risk Calculations" << endl;
        cout << "2: Diceroll" << endl;
        cout << "0: Exit" << endl;
        cin >> input;
        
             switch (input)
             {
               case 1:
                    do {
                        riskrun = 1;
                    riskroll (riskrun); // calls the riskroll function
                    }
                    while (riskrun !=0);
                   
                    break;
               case 2:
                    do 
                   {
                    dicerollrun = 1;
                    diceroll (dicerollrun); //calls the diceroll function
                    }
                    while (dicerollrun !=0);
                    break;
               case 0:
                    run = 0;
                    break;
               default:
                       cout << "invalid input" << endl;
                       run = 0;
                       break;
           
           }   
        }
        while (run != 0);
        
        return 0;
    }
    
    
    int diceroll (int& dicerollrun)
    {
         int numdice; //number of dice to roll
         int numsides; // number of sides to each die
         int l;    //loop controll
         srand(time(NULL)); // seed rand w/ time
         cout << "Please enter the number of dice sides. Enter 0 to exit to Main Menu" << endl;
         cin >> numsides;
         cout << "Please enter number of dice." << endl;
         cin >> numdice;
         
         
         if (numsides != 0)
         {                        
           cout << "The dice rolls are:" << endl;
           for (l = 0; l < numdice; l++)
           {
               cout << rand () %numsides + 1 << endl;
               
           }
           system ("pause");
           clrscr();
        }
        else 
        {
            dicerollrun = 0;
            clrscr();
        }
    return dicerollrun;
    }
    
    int riskroll (int& riskrun)
    {
        int numattackers; // the number of attacking armies
        int numdefenders; // the number of defending armies
        int attackingarmieslost = 0; // number of attacking armies lost
        int defendingarmieslost = 0; // number of defending armies lost
        int rounds;             // the number of rounds the dice will be rolled
        int x;            // for-loop control attackarray
        int y;            // for-loop control defendarray
        int z;            // for-loop control compairing the arrays
        int n;            // for-loop controll for number of rounds
        srand(time(NULL)); // seeding random
        int temp;          //temp int for sorting
        
        cout << "enter number of rounds to attack. enter 0 to exit" << endl; 
        cin >> rounds;
        cout << "enter number of attacking armies. enter 0 to exit" << endl;
        cin >> numattackers;
        cout << "enter number of defending armies. enter 0 to exit" << endl;
        cin >> numdefenders;
        
        int attackarray [numattackers];    // the dice rolls of the attackers
        int defendarray [numdefenders];    // the dice rolls of the defenders
        
        if (numattackers != 0 && numdefenders != 0 && rounds != 0)
        {
        for (n = 0; n < rounds; n++)
        {
           for (x = 0; x < numattackers; x++)
            {
            attackarray [x] = rand () %6 + 1;
            }
        
        
           // sorts attackarray
           for(int x=0; x< numattackers; x++)
    	   {
    		for(int y=0; y<numattackers-1; y++)
    		{
    			if(attackarray[y]<attackarray[y+1])
    			{
    				int temp = attackarray[y+1];
    				attackarray[y+1] = attackarray[y];
    				attackarray[y] = temp;
    			}
    	     }
    	    }
            /*
            cout << "these are the attackers rolls" << endl;
        
            for (x = 0; x < numattackers; x++)
            {
            cout << attackarray [x] << endl;
            }*/
        
            for (y = 0; y < numdefenders; y++)
            {
                defendarray [y] = rand () %6 + 1;
            }
       
            // sorts the defendarray
             for(int x = 0; x < numdefenders; x++)
    	     {
    		        for(int y=0; y<numdefenders-1; y++)
    		        {
                            if(defendarray[y]<defendarray[y+1])
    			            {
                            int temp = defendarray[y+1];
    				        defendarray[y+1] = defendarray[y];
    				        defendarray[y] = temp;
                            }
                    }
    	     }
             /*cout << "these are the defender rolls" << endl;
        
             for (y = 0; y < numdefenders; y++)
             {
                    cout << defendarray [y] << endl;
             }*/
    
             for (z = 0; z < numdefenders; z++)
                 {
                 if (defendarray [z] >= attackarray [z])
                 {
                           attackingarmieslost++;
                 }
                 else 
                 {
                      defendingarmieslost++;
                 }
                 } 
                 
                 
             }      
       cout << "the attackers lost " << attackingarmieslost << " armies." << endl;
       cout << "the defenders lost " << defendingarmieslost << " armies." << endl;
       system ("pause");
       clrscr();
       riskrun = 1;
       }
       else 
       {
         riskrun = 0;
         clrscr();
       }   
    
        return riskrun;
    }
    
    void clrscr(void)
    {
        COORD                       coordScreen = { 0, 0 };
        DWORD                       cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO  csbi;
        DWORD                       dwConSize;
        HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, TEXT(' '), 
                                   dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
                                   dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You're only supposed to call srand() once in the execution of a program.

    It's a good program.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Mabe work on naming conventions for your variables

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The program looks good. One question is why are you returning a value when it is not being used. Also, in C++ you generally want to declare the variables only when you are ready to initialize and use them. So while it will work either way, the variable declarations should probably be moved to where they are first needed.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    the functions that return values are returning them so that the functions will continue to repeat untill terminated by the user. I dont think that this is a very elegant solution to that problem, however, so if there is another way, by all means, inform me.

    thanks for the note about variable declarations, i will modify the code accordingly.

    thanks for the comments.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    From what I can tell, you aren't using the return values. For example, you have this code:
    Code:
    diceroll (dicerollrun);
    It ignores the return value. It works because you pass the argument by reference and so dicerollrun is updated. You might as well have the functions return void instead of int because there is no need for a return value, the reference parameter handles the function output.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    heard and understood.

  8. #8
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Daved
    From what I can tell, you aren't using the return values. For example, you have this code:
    Code:
    diceroll (dicerollrun);
    It ignores the return value. It works because you pass the argument by reference and so dicerollrun is updated. You might as well have the functions return void instead of int because there is no need for a return value, the reference parameter handles the function output.
    I'd disagree with that slightly. rather then
    Code:
    void diceroll (int& dicerollrun);
    I'd do
    Code:
    int diceroll (void);
    since the original int& dicerollrun is used purely as a return type, that's what it should be.
    Code:
    int dicerollrun = diceroll();
    is much clearer and easier to read.

    Also, try to be a little more consistent with your indenting
    Code:
       if (numattackers != 0 && numdefenders != 0 && rounds != 0)
        {
        for (n = 0; n < rounds; n++)
        {
           for (x = 0; x < numattackers; x++)
            {
            attackarray [x] = rand () %6 + 1;
            }
    should be
    Code:
        if (numattackers != 0 && numdefenders != 0 && rounds != 0)
        {
            for (n = 0; n < rounds; n++)
            {
                for (x = 0; x < numattackers; x++)
                {
                    attackarray [x] = rand () %6 + 1;
                }
    it doesn't matter if you use
    Code:
    if (cond) {
        stuff();
    // or
    
    if (cond)
    {
        stuff();
    
    // or even
    
    if (cond)
        {
        stuff();
    but pick one style and stick to it. Other then that, it looks pretty good. lots of comments and correct use of standard library. like it!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> since the original int& dicerollrun is used purely as a return type, that's what it should be.

    I didn't read it closely enough to see that. I agree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 01:09 AM
  4. a tutorial on a voice communications program
    By Geo-Fry in forum C++ Programming
    Replies: 5
    Last Post: 08-12-2003, 08:31 AM
  5. Seeking Help on my 1st C++ class program!
    By YevGenius in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2003, 04:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21