我的程序没有使用我的代码的某些部分

时间:2016-11-04 17:42:50

标签: c++

我需要帮助我的程序,当用户输入一个不是彩票赢家的号码时,它仍然会告诉赢家任何建议如何解决这个问题如果你能给我更好的建议我的线性搜索功能会很棒,这是我的代码:

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

bool input(double);

const int LUCKY_NUMS =10;

// Function prototype that searches winning ticket number
bool ticketSearch(const int [], int, int);

int main ()
{
    //user continues playing lotto

    char again;

    const char QUIT = 'N';
    //determines if player's ticket is a winner

    int winningNum;
    //5 digit ticket number entered by player

    int playerNum;
    //holds winning ticket number

    int ticket;

    //Array holding the winning tickets for each week

    int lottoTix[LUCKY_NUMS] = {13579, 26791, 26792, 33445, 55555,
                                62483, 77777, 79422, 85647, 93121};

    //Player decides if they want to continue playing

            for (int week = 0; week < 10; week++)
            {
            //Winning lotto ticket for each week (10 weeks total)

                ticket = lottoTix[week];
                do
               {
                    cout << "Please enter your 5-digit ticket number for     week " << (week + 1) << ": " << endl;

            //Player's ticket number

            cin >> playerNum;
            }while(input(playerNum));



            //Calls linear search for winning lotto ticket

            winningNum = ticketSearch(lottoTix, LUCKY_NUMS, playerNum);

            //Error message if player's number is not the winning ticket
            //here is where my problems occurs it doesn't use this part of code at all
            if (playerNum != lottoTix[LUCKY_NUMS])
            {

                cout << "Sorry, you did not win the MEGAMILLIONS lottery. ";
                cout << "Thanks for playing! ";
                cout << "Play again? (Y/N)";
                    cin >> again;
            }

            //Player wins the lottery
            else if (playerNum == lottoTix[LUCKY_NUMS])
            {

                cout << "You have just won 598 MILLION DOLLARS!!! ";
                cout << "CONGRATULATIONS!!!";
                cout << "Play again? (Y/N)";
                cin >> again;
            }


            if ((again != 'Y') && (again != 'y'))
                {
                    //exit message
                    cout << "Press [Enter] to exit...\n\n";
                    //exits program
                    exit(0);
                }
        }
   return 0;
}

//linear search for winning ticket of the week
bool ticketSearch(const int ticketList[], int numTickets, int winningNum)
{
int index = 0;
int position = -1;
bool found = false;

while ((index < numTickets) && !found)
{
    if (ticketList[index] == winningNum)
    {
        found = true;
        position = index;
    }
    index ++;
    }
    return found;
}
// function does invalid input
bool input(double number)
{
if (number < 0 || number >=99999 ||  cin.fail())
{
    cin.clear();
    cin.ignore();
    cout <<"\nYou have entered an invalid answer\n" << endl;
    return true;
}
 else
     return false;

}

我知道可能存在一些标点符号错误,但这可能是最容易解决的问题

1 个答案:

答案 0 :(得分:1)

您的代码的问题是您从函数返回的值应该是布尔值。 winsNum是一个整数。此外,您可以使用诸如&#34之类的内容;如果找到元素&#34;,则执行某些操作。您也可以使用for循环进行线性搜索。