2016-07-05 2 views
0

내가 플레이하는 동안 돈 출력에 158, 1000 및 140이 있습니다. 원래 입력 한 금액은 100입니다. 총 금액과 사용자가 입력 한 금액이 제대로 실행되지 않았습니다. 때때로 합계와 입력 한 금액이 올바르게 표시되므로 유의해야합니다. 항상 그런 것은 아니지만 그게 문제입니다. 알아낼 수없는 몇 가지 논리 오류가 있습니다. 도움?C++ 논리 오류

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <string> 
#include <stdlib.h> 
using namespace std; 

void switchStatementsCalculations (int &slot1, int &slot2, int &slot3, string cherries, string 
            oranges, string plums, string bells, string melons, string bars); 

void calculateAmountEarnedByPlaying (double &money, int slot1, int slot2, int slot3, 
            double &total); 

int main() 
{ 
    int slot1; 
    int slot2; 
    int slot3; 

    double money=0; 
    double total=0; 
    double amountOfMoneyEnterd=0; 
    int count; 

    string cherries = "cherries"; 
    string oranges = "oranges"; 
    string plums = "plums"; 
    string bells = "bells"; 
    string melons = "melons"; 
    string bars = "bars"; 
    string doAgain; 

    cout << "We are going to be playing a slot machine game today." << endl; 
    srand(time(0)); 

    do 
    { 

     cout << "Please enter the amount of money you'd like to insert into the slot machine. We will pull the lever for you." << endl; 
     cin >> money; 
     cout << "You put in $" << money << endl; 




     slot1=rand()%6+1; 
     slot2=rand()%6+1; 
     slot3=rand()%6+1; 

     switchStatementsCalculations(slot1, slot2, slot3, cherries, oranges, plums, bells, melons, bars); 

     calculateAmountEarnedByPlaying(money, slot1, slot2, slot3, total); 

     amountOfMoneyEnterd=(amountOfMoneyEnterd+money); 





     cout << "Would you like to play again? Please type yes if so." << endl; 
     cin >> doAgain; 
     if(doAgain!= "yes") 
     { 
      cout << "The total amount of money you put in the slot machine is " << amountOfMoneyEnterd << endl; 
      cout << "The total amount of money you won is $" << total << endl; 
     } 

    } 
    while(doAgain=="yes"); 


    system("Pause"); 
    return 0; 
} 

void switchStatementsCalculations(int &slot1, int &slot2, int &slot3, string cherries, string 
            oranges, string plums, string bells, string melons, string bars) 
{ 
    switch (slot1) 
    { 
    case 1: 
     cout << "You got " << cherries << endl; 
    case 2: 
     cout << "You got " << oranges << endl; 
     break; 
    case 3: 
     cout << "You got " << plums << endl; 
     break; 
    case 4: 
     cout << "You got " << bells << endl; 
     break; 
    case 5: 
     cout << "You got " << melons << endl; 
     break; 
    case 6: 
     cout << "You got " << bars << endl; 
    } 

    switch (slot2) 
    { 
    case 1: 
     cout << "You got " << cherries << endl; 
     break; 
    case 2: 
     cout << "You got " << oranges << endl; 
     break; 
    case 3: 
     cout << "You got " << plums << endl; 
     break; 
    case 4: 
     cout << "You got " << bells << endl; 
     break; 
    case 5: 
     cout << "You got " << melons << endl; 
     break; 
    case 6: 
     cout << "You got " << bars << endl; 

    } 

    switch (slot3) 
    { 
    case 1: 
     cout << "You got " << cherries << endl; 
     break; 
    case 2: 
     cout << "You got " << oranges << endl; 
     break; 
    case 3: 
     cout << "You got " << plums << endl; 
     break; 
    case 4: 
     cout << "You got " << bells << endl; 
     break; 
    case 5: 
     cout << "You got " << melons << endl; 
     break; 
    case 6: 
     cout << "You got " << bars << endl; 

    } 
} 

void calculateAmountEarnedByPlaying(double &money, int slot1, int slot2, int slot3, double &total) 

