Program not running

This is my current program. I am trying to make a lottery ticket program but I cannot seem to be able to run it.

#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

//Global variables
vector <int> luckyNum (12);
vector <int> yourNum(0);

//Search for the matching numbers
bool match()
{
for(int i = 11; 0 < luckyNum.size(); i--)
for(int j = 0; yourNum.size() > j; j++)
luckyNum[i];
yourNum[j];
if(luckyNum[i] == yourNum[j])
{
cout << "Your matching number is: " << luckyNum[i] << endl;
return true;
}
else
return false;
}

//Function to enter your numbers
void chooseNum()
{
int num;

cout << "Please enter 12 of your 5 digit lucky number!" << endl;

for(int i = 0; i < 12; i++)
{
cout << "You have " << luckyNum.size() - yourNum.size()
<< " numbers remaining." << endl;
cin >> num;
if(num > 10000 && num < 99999)
yourNum.push_back(num);
else
cout << "Invalid number. Please try again." << endl;
}
}

//Randomly selects the winning numbers
void winningNum()
{

int numbers, i;
for(i = 0; i < 12; i++)
{
numbers = (rand() % 300)*(rand() % 300) + 10000;
if(numbers > 10000 && numbers < 99999)
{ luckyNum[i] = numbers; }
}
}

int main()
{
void winningNum();
void chooseNum();
bool match();

if (match() == true)
cout << "You are a winner!" << endl;
else
cout << "Sorry, today is not your lucky day!" << endl;

return 0;
}
You for loops in the bool match() do not have any brackets {}.
it says
"warning: comparison between signed and unsigned integer expressions [-Wsign-compare]"
for
"for(int j = 0; yourNum.size() > j; j++)" in the bool match() function
I got your program to run smoothly by adding the brackets to your match function. Refer to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool match()
{
for(int i = 11; 0 < luckyNum.size(); i--){
for(int j = 0; yourNum.size() > j; j++){
luckyNum[i];
yourNum[j];
if(luckyNum[i] == yourNum[j])
{
cout << "Your matching number is: " << luckyNum[i] << endl;
return true;
}
}
}

return false;
}


Additionally, you re-declared your functions in main instead of calling them as you had intended. Change the functions in main from

1
2
3
void winningNum();
void chooseNum();
bool match()


to

1
2
3
winningNum();
chooseNum();
match();
okay it works now but when i run it and entered my 12, 5 digit numbers, the program crashes and ends
The problem is with your match() function. The following line:

for(int i = 11; 0 < luckyNum.size(); i--)

needs to be changed to

for(int i = 12; 0 > luckyNum.size(); i--)

The initial value of i needs to be 12 to meet the size of luckyNum and the second n third statements:

0 > luckyNum.size(); i--

state that while 0 is greater than 12, continue to subtract 1 from i for each loop iteration. So the loop will continue to iterate until i = 0 and the for loop is therefore cancelled after it has been run 12 times.
Topic archived. No new replies allowed.