I am taking a introduction to C++ programming class in college and I am having some issues with my assignment. If anyone can help It would mean a world of diffrence to me. Thanks in advance.

Okay the assigenment is to write a program that simulates the lottery, the program should have an array of five integers named lottery and should generate a random number in the range of 0 through 9 for each element in the array. The user should enter five digits which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that match.

The program should then display the random numbers stored in the lottery array and the number of digits matching digits. if numbers of digits match display a prize pakagem of each coresspoding to 2, 3,4 and all five digits that are matching.

I have gotten the code to compile and it runs though the prompts asking the user to enter there numbers, but when I run the progam I keep getting a "segmentation fault" . How can I fix this?

below is my code

---------------------------------------------------------------------------------------------------------

// Description: This program allows the user to enter five numbers, like a lottery.
// The program chooses five numbers at random and compares them to see if the user won.
// Inputs: numbers
// Outputs: random numbers, prize message, user numbers
// Programmer Name: David Pfaff
// Username: dpfaff
// Date: April 9, 2008

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

const int MAXCHARS = 5;

// prints a number of new lines
// inputs: num, the number of new lines desired
// outputs: num newlines on the screen
// usage: printNewLines(4);
void printNewLines(int num);

// displays the programmer name and description
// outputs: the programmer name and description of program
// usage: displayInfo();
void displayInfo();

// allows the user to select five numbers to play the lottery.
// inputs: five numbers
// outputs: none
// usage: getTicket (int user[])
void getTicket (int user[]);

// the computer selects five random numbers for the lottery
// inputs: none
// outputs: five random numbers
// usage: getLottery(int lottery[])
void getLottery(int lottery[]);

// compares the user input and the computer's output
// inputs: none
// outputs: number of pairs
// usage: int compare(user, lottery)
int compare(int user[], int lottery[]);

// tells the user which prize package he/she has won if there were any matching pairs
// inputs: none
// outputs: prize package
// usage: displayPrize(user)
void displayPrize(int correct);

// receives an answer from user as to whether he/she would like to continue playing
// inputs: answer
// ouputs: repeat of program
// usage: toContinue(answer)
void toContinue(char& answer);

int main()
{
    int user[MAXCHARS];
    int lottery[MAXCHARS];
    int correct, compareNumbers;
    char answer;
   
    do
    {
        printNewLines(2);
        displayInfo();
        printNewLines(2);
   
        getTicket(user);
        printNewLines(2);
   
        getLottery(lottery);
        printNewLines(2);
 
        correct = compareNumbers[user, lottery];
        printNewLines(2);
       
        displayPrize(correct);
        printNewLines(2);
       
        toContinue(answer);
        printNewLines(2);
    }while(answer == 'y' || answer == 'Y');
}  
   

// prints a number of new lines
// inputs: num, the number of new lines desired
// outputs: num newlines on the screen
// usage: printNewLines(4);
void printNewLines(int num)
{
    for (int i = 0; i < num; i++)
        cout << endl;
} 

// displays the programmer name and description
// outputs: the programmer name and description of program
// usage: displayInfo();
void displayInfo()
{
    cout << "David Pfaff";
    printNewLines(2);
    cout << "Description: This program allows the user to enter five numbers, like a lottery.";
    cout << "The program chooses 5 numbers at random and compares them to see if the user won.";
}

// allows the user to select five numbers to play the lottery.
// inputs: five numbers
// outputs: none
// usage: getTicket (int user[])
void getTicket (int user[])
{
    cout << "Enter a single digit number (0 through 9).";
    printNewLines(2);

    for(int i = 0; i < MAXCHARS; i++)
    {
        cout << "Enter number --> ";
        cin >> user[i];
   
        if(user[i] < 0 || user [i] > 9)
        {
            cout << "Please enter another number, your entry was invalid.";
            printNewLines(2);
            cout << "Enter a number -> ";
            cin >> user[i];
            printNewLines(2);
        }   
    }
 }

// the computer selects five random numbers for the lottery
// inputs: none
// outputs: five random numbers
// usage: getLottery(int lottery[])
void getLottery(int lottery[])
{  
    for (int num = 0; num < MAXCHARS; num++)
        lottery[num] = rand() % 9;
}   

// compares the user input and the computer's output
// inputs: none
// outputs: number of pairs
// usage: int compare(user, lottery)
int compare (int user[], int lottery[])
{
    int numberCorrect = 0;
    int index = 0;
    while (index < MAXCHARS)
    {
        if(lottery[index] == user[index])
            numberCorrect++;
    }
    return numberCorrect;
}   

// displays the random numbers and the user's numbers
// inputs: none
// outputs: random numbers, user numbers
// usage: displayNumbers(lottery, user)
void displayNumbers(int lottery[], int user[])
{
    cout << "Lottery numbers are -> ";
    for(int count = 0; count < MAXCHARS; count++)
      cout << lottery[count];
     
    cout << "User numbers are -> ";
    for(int i = 0; i < MAXCHARS; i++)
      cout << user[i];
} 

// tells the user which prize package he/she has won if there were any matching pairs
// inputs: none
// outputs: prize package
// usage: displayPrize(user)
void displayPrize(int correct)
{
    switch(correct)
    {
        case 0: cout << "You did not match any numbers, please play again." << endl;
            break;
        case 1: cout << "You only matched one number, that is not enough to win." << endl;
            break;
        case 2: cout << "You matched 2 numbers, you win $100." << endl;
            break;
        case 3: cout << "You matched 3 numbers, you win $1000." << endl;
            break;
        case 4: cout << "You matched 4 numbers, you win $100,000." << endl;
            break;
        case 5: cout << "You matched all the numbers, you win $2,000,000." << endl;
            break;
    }
}
// receives an answer from user as to whether he/she would like to continue playing
// inputs: answer
// ouputs: repeat of program
// usage: toContinue(answer)
void toContinue(char& answer)
{
    cout << "Would you like to play again? If so," << endl;
    cout << "please enter a 'y' for yes and an 'n' for no." << endl;
    cout << "Press enter after answering.";
    printNewLines(2);
    cin >> answer;
}

>>correct = compareNumbers[user, lottery];

First, compareNumbers is uninitialized, which is why you get the core dump.

Second, what exactly is that line doing? compareNumbers is a simple integer, so how can you possibly use it like that ???? user and lottery are both int arrays. That doesn't make sense, but the compiler doesn't complain.

I've never seen anything like that before -- what is it doing?

I corrected

int correct, compareNumbers = 0;

but after it gose through once and asks I want to play agin, Its goes iinto an infinite loop.

why is it doing this?

Hey It works!!! It's so nice when a computer does what you tell it to.

Thanks for your help.

Hey It works!!! It's so nice when a computer does what you tell it to.

Thanks for your help.

The computer was doing exactly what you told it to do. Your problem was that you told it to do the wrong thing :)

This article has been dead for over six months. Start a new discussion instead.