{ 
    double won=0; 

    if(slot1==slot2 || slot1==slot3 || slot2==slot3) 
    { 
     cout << "Congratulations! You won." << endl; 
     won=(money * 2); 
     cout << "You won " << won << endl; 
    } 


    else if ((slot1==slot2 && slot1==slot3) || (slot2==slot1 && slot2==slot3) || (slot3==slot1 && slot3==slot2)) 
    { 
     cout << "Congratulations! You won." << endl; 
     won=(money*3); 
     cout << "You won " << won << endl; 
    } 
    else 
    { 
     cout << "You didn't earn any money." << endl; 
    } 

    total=(total+won); 
} 
+7

디버거를 사용하여 코드를 단계별로 실행 해보십시오. – NathanOliver

+0

나는 온라인 하나를 사용했으나 아무런 결과도 얻지 못했습니다. 수동으로 추가 할 때까지 내가 가지고 있지 않다고 생각했습니다. – Kikuo

+1

'srand (time (0));'이것은 루프 밖에서 루프 전에 한 번만 수행되어야하며 루프에서 반복적으로 수행되지 않아야합니다. 또한 이제는 동일한 코드를 6 번 반복 했으므로 배열을 배우는 좋은 시간입니다. – PaulMcKenzie

답변

0

하나의 실수는 당신의 calculateAmountEarnedByPlaying 기능이 있습니다 : 당신은이 변수를 초기화하지 않았다

double won;

, 따라서는 불확정 값이 포함되어 있습니다.

무언가를 얻은 경우에만 설정되며, 승리하지 못하면 설정되지 않습니다. 그런 다음 함수의 끝에서이 작업을 수행 : won 만약

total = (total + won);

가 초기화되어 있지 않은 경우, total은 불확정 값으로 설정 (또는 정의되지 않은 문제가 발생합니다)됩니다.

won 변수는 0에 초기화 할 필요가

double won = 0;

대부분의 컴파일러는 경고를이 같이 초기화되지 않은 변수에 액세스하는 경우. 이러한 문제를 감지하기 위해 경고를 받았는지 확인하십시오. slot11입니다

switch (slot1) 
{  
    case 1: 
     cout << "You got " << cherries << endl; // no break statement 
    case 2: 
     cout << "You got " << oranges << endl; 
    break; 

경우,이 경우 1과 경우 2

에 대한 출력의 두 세트를 인쇄합니다 :


다른 문제는 당신이 당신의 switch 문에서 break를 잊었이다

6 개의 개별 변수와 6 개의 별도 출력 행 대신 배열을 사용한 경우에는이 중 아무 것도 필요하지 않습니다.

std::string slot_items[] = {"cherries", "oranges", "plums", "bells", "melons", "bars"}; 
    //... 
    int slots[3]; 
    //... 
    for (int i = 0; i < 3; ++i) 
     slots[i] = rand()%6+1; 
    //... 
    // inside the function... 
    //... 
    for (int i = 0; i < 3; ++i) 
     cout << "You got " << slot_items[slots[i]] << endl; 

두 줄의 for 루프는 60 줄의 switch과 대소 문자를 대체합니다.

+0

글쎄, 내 문제가 해결 된 것 같아. 수정 된 코드를 수정하고 보여 드리겠습니다. 감사! 다른 의견이 있으면 의견을 남겨주세요. – Kikuo

+0

질문이 하나 더 있습니다. 처음으로 돈을 넣었을 때 네 과일을 처음 보여준 다음 두 번째 과일을 넣을 때 과일 두 개를 보여줍니다. 나는 이것이 내 코드의 실수라고 생각한다. 3 개의 슬롯 머신 만 있기 때문에 돈을 넣을 때마다 3 개의 과일을 보여야하지 않습니까? http://prntscr.com/bp9okf @PaulMcKenzie – Kikuo

+0

첫 번째 세트의 switch 문을 살펴보십시오. case 1에 대한 'break'문을 잊어 버렸습니다. – PaulMcKenzie