lottery program

im not sure plz help?

Page 1 of 1

10 Replies - 12161 Views - Last Post: 25 January 2008 - 09:21 AM Rate Topic: -----

#1 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

lottery program

Posted 25 January 2008 - 04:27 AM

As u can see.. im making a lottery game.. the thing is ..that each player before playing the game should have $1500
and each time a player inputs a number it gets deduct.. but the thing is i cant make it deduct it kept on 1500 ..anyone can correct or giv me some tips about this code plz and thank you.







#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;


void Start();
void GetResults ();
void Moneyz();


int a,b,maxrand,money,one,two ,three;
char d;
char c; 

void 
Start ()
{

a =0; b=0;  maxrand =4,money = 1500;
	
//the User has to select a difficulty level
cout<<"Please Select difficulty mode of your choice:\n"; 
cout<< "1: Easy	 (0-20)$200\n";
cout<< "2: Medium   (0-15)$500\n";
cout<< "3: Difficult(0-5 )$700\n";
cout<< "Or Type another key to quit\n";

cin >> c; // Read the user input
cout << "\n";



switch (c)
{
case '1' : 	maxrand =20; //The random number between
			  one;	// 0 and maxrand
  break;
case '2' : maxrand =15;
				two;
  break;
case '3' : maxrand =5;
			three;
	break;
default : exit(0);
	break;
}


srand ((unsigned)time(NULL));//init Rand() function
b=rand()%maxrand;// b gets the range 0 to maxrand
GetResults();


}

void Moneyz()
{

	if (c==1){
	one = 200;
		}
	else if (c==2){
	two = 500;
	}
	else if (c==3){
	three=700;
	}
//cout << "Money= "<<(money-one)<<"\n";
}



//To write GetResults Class

void
GetResults ()

