RE: [Dev-C++] Please help with program bugs, i tried, no luck.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ya...> - 2001-01-19 11:21:09
|
My ANSI C++ suggestions:
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of
> ropbert d baugh
> Sent: Friday, January 19, 2001 3:02 AM
> To: dev...@li...
> Subject: Re: [Dev-C++] Please help with program bugs, i tried, no luck.
>
>
> Thanks to Ionnis... Here is the completed game. Enjoy!
> #include <iostream.h>
#include <iostream>
> #include <stdlib.h>
or #include <cstdlib> better
> #include <time.h>
#include <ctime>
>
> int roll(void); // protypes
>
> int dice_1; // Declared globals
> int dice_2;
Try to avoid the use of global variables.
>
> int roll()
> {
using std::srand;
using std::rand;
> // srand(6);
srand(time(0)); // If srand() is given a standard value, rand() produces the
same values
// every time the program is executed.
> do
> {
> dice_1 = (rand() + 1); // dev-c++ does not have random function
It has, the rand() of the standard library. rand() produces a value from 0
to RAND_MAX. Usually RAND_MAX equals to 32767 but if you want to be portable
use RAND_MAX. So, if you want to produce values from 0 to 2 (3 values) use:
int dice_1=rand()/(RAND_MAX/3);
> }while(dice_1 >7);
// Use the trick i told you above.
> do
> {
> dice_2 = (rand() + 1); // that i can find . use bcc55 for this
> }while(dice_2 >7);
// The same
> return dice_1; // anyone else have information on
> random??
> }
>
> int main()
> {
> int through = 0, point = 0;;
> int lose =0, first = 1, win ;
>
> do
> {
> win = 0;
> roll();
> cout<<" the value rolled is : "<<(dice_1+dice_2)<<endl;
> cout <<" the first die was : "<<dice_1<<endl; // changed to int
> cout<<" the second die was: "<<dice_2<<endl;
>
> if(first == 1) point = (dice_1 + dice_2);
> if(dice_1+dice_2 == 11 && first ==1)
> {
> lose =1;
> cout<<endl<<"Sorry You Lose"<<endl;
> through = 1;
> }
> if(dice_1+dice_2 == 7 && first == 0)
> {
> lose =1;
> cout<<endl<<"Sorry You Lose"<<endl;
> through = 1;
> }
> if(first == 0 && (dice_1+dice_2) == point || first == 1 && point ==7)
> {
> cout<<endl<<"HAY You WIN!!!"<< endl;
> first = win = 1;
> }
> if(win != 1)first = 0;
> cout<<"Enter 1 to quit " ;
> cin>>through;
> }while(through != 1 || lose ==0);
>
> return 0; // screen in console
> mode
I did not check the whole program, see my suggestions and fix it. :)
Keeep coding,
Ioannis
* Ioannis Vranos
* Programming pages: http://members.nbci.com/noicys
* Alternative URL: http://run.to/noicys
|