2016-04-13 27 views
-1

Ich schreibe einen Code, um ein einfaches textbasiertes Blackjack-Spiel in C++ zu erstellen. Ich fange gerade an, aber ich stieß sofort auf ein Problem. Hier ist der Code, den ich bis jetzt habe, beachten Sie, dass es NICHT beabsichtigt ist, vollständig zu sein oder sogar noch zu laufen. Ja, ich weiß, es ist extrem chaotisch und ineffizient, ich bin in meiner ersten C++ - Klasse aller Zeiten.Kennung nicht gefunden Fehler?

Mein Problem ist, dass ich "Bezeichner nicht gefunden" bekomme, wenn mein Code rekursive Funktionen aufruft (DealingHandler und EndProgram). Ja, ich weiß, eine Funktion speziell zum Beenden des Programms ist in diesem Programm völlig nutzlos, aber ich weiß nicht, wie ich das Programm plötzlich ohne etwas in der Art beenden soll.

#include <iostream> 
#include <string> 
#include <cstdlib> 
using namespace std; const int maxScore = 21; //immutable upper limit 
               //string nameInput; - Idea for later. 
int HandVal, DealerHandVal, Aceone, val, aceVal; 
char HitStay; 
string DealerHand, currenthand; //Strings to output the cards (4 5 for example) 
int numAces, numTwo, numThree, numFour, numFive, numSix, numSeven, numEight, numNine, numTen, numJack, numQueen, numKing; //Inefficient way of making sure it doesn't give five twos out, however unlikely that may be. 
int main() { 
    cout << "Welcome to our C++ BlackJack Program." << endl; 
    cout << "Please press enter to begin the game." << endl; 
    system("PAUSE");//Bad. Still easiest for a beginning C++ class. 
    currenthand = ""; 
    numAces = 4; 
    numTwo = 4; 
    numThree = 4; 
    numFour = 4; 
    numFive = 4; 
    numSix = 4; 
    numSeven = 4; 
    numEight = 4; 
    numNine = 4; 
    numTen = 4; 
    numJack = 4; 
    numQueen = 4; 
    numKing = 4; 
    int i = 2; 
    { 
     DealingHandler(i); 
     cout << DealerHand; 
     cout << "Press H to hit or S to stay." << endl; 
     cin >> HitStay; 
     switch (HitStay){ 
        case 'H': 
         DealingHandler(1); 
         break; 
        case 'h': 
         DealingHandler(1); 
         break; 
        case 's': 
         EndProgram(); 
         break; 
        case 'S': 
         EndProgram(); 
         break; 
        default: 
         cout << "Invalid entry."; 
         break; 
     } 
    } 
    system("PAUSE"); 
} 
int DealingHandler(int HowManyDealed) { 
    for (int x = 0; x <= HowManyDealed; x++) { 
     val = rand() % 13; 
     switch (val) { 
     case '1': 
      cout << "A"; 
      cout << "Press One to set this Ace equal to one. Press Two to set it equal to eleven. This cannot be changed, so choose wisely!" << endl; 
      cin >> aceVal; 
      if (aceVal = 1) { 
       Aceone = 1; 
       currenthand += " 1"; 
       cout << currenthand << endl; 
       HandVal += 1; 
      } 
      if (aceVal = 2) { 
       Aceone = 11; 
       currenthand += " 11"; 
       cout << currenthand << endl; 
       HandVal += 11; 
      } 
      numAces = numAces - 1; 
     case '2': 
      currenthand += " 2"; 
      cout << currenthand << endl; 
      HandVal += 3; 
      numTwo -= 1; 
      break; 
     case '3': 
      currenthand += " 3"; 
      cout << currenthand << endl; 
      HandVal += 3; 
      numThree -= 1; 
      break; 
     case '4': 
      currenthand += " 4"; 
      cout << currenthand << endl; 
      HandVal += 4; 
      numFour -= 1; 
      break; 
     case '5': 
      currenthand += " 5"; 
      cout << currenthand << endl; 
      HandVal += 5; 
      numFive -= 1; 
      break; 
     case '6': 
      currenthand += " 6"; 
      cout << currenthand << endl; 
      HandVal += 6; 
      numSix -= 1; 
      break; 
     case '7': 
      currenthand += " 7"; 
      cout << currenthand << endl; 
      HandVal += 7; 
      numSeven -= 1; 
      break; 
     case '8': 
      currenthand += " 8"; 
      cout << currenthand << endl; 
      HandVal += 8; 
      numEight -= 1; 
      break; 
     case '9': 
      currenthand += " 9"; 
      cout << currenthand << endl; 
      HandVal += 9; 
      numNine -= 1; 
      break; 
     case '10': 
      currenthand += " 10"; 
      cout << currenthand << endl; 
      HandVal += 10; 
      numTen -= 1; 
      break; 
     case '11': 
      currenthand += " J"; 
      cout << currenthand << endl; 
      HandVal += 10; 
      numJack -= 1; 
      break; 
     case '12': 
      currenthand += " Q"; 
      cout << currenthand << endl; 
      HandVal += 10; 
      numQueen -= 1; 
      break; 
     case '13': 
      currenthand += " K"; 
      cout << currenthand << endl; 
      HandVal += 10; 
      numKing -= 1; 
      break; 
     } 
     if (HandVal < 21) { 
      cout << "Busted!" << endl; 
      EndProgram(); 
     } 

    } 
    return HandVal; 
} 
void EndProgram() 
     { 
      cout << "I hope you had fun with my game!"; 
      system("PAUSE"); 
     } 

Vielen Dank im Voraus für Ihre Hilfe.

+1

Sie benötigen einen Prototyp für 'int DealingHandler hinzufügen (int) 'und' void EndProgram() 'über' int main() ' – drescherjm

+1

Wenn Sie' std :: toupper' oder 'std :: tolower' vor der Anweisung 'switch' verwenden, können Sie die Anzahl der Fälle um die Hälfte reduzieren . –

Antwort

1

This ist ein großartiges Beispiel für Deklarationen, Prototypen und Definitionen, die Sie kennen sollten.

Funktionen Prototypen sind eine Möglichkeit, die Funktionsstruktur ohne seinen Körper zu erklären, zum Beispiel:

int my_function(int arg1, int arg2); 

In der Tat, Sie in Funktionsprototypen nicht einmal die Argumente Namen ihrer Art setzen müssen, nur:

int my_function(int, int); 

Da der Übersetzungsvorgang von oben geht nach unten, wenn Sie diesen Code

// function a definition 
int function_a() 
{ 
    int some_variable = function_b(); 
} 

// function b definition 
int function_b() 
{ 
    return 20; 
} 

der Compiler nichts über function_b() wissen, wenn Sie sie in functiona() aufrufen, so müssen Sie entweder Ihren Code wie diese Prototypen

// function b definition 
int function_b() 
{ 
    return 20; 
} 

// function a definition 
int function_a() 
{ 
    // now the compiler knows how function_b() works, or, at least, what arguments she need 
    int some_variable = function_b(); 
} 

oder Verwendung neu ordnen Sie den Code Organisation zu erhalten, wie diese

// function b prototype 
int function_b(); 

// function a definition 
int function_a() 
{ 
    int some_variable = function_b(); 
} 

// function b definition 
int function_b() 
{ 
    return 20; 
} 

so, jetzt, der Compiler weiß, dass ein „function_b“ gibt es das, und wissen, was ihre Parameter und Rückgabewert Typ sind vorhanden (in diesem Fall gibt es keine Parameter und der Rückgabetyp ist int)

Für die Bestellung werden Sie normalerweise y Setzen Sie den Funktionsprototyp in die Headerdatei (.h) und die Definition in die Codedatei (.c/cpp)

In Ihrem Beispiel rufen Sie innerhalb der Hauptfunktion die Funktionen DealingHandler und EndProgram auf, aber sie wird später definiert und deshalb erhalten Sie einen Fehler. So, Prototypen unter Verwendung von Funktionen können Sie es leicht beheben,

/* this can be 
int DealingHandler(int); 
because function prototypes doesn't need argument's name, just their types */ 
int DealingHandler(int HowManyDealed); 
void EndProgram(); 

auf die Funktion main() setzen, oder besser unterhalb dieser Linie

using namespace std; 
+0

Veröffentlichen Sie keine Link-Only-Antworten. Kopieren oder fassen Sie die relevanten Informationen zusammen und verwenden Sie den Link, um zusätzliche Unterstützung bereitzustellen. SO-Antworten sollten für sich selbst stehen, auch wenn die Remote-Site ausfällt. – Barmar

+0

Danke, ich habe es versucht und ich fühle mich so blöd im Moment, alles was ich brauchte, war das Verschieben der Funktion vor dem Haupt ... TYVM für deine Hilfe. – BMK600

+0

Wie auch immer, das Verschieben einer Funktion vor einer anderen ist nicht der richtige Weg, um die Dinge zu tun, Sie sollten Funktionsprototypen verwenden. Sie würden es später zu schätzen wissen, wenn Code-Organisation ein Muss wird. – Rezniaq