2016-11-04 24 views
-4

我需要我的程序幫助,當用戶輸入一個不是彩票贏家的號碼時,它仍然說贏家任何建議如何解決這個問題,如果你可以給我更好的在我的線性搜索功能的建議,這將是驚人的,這裏是我的代碼:我的程序沒有使用我的代碼的某些部分

#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; 

}

我知道有可能是一些標點符號錯誤,但可能是最容易thingt修復

+0

請編輯您的問題以提供[mcve]。 –

+0

解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –

+0

你爲什麼比較playerNum和lottoTix [LUCKY_NUMS]? (LUCKY_NUMS是一個常量整數) –

回答

1

的你的代碼的問題在於你從函數中獲得的值on應該是一個布爾值。 winningNum是一個整數。另外,你可以使用諸如「如果找到一個元素」之類的東西,然後做一些事情。您也可以使用for循環來進行線性搜索。

相關問題