Any ideas on ways to improve it?
I am new to C++, well programming in general.
Thanks in advance.
BJ

#include <cstdlib>
#include <iostream>

using namespace std;
int c = 0;
char p;
int g =0;
int n = 0;
void D4(int A, int B);
int main()

{
    cout<<"Dice emulator writen in C++ by BJ Halterman\n";
    while (c!=7)
    {
    cout<<"\t 1...4 Side\n"; //menu
    cout<<"\t 2...6 Side\n";
    cout<<"\t 3...8 Side\n";
    cout<<"\t 4...10 Side\n";
    cout<<"\t 5...20 Side\n";
    cout<<"\t 6...100 Side\n";
    cout<<"\t 7...Exit\n";
    cout<<"Enter your selection please...";
    cin>>c;
          if(c==1){
                   cout<<"Enter the number of dice to roll...";
                   cin>>n;
                   cout<<"in groups of how many?  ";
                   cin>>g;
                   D4(n,4);
                  }
          if(c==2){
                   cout<<"Enter the number of dice to roll...";
                   cin>>n;
                   cout<<"in groups of how many?  ";
                   cin>>g;
                   D4(n,6);
                   }
          if(c==3){
                   cout<<"Enter the number of dice to roll...";
                   cin>>n;
                   cout<<"in groups of how many?  ";
                   cin>>g;
                   D4(n,8);
                   }
          if(c==4){
                   cout<<"Enter the number of dice to roll...";
                   cin>>n;
                   cout<<"in groups of how many?  ";
                   cin>>g;
                   D4(n,10);
                   }
          if(c==5){
                   cout<<"Enter the number of dice to roll...";
                   cin>>n;
                   cout<<"in groups of how many?  ";
                   cin>>g;
                   D4(n,20);
                   }
          if(c==6){
                   cout<<"Enter the number of dice to roll...";
                   cin>>n;
                   cout<<"in groups of how many?  ";
                   cin>>g;
                   D4(n,100);
                   }
          if (c>7||c<1)     {//error
                   cout<<"That is not a valid selection!\n"<<"\n";
                   }
    }
    cout<<"Press any key to exit";
    cin.get();
}
void D4(int A,int B)
{
     int rn=0;
     int T=0;
     int t= A/g;
     t= A/t;
     int gr=t;
     int x=0;
     while(x<(A/g))
     {
               rn = ((rand()%(gr*B))+1);
               cout<<"\t"<<x+1 <<"...."<<rn<<"\n";
               x=x+1;
     }
     cout<<"\n";
}

Recommended Answers

All 3 Replies

Where there is repetition, try to separate the common code. For example,

cout<<"Enter your selection please...";
      cin>>c;

      cout<<"Enter the number of dice to roll...";
      cin>>n;

      cout<<"in groups of how many?  ";
      cin>>g;

      if ( c == 1 )
      {
         D4(n,4);
      }
      else if ( c == 2 )
      {
         D4(n,6);
      }
      else if ( c == 3 )
      {
         D4(n,8);
      }
      else if ( c == 4 )
      {
         D4(n,10);
      }
      else if ( c == 5 )
      {
         D4(n,20);
      }
      else if ( c == 6 )
      {
         D4(n,100);
      }
      else //error
      {
         cout << "That is not a valid selection!\n\n";
      }
   }

Pass parameters instead of using global variables. Choose variable names that make the code easier to understand.

just use else instead of if (c>7||c<1)

if(...)
  //if case
else
  //else case

Maybe U should try with switch
.
.
.
cin>>c;
switch(c)
{
case 1:
cout<<"Enter the number of dice to roll...";
cin>>n;
cout<<"in groups of how many? ";
cin>>g;
D4(n,4);
break;
.
.
.
default:
cout<<"That is not a valid selection!\n"<<"\n";
break;
}

and also try to avoid globals

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.