Just a little code I have been working on. New to programming and learned about rand. I applied it in this program. Like I said I'm new to programming and if there is a *easier and more effective way* to write some of my code, please tell me. The part where there is massive sleeps, was me experimenting with some of it's capabilities, text that appears to be writting (probably the hard way!).

Thanks,
Derek

// By a complete newbie, Derek Adam
// Version 1, any comments will be appreciated, please note I'm a noob to programming 



#include <cstdlib>
#include <iostream>
#include <ctime>
#include <windows.h>

using namespace std;
void dice ();//dice game
void menu ();//menu duh
int u = 1; //upgrade dice max
int main()
{  
    menu ();
                      
    system("PAUSE");
    return EXIT_SUCCESS;
}
  


void dice()
{  
     
    
    char answer[1];
    srand((unsigned)time(0));    
    int hdice;    //human dice
    int cdice;//comp dice
    int hlife = 5;//human life
    int clife = 5;//comp life
    
    do{  
    cout << "A dice rolling simulation....\n\n";
    hdice = (rand()%6)+u;// random generator 1 is u so we can upgrade later
    cdice = (rand()%6)+1;//comp ""  ""
    
    cout << "You:\t" <<hdice << endl;// what you rolled
    Sleep(1000);
    cout << "Comp:\t" <<cdice << endl;//comp "" ""
    Sleep(1000);
    if (hdice == cdice) // draw
    {
                 cout << "Reroll, draw.\n";
                 }
                 else if(hdice>cdice) // we roll higher they lose a life
    {
                          clife = clife - 1;
                          cout << "\tLives\n";
                          cout << "Computer lives:  " << clife <<"\t\t "<< hlife << " Human lives\n";
                          Sleep(1000);
                          }
                          else// comp rolls higher we lose a life
                          {
                          
                          hlife = hlife - 1;
                          cout << "\tLives\n";
                          cout << "Computer lives:  " << clife <<"\t\t "<< hlife << " Human lives\n";
                                              
                          }
                                              
                          }while(hlife != 0 && clife != 0);

if (hlife>clife)//who wins?
{
                      char answer[1];
                      cout << "YOU \tWIN\n";
                      }
                      else
                      {
                          cout << "YOU \t LOSE\n";
                          }
                          
                          cout << "Would you like to try again?\n";
                          cout << "y or n\n\n";
                          cin >> answer[1];
                          if (answer[1] == 'y')
                          {
                                     
                                     dice();
                                     }
                                 else
                                 {
                                    menu();
                                     }    
}
void menu ()
{
     char choice[1];
     cout << "Welcome to the menu....";
     cout << "Please select a choice!\n";
     cout << "[d]ice game\t[q]uit\t[u]pgrade dice\n";//options
     cin >> choice[1];
     switch(choice[1])//Fun section messing with sleep and escape \b to generate typing text
     {
                   case 'd':
                        dice();
                        break;
                        case 'u':
                             cout << "Now upgrading \n";
                             Sleep(1000);
                             cout << ".";
                             Sleep(1000);
                             cout << "..";
                             Sleep(1000);
                             cout <<"...";
                             Sleep(1000);
                             cout << "C";
                             Sleep(1000);
                             cout << "\bCO";
                             Sleep(1000);
                             cout << "\b\bCOM";
                             Sleep(1000);
                             cout << "\b\b\bCOMP";
                             Sleep(1000);
                             cout << "\b\b\b\bCOMPL";
                             Sleep(1000);
                             cout << "\b\b\b\b\bCOMPLE";
                             Sleep(1000);
                             cout << "\b\b\b\b\b\bCOMPLET";
                             Sleep(1000);
                             cout << "\b\b\b\b\b\b\bCOMPLETE";
                             Sleep(1000);
                             cout << "\b\b\b\b\b\b\b\bCOMPLETE!\n\n";
                             Sleep(1000);
                             u = u+1;
                             menu();
                        
                        
                        case 'q':
                             cin.get();
                             break;
                  
                  }
     }

It's a bad idea to use SYSTEM PAUSE in code, as it's not efficient. A better way to do it is:

cin.get;

hope this helps somewhat.

Not 100% sure why you are using an array to store your character since you are only using 1. Also when you make an array char answer[1]; that means that it has a size of 1 and it starts at 0 so you would use it like if( answer[0] == 'y' ) .

Also you have answer being declared multiple times through your code when you only have to do it once as long as it is being used in a scope (not sure if this is the right word) lower than the one it was declared in.

For example

int main()
{
	char a = 'a'; //a is defined in all of the main() function but no where outside of it

	if( a == 'a' )
	{
		char b = 'b'; //b is only defined within this scope
	}

	if( a != b ) //b is not defined in this scope
	{
		char a; //a is already defined so this should give an error
		a = 'b';
	}

	return 0;
}
Comments
good clarification

Oh ya, dev automaticly adds that system(pause)
Thanks!

Thanks, ya I was confused with char's thanks for clearing that up, didn't know it started from 0! And my redundent declarations of answer, haha :-)


New question:

When ever I add more than one condition for the while loop it doesn't work...
It works if I just have while (pick != 'r'), I have tried pick != 'r','g','b'....
Can't figure it out or find a answer anwhere...
I want the program to run untill 'r' 'g' or 'b' is entered.....but when it runs it will just keep looping, thanks!

do
{
cout << "What color do you want to bet on?\n";
cout << "[r]ed\t[b]lue\t[g]reen\n";
cin >> pick;
switch(pick)
{
               case 'r':
                    cout << "You want red to win!\n";
                    break;
                    case 'g':
                         cout<< "You want green to win!\n";
                        break;
                         case 'b':
                              cout << "You want blue to win!\n";
                              break;
               }
}while (pick != 'r' || pick != 'g' || pick != 'b');

Edited 6 Years Ago by dadam88: Forgot something

Also you have answer being declared multiple times through your code when you only have to do it once as long as it is being used in a scope (not sure if this is the right word) lower than the one it was declared in.

For example

int main()
{
	char a = 'a'; //a is defined in all of the main() function but no where outside of it

	if( a == 'a' )
	{
		char b = 'b'; //b is only defined within this scope
	}

	if( a != b ) //b is not defined in this scope
	{
		char a; //a is already defined so this should give an error
		a = 'b';
	}

	return 0;
}

It won't give an error if you're trying to define variable twice if it's in other block of code. The second time you've defined var a is in the 'if'-construction, so we've got different a now as far the if-construction won't end. It is some kind of overloading (or something similar, I don't remember appropriate name).

i believe this line

while (pick != 'r' || pick != 'g' || pick != 'b');

should be

while (pick != 'r' && pick != 'g' && pick != 'b');