{
	if(money <=0)
		//if player has no more money he lose
	{
		cout <<"Game Over .\n\n";
		Start ();
	}
	cout << "Type a number \n\n";
	cout <<"\n";
	Moneyz();
	cin >> a; //read user's number

if ((a>maxrand) || (a<0)) // if the user isn't correct,restart
{
	cout <<"Error: Number not between 0 and \n" <<maxrand;
	GetResults();
}

if(a==b)
{
	cout <<"Congratz You Win the LOTTERY!! !\n\n";// User find the secret number
	Start ();
}
else if (a != b)
{
	cout<< "Sorry Back luck , Try again";
	cout <<"Your current money is:"<<money<<"\n\n";
	GetResults();
	
}

}
int
 main()
{
	//Rules and regulations regarding the game.

cout <<"^^/>^^^LOTTERY GAME^^^/>^^^/>\n";
cout <<"\n";
cout <<" Welcome to the Lottery Play.\n";
cout <<"\n";
cout <<" Rules and Regulations~.\n";
cout <<"\n";
cout <<" The Rules are Simple .\n";
cout <<" 1. There are three levels to play in ,Easy,Medium and Hard.\n";
cout <<" 2. Choose either one of the levels and input a number.\n";
cout <<" 3. Different amount of money is needed to play different levels .\n";
cout <<" 4. You need to input your digits of ur own choice.\n";
cout <<" 5. If your money finishes and its game over for you.\n";
cout <<" 6. If one really striked the lottery one cab take home the money excluding Gov.Tax.\n";
cout <<"\n";

Start();
return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: lottery program

#2 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: lottery program

Posted 25 January 2008 - 04:39 AM

In this bit here, you need to deduct the number.
{
	cout <<"Congratz You Win the LOTTERY!! !\n\n";// User find the secret number
	Start ();
}

You can deduct the amount easily. For instance, if we have a variable called 'i' and it is 5000, we can make it 3000 like this:
i -= 2000;


OR
i = i - 2000;


Was This Post Helpful? 0
  • +
  • -

#3 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: lottery program

Posted 25 January 2008 - 05:52 AM

Attached Image

see i keep on getting 1500 after input the number ..i wan it deducted by 200 or 500 by different levels.Can anyone correct or giv me tips? i'm not that clear plz n tq

View Postshadachi, on 25 Jan, 2008 - 05:48 AM, said:

Attachment attachment

see i keep on getting 1500 after input the number ..i wan it deducted by 200 or 500 by different levels.

This post has been edited by shadachi: 25 January 2008 - 05:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: lottery program

Posted 25 January 2008 - 06:34 AM

c==1


you are doing a comparison using the variable c as if it were an integer, yet you have clearly defined the data type as a character. either change the data type of the variable c to int, or do the comparion on a character, not a number, like if(c=='1').
Was This Post Helpful? 0
  • +
  • -

#5 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: lottery program

Posted 25 January 2008 - 06:50 AM

i've change it to this ..according to what u've said but i get the same thing . it only changes from 1500 to 1451..

switch (c)
{
case '1' :	 maxrand =20; //The random number between
			  one;	// 0 and maxrand
  break;
case '2' : maxrand =15;
				two;
  break;
case '3' : maxrand =5;
			three;
	break;
default : exit(0);
	break;
}

void Moneyz()
{

	if (c=='1'){
	one = 200;
		}
	else if (c=='2'){
	two = 500;
	}
	else if (c=='3'){
	three=700;
	}
cout << "Money= "<<(money-c)<<"\n";
}






Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: lottery program

Posted 25 January 2008 - 07:02 AM

what are 'one', 'two' and 'three' for in your code? they don't seem to be doing anything useful - could you explain what you intend them to do?.
Was This Post Helpful? 0
  • +
  • -

#7 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: lottery program

Posted 25 January 2008 - 07:04 AM

View PostBench, on 25 Jan, 2008 - 07:02 AM, said:

what are 'one', 'two' and 'three' for in your code? they don't seem to be doing anything useful - could you explain what you intend them to do?.


they represent levels .one for level one .two for level two and so on.. different levels cost different amount of money to play per turn ..eg. lvl one cost 200 ..lvl two cost 500 ...
Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: lottery program

Posted 25 January 2008 - 07:17 AM

View Postshadachi, on 25 Jan, 2008 - 02:04 PM, said:

View PostBench, on 25 Jan, 2008 - 07:02 AM, said:

what are 'one', 'two' and 'three' for in your code? they don't seem to be doing anything useful - could you explain what you intend them to do?.


they represent levels .one for level one .two for level two and so on.. different levels cost different amount of money to play per turn ..eg. lvl one cost 200 ..lvl two cost 500 ...

In which case, you might be better off with just a single cost variable, whose value changes to 200/500/700/etc depending on the level you choose. Your level appears to be stored in 'c' already. Right now, using 3 separate variables means you need 3 separate blocks of code to deal with each level (Perhaps this is what you intended, but it seems long-winded if the only thing which changes is the cost)

your switch/case statement would become
switch (c)
{
case '1':
	cost = 200;
	break;
case '2':
	cost = 500;
	break;
case '3':
	cost = 700;
} 

cout << "Money= "<<(money-cost)<<"\n"; 


in general, you need different variables when you expect their values to change independently of each another. If you know in advance that the values are final, then you're better off keeping them as constants.


If you wish, you could change that switch statement further, since "magic" numbers like 200, 500, and 700 seem a little bit random when you read the code.

const int one = 300;
const int two = 500;
const int three = 700;

switch (c)
{
case '1':
	cost = one;
	break;
case '2':
	cost = two;
	break;
case '3':
	cost = three;
} 

cout << "Money= "<<(money-cost)<<"\n";
This does exactly the same thing as the first snippet, you're still only bothered with your cost variable. but you've got 'one', 'two' and 'three' instead of the "magic" numbers.

This post has been edited by Bench: 25 January 2008 - 07:21 AM

Was This Post Helpful? 0
  • +
  • -

#9 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: lottery program

Posted 25 January 2008 - 07:29 AM

so what should i declare the cost as?? string int?

is it correct if i write like this? cause i wan the random numbers to generate the winning numbers

switch (c)
{
case '1' : 	maxrand =20; //The random number between
			   cost = one;
	// 0 and maxrand
  break;
case '2' : maxrand =15;
	cost = two;
  break;
case '3' : maxrand =5;
	cost = three;
	break;
default : exit(0);
	break;
}


This post has been edited by shadachi: 25 January 2008 - 07:32 AM

Was This Post Helpful? 0
  • +
  • -

#10 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: lottery program

Posted 25 January 2008 - 08:49 AM

anyone can help plz??
Was This Post Helpful? 0
  • +
  • -

#11 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: lottery program

Posted 25 January 2008 - 09:21 AM

The compiler should complain if you try copying a number straight in to a string. the data you want to hold is a number, so an int would make alot of sense to me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1