Hello, I am new to C++ and I am enjoying it very much when I am getting it! I wrote a program to generate random numbers like a lottery but i have a few quirks in it, hopefully someone can assist me in correcting. I have extensively searched to find an answer and cannot. been working on this for about two days.
1. I don't understand why i need to add a star next to my for loop to insure the input number is under 10.
2. That same loop only works for the first input not any others.
3. My random number generator is producing 0,0,0,0,1493466176 every time. (or similar last number).
when I run this on my downloaded compiler it does what is stated above, if i run this on ideoneit.com it just runs all lines (ie. tells me i need to enter a number between 0 and 9, 5 times, but doesn't allow me to enter) and spits out 5 very large numbers.
Code:
- /* James Smith
- week5 assignment 1
- lottery generator*/
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- //function prototype
- double randomnum(int);
- int main ()
- {
- //declare variables
- int lottery[5], user[5];
- //introduce program
- cout << "Is today your lucky day?? Play the lottery and find out! \n" << endl;
- //get user numbers
- cout << "Please input your 5 lucky numbers between 0 and 9! \n";
- for (int i=0; i<5; i++)
- {
- cout <<"Lucky number"<< (i+1)<<": ";
- cin >> user[i];
- if (*user>10)// why is this making me add a star? also only works for the first input
- {
- cout <<"The number must be between 0 and 9, please reenter" << endl;
- cin >> user[i];
- }
- }
- //generate lottery numbers
- randomnum;
- //display lottery numbers
- for(int i=0; i<5; i++)
- {
- cout << lottery[i] <<" " << endl;
- }
- //declare if a winner or not
- for (int n=0; n<1; n++)
- {
- if (user[n] == lottery [n])
- cout << "You were right it was your lucky day!! Congratulation you matched all 5 numbers!!" << endl;
- else
- cout << "I'm sorry you did not match all 5 numbers please play again" << endl;
- }
- return 0;
- }
- double randomnum(int lottery)
- {
- int num[5];
- for (int i =0; i<5; i++)
- {
- //initialize random seed
- srand (time(0));
- // generate number
- return num[i] =rand()%9;//getting garbage numbers
- }
- }