2016-11-04 3 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; 

}

私はいくつかの句読点のエラーはおそらくあります知っているが、それはおそらく

+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

修正する最も簡単なthingtですあなたのコードの問題は、あなたがfunctiから得た価値onは論理値でなければなりません。 winningNumは整数です。さらに、「要素が見つかった場合」など何かを使用してから、何かを行うことができます。 forループを使用して線形検索を行うこともできます。

関連する問題