Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 9th, 2007, 12:08 AM   #1
C.B.
Newbie
 
Join Date: Apr 2007
Posts: 9
Rep Power: 0 C.B. is on a distinguished road
Lottery Program

How could I create a program to Pick lets say a Number from as many other numbers I enter, at random? And another program to generate random numbers?

Also, I know how to program a little in C++ and I have a compiler and all. This program doesn't sound to complex in my opinion. Any help to make would be great.

Last edited by C.B.; Jul 9th, 2007 at 12:10 AM. Reason: To add...
C.B. is offline   Reply With Quote
Old Jul 9th, 2007, 12:14 AM   #2
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 376
Rep Power: 0 King is an unknown quantity at this point
Well start by learning how to generate random number. Google the c++ rand function. You will get more help if you make an effort first. At the very least Google random number in c++ and then try and throw something together. If that that point you can't get something working, post what you have so far and we would all be glad to help you out.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Jul 9th, 2007, 12:15 AM   #3
C.B.
Newbie
 
Join Date: Apr 2007
Posts: 9
Rep Power: 0 C.B. is on a distinguished road
This is plenty help, I'll do that right now and see what I can come up with.


Edit. I noticed your from Ontario have there been any Sub 20 games there? For the Sub 20 World Cup.

Last edited by C.B.; Jul 9th, 2007 at 12:16 AM. Reason: Sub 20
C.B. is offline   Reply With Quote
Old Jul 9th, 2007, 12:21 AM   #4
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 376
Rep Power: 0 King is an unknown quantity at this point
Quote:
Originally Posted by C.B. View Post
Edit. I noticed your from Ontario have there been any Sub 20 games there? For the Sub 20 World Cup.
No clue, didn't even know what the Sub 20 games were until I just googled it lol. Not a huge fan of soccer or sports in general. I like MMA (mixed martial arts).
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Jul 9th, 2007, 12:26 AM   #5
C.B.
Newbie
 
Join Date: Apr 2007
Posts: 9
Rep Power: 0 C.B. is on a distinguished road
Quote:
Originally Posted by King View Post
No clue, didn't even know what the Sub 20 games were until I just googled it lol. Not a huge fan of soccer or sports in general. I like MMA (mixed martial arts).
Ah, well if you have a chance you should catch a game just for the hell of it.

Anyways, I've read so far and I like what I'm reading. I can create a number within a disired range and if im correct could I make it to where it would only be say 3 digit numbers?
Then Use the rand function again to choose a number from one of those? Like maybe in a seperate program?

The reason Im doing this is because I want to start a little lottery with my WoW guild. Say perhaps everyone that wants a ticket(number) Could send a Mail(sorry if youve never played WoW and dont understand) with the money for the ticket. Then as I receieve the mails, Ill generate a number for them and write it down so no one gets it. Then when its time for the drawing enter said numbers and have the prog pick one set randomly.
C.B. is offline   Reply With Quote
Old Jul 9th, 2007, 12:49 AM   #6
MiKuS
Hobbyist Programmer
 
Join Date: Jun 2007
Posts: 129
Rep Power: 2 MiKuS is on a distinguished road
the way i see this program working is:
* user puts in 5 numbers (5 for arguements sake)
* validate the input to make sure there are only 5 and that they're numbers
* store the result in an array
* generate 5 random numbers and store them in an array
* compare the two arrays to see if they're the same
* if they're the same display congrats or bad luck

this could be any ammount of numbers, including one - except with one you wouldn't use an array (or would you?)

if this is wrong please correct me (i'm new to coding)
MiKuS is offline   Reply With Quote
Old Jul 9th, 2007, 12:50 AM   #7
C.B.
Newbie
 
Join Date: Apr 2007
Posts: 9
Rep Power: 0 C.B. is on a distinguished road
//7-8-07
#include <stdio.h>
#include <stdlib.h>

int main( void )
main()
{
//Define Variables
int seed;
cin.get();
double r;
long int M;
double x;
int y;
int z;
int count;
//Code
seed = 10000;
srand(seed);
M = 1000;
for(count=1; count<=20; ++count)
{
r = (   (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
x = (r * M);
y = (int) x;
z = y + 1;
printf("random number %3d %5f %5f %5d %5d\n",count,r,x,y,z);
}
}

Ok this is what i got so far. Can you tell me some problems with it?
I put the int main( void ) so i could use the cin.get because the program closed as soon as i Tryed to use it. And now it wont decompile.
C.B. is offline   Reply With Quote
Old Jul 9th, 2007, 6:00 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You're writing C++. Don't use stdio.h and stdlib.h. Don't use printf. Use <iostream>, cin, and cout. cin.get () will pause the program at the point of the statement. Therefore, put if right before the return.

If you have additional functionality issues, you need to explain them. If your program won't compile, include the compiler errors and warnings. Make sure they are all turned on.

Read the "How to Post a Question" thread.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 9th, 2007, 7:45 AM   #9
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
What is with the random cin.get()?

also,
I would seed the generator with time, seeing as the seed is constant, the generator is not truly random.
srand( static_cast<int>( time(0) )

for casting, I would also use static_cast< type >( value ).

Also like Dawei said, use std::cout instead of printf, as this is a C++ program, not C.
Ben.Dougall is offline   Reply With Quote
Old Jul 9th, 2007, 10:33 AM   #10
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
I quickly made a solution for the first problem, it should compile from what i can tell, I'm at work, and don't have a C++ compiler handy. There is no error checking in this at all, and the quality of the code in general is lame, but I'm not gonna make a full out solution for a little thing like this.

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <time.h>
  5.  
  6. // Gets input from the user's keyboard, returns the input in the form of a double.
  7. double GetNumber( void )
  8. {
  9. std::cout << "Enter Number or 0 to exit: ";
  10. double ret;
  11.  
  12. std::cin >> ret;
  13. return ret;
  14. }
  15.  
  16. // Retrieves the numbers from the user and stores/returns a vector of doubles.
  17. std::vector<double> GetNumbers( void )
  18. {
  19. std::vector<double> nums;
  20. for (;;)
  21. {
  22. double num = GetNumber();
  23. if ( num = 0 )
  24. break;
  25. else
  26. nums.push_back( num );
  27. }
  28. return nums;
  29. }
  30.  
  31. int main( void )
  32. {
  33. std::vector<double> numbers = GetNumbers();
  34.  
  35. std::cout << "What number from the list do you want to see? ( 1 - " << numbers.size() << ")";
  36. int choice;
  37.  
  38. std::cin >> choice;
  39.  
  40. std::cout << numbers[choice - 1] << std::endl;
  41.  
  42. return 0;
  43. }
Ben.Dougall is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Language display in program Prm753 C++ 3 May 30th, 2006 5:45 PM
Creating a program to test a program sixstringartist C 8 Jan 21st, 2006 1:15 PM
move program console window back badbasser98 C++ 21 Oct 18th, 2005 2:02 PM
Nonsense Name generator program chillster13 C 14 Jun 17th, 2005 2:05 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC