2016-12-07 116 views
-4

當我嘗試運行我的代碼並始終處於最後一行時,我總是收到此錯誤。我正在嘗試製作基於文本的冒險遊戲。它似乎(當我運行這個),這是我唯一的錯誤。但是,如果有更多的人注意到,幫助將不勝感激。所以我的問題是:(1)這意味着什麼,(2)我該如何解決這個問題,以及(3)我如何在將來避免這種情況?不斷收到錯誤:在輸入結束時出現預期的不合格ID - C++

只是一個警告:此代碼有800多行,所以採取,因爲你會。

編輯:好吧,我已經找到了從前的問題,但是,現在我的類中的語句不會輸出。我的課程中的'cout'語句(例如fairy()類)只會跳過,房間標題和說明也會跳過。我已經重新安排了一小段代碼。

//Chandler Witthaus 
//Final Project- Text Adventure 
//Professor Caleb Fowler 
//17 December 2016 

#include <iostream> 
#include <string> 
#include <stdlib.h> 

int playerGold, 
findDirect, 
exitN, 
exitS, 
exitE, 
exitW; 

std::string start, 
direction, 
roomName, 
roomDiscription; 

using namespace std; 

class Monster { 
    public: 
     void fairy() { 
      std::string monEncounter = "A fairy is stuck in a discarded can. You let her out and she rewards you with 5 gold.\n"; 
      playerGold = playerGold + 5; 
     } 
     void unicorn() { 
      std::string monEncounter = "You see a unicorn frolicking in this room. The animal stares at you for a moment " 
      "before pointing its horn at a sack on the floor containing 20 gold.\n"; 
      playerGold = playerGold + 20; 
     } 
     void nymph() { 
      std::string monEncounter = "A beautiful nymph approached you, she offers you 100 gold for a brief fling. You are " 
      "reminded of your college days but hastily accept.\n"; 
      playerGold = playerGold + 100; 
     } 
     void imp() { 
      std::string monEncounter = "A little imp runs by, snatching 2 gold from your pouch. He disappears before you are able to catch him.\n"; 
      playerGold = playerGold - 2; 
     } 
     void wisp() { 
      std::string monEncounter = "A will-o'-the-wisp entices you to follow it. You bang into a wall, dropping your gold pouch. You pick it back " 
      "up but are unable to find 10 pieces.\n"; 
      playerGold = playerGold - 10; 
     } 
     void goblin() { 
      std::string monEncounter = "A greedy goblin ambushes you. You fight him off, but not before he takes 15 gold.\n"; 
      playerGold = playerGold - 15; 
     } 
     void centaur() { 
      std::string monEncounter = "You encounter a wild centaur, who is bucking wildly. You try to sneak around him but are struck, losing 20 gold."; 
      playerGold = playerGold - 20; 
     } 
     void wraith() { 
      std::string monEncounter = "Upon entering you are confronted by a wraith. It throws a curse on you, but thinking fast, you " 
      "eat 50 gold pieces. This makes the wraith very uncomfortable, it quickly removes the curse and leaves without " 
      "making eye contact.\n"; 
      playerGold = playerGold - 50; 
     } 
     void cyclops() { 
      std::string monEncounter = "You encounter a massive cyclops, he attempts to eat you. However, acting quickly, you make " 
      "a replica of yourself out of 80 gold pieces. The cyclops eats the replica and chokes to " 
      "death. You are not willing to recover the gold.\n"; 
      playerGold = playerGold - 80; 
     } 
     void hydra() { 
      std::string monEncounter = "You encounter a massive hydra. He will let you pass for 15 gold, you begrudgingly agree. You begin " 
      "to walk past, but the other heads of the hydra demand their pay. The toll ends up costing 135 gold. You quietly curse the hydra.\n"; 
      playerGold = playerGold - 135; 
     } 
     void bird() { 
      std::string monEncounter = "You see a bird, this must be the exit, but which way?\n"; 
     } 
}; 

class RandomSpawn { 
    public: 
     void spawnEnemy() { 
      int spawn = rand() % 10; 
      if (spawn <= 4) { 
       Monster objectImp; 
       objectImp.imp(); 
      } 
      if (spawn == 5) { 
       Monster objectWisp; 
       objectWisp.wisp(); 
      } 
      if (spawn == 6) { 
       Monster objectGoblin; 
       objectGoblin.goblin(); 
      } 
      if (spawn == 7) { 
       Monster objectCentaur; 
       objectCentaur.centaur(); 
      } 
      if (spawn == 8) { 
       Monster objectWraith; 
       objectWraith.wraith(); 
      } 
      if (spawn == 9) { 
       Monster objectCyclops; 
       objectCyclops.cyclops(); 
      } 
      if (spawn == 10) { 
       Monster objectHydra; 
       objectHydra.hydra(); 
      } 
     } 
}; 

class Treasure { 
    public: 
     void startChest() { 
      std::string chestType = "You find a beautiful chest. It is filled with 1000 gold. It makes you so excited that you don't even notice " 
      "as the entrance disappears. Looks like you will have to find your own way out.\n"; 
      playerGold = 1000; 
     } 
     void smallChest() { 
      std::string chestType = "You find a small chest containing 150 gold. You feel a strong urge to shower in it, but you hold yourself back.\n"; 
      playerGold = playerGold + 150; 
     } 
     void mediumChest() { 
      std::string chestType = "You find a chest with a pirate symbol on it. Ignoring it, you plunder 300 gold, feeling a thrill from the " 
      "steal. You think you may be a kleptomaniac."; 
      playerGold = playerGold + 300; 
     } 
     void largeChest() { 
      std::string chestType = "You find a large chest, no not that kind. You get 500 gold...dirty\n"; 
      playerGold = playerGold + 500; 
     } 
}; 

class Room { 
    public: 
     void room11() { 
      std::string roomName = "North-West corner"; 
      std::string roomDiscription = ""; 
      int roomId = 11; 
      Monster objectUnicorn; 
      objectUnicorn.unicorn(); 
      int exitN = 0; 
      int exitS = 12; 
      int exitE = 21; 
      int exitW = 0; 
     } 
     void room21() { 
      std::string roomName = "Storage room"; 
      std::string roomDiscription = ""; 
      int roomId = 21; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 0; 
      int exitS = 22; 
      int exitE = 31; 
      int exitW = 11; 
     } 
     void room31() { 
      std::string roomName = "Grey room"; 
      std::string roomDiscription = ""; 
      int roomId = 31; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 0; 
      int exitS = 32; 
      int exitE = 41; 
      int exitW = 21; 
     } 
     void room41() { 
      std::string roomName = "Clock room"; 
      std::string roomDiscription = ""; 
      int roomId = 41; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 0; 
      int exitS = 42; 
      int exitE = 51; 
      int exitW = 31; 
     } 
     void room51() { 
      std::string roomName = "North-East corner"; 
      std::string roomDiscription = ""; 
      int roomId = 51; 
      Monster objectNymph; 
      objectNymph.nymph(); 
      int exitN = 0; 
      int exitS = 52; 
      int exitE = 0; 
      int exitW = 41; 
     } 
     void room12() { 
      std::string roomName = "Chair room"; 
      std::string roomDiscription = ""; 
      int roomId = 12; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 11; 
      int exitS = 13; 
      int exitE = 22; 
      int exitW = 0; 
     } 
     void room22() { 
      std::string roomName = "Alchemy room"; 
      std::string roomDiscription = ""; 
      int roomId = 22; 
      Treasure objectLargeChest; 
      objectLargeChest.largeChest(); 
      int exitN = 21; 
      int exitS = 23; 
      int exitE = 32; 
      int exitW = 12; 
     } 
     void room32() { 
      std::string roomName = "Sun room"; 
      std::string roomDiscription = ""; 
      int roomId = 32; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 32; 
      int exitS = 33; 
      int exitE = 42; 
      int exitW = 22; 
     } 
     void room42() { 
      std::string roomName = "Meaning of the universe room"; 
      std::string roomDiscription = ""; 
      int roomId = 42; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 41; 
      int exitS = 43; 
      int exitE = 52; 
      int exitW = 32; 
     } 
     void room52() { 
      std::string roomName = "Hallway"; 
      std::string roomDiscription = ""; 
      int roomId = 52; 
      Monster objectBird; 
      objectBird.bird(); 
      int exitN = 51; 
      int exitS = 53; 
      int exitE = 62; 
      int exitW = 42; 
     } 
     void room13() { 
      std::string roomName = "Medical room"; 
      std::string roomDiscription = ""; 
      int roomId = 13; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 12; 
      int exitS = 13; 
      int exitE = 23; 
      int exitW = 0; 
     } 
     void room14() { 
      std::string roomName = "Guest bedroom"; 
      std::string roomDiscription = ""; 
      int roomId = 14; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 13; 
      int exitS = 15; 
      int exitE = 24; 
      int exitW = 0; 
     } 
     void room15() { 
      std::string roomName = "Guest kitchen"; 
      std::string roomDiscription = ""; 
      int roomId = 15; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 14; 
      int exitS = 16; 
      int exitE = 25; 
      int exitW = 0; 
     } 
     void room16() { 
      std::string roomName = "Storage room"; 
      std::string roomDiscription = ""; 
      int roomId = 16; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 15; 
      int exitS = 17; 
      int exitE = 26; 
      int exitW = 0; 
     } 
     void room17() { 
      std::string roomName = "Hallway"; 
      std::string roomDiscription = ""; 
      int roomId = 17; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 16; 
      int exitS = 18; 
      int exitE = 27; 
      int exitW = 0; 
     } 
     void room18() { 
      std::string roomName = "South-West Corner"; 
      std::string roomDiscription = ""; 
      int roomId = 18; 
      Treasure objectSmallChest; 
      objectSmallChest.smallChest(); 
      Monster objectWraith; 
      objectWraith.wraith(); 
      int exitN = 17; 
      int exitS = 0; 
      int exitE = 28; 
      int exitW = 0; 
     } 
     void room23() { 
      std::string roomName = "Library room"; 
      std::string roomDiscription = ""; 
      int roomId = 23; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 22; 
      int exitS = 24; 
      int exitE = 33; 
      int exitW = 13; 
     } 
     void room24() { 
      std::string roomName = "Washroom"; 
      std::string roomDiscription = ""; 
      int roomId = 24; 
      Treasure objectSmallChest; 
      objectSmallChest.smallChest(); 
      int exitN = 23; 
      int exitS = 25; 
      int exitE = 34; 
      int exitW = 14; 
     } 
     void room25() { 
      std::string roomName = "Workshop"; 
      std::string roomDiscription = ""; 
      int roomId = 25; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 24; 
      int exitS = 26; 
      int exitE = 35; 
      int exitW = 15; 
     } 
     void room26() { 
      std::string roomName = "Courtyard"; 
      std::string roomDiscription = ""; 
      int roomId = 26; 
      Monster objectNymph; 
      objectNymph.nymph(); 
      int exitN = 25; 
      int exitS = 27; 
      int exitE = 36; 
      int exitW = 16; 
     } 
     void room27() { 
      std::string roomName = "Garden"; 
      std::string roomDiscription = ""; 
      int roomId = 27; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 26; 
      int exitS = 28; 
      int exitE = 37; 
      int exitW = 17; 
     } 
     void room28() { 
      std::string roomName = "Chapel"; 
      std::string roomDiscription = ""; 
      int roomId = 28; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 27; 
      int exitS = 0; 
      int exitE = 38; 
      int exitW = 18; 
     } 
     void room33() { 
      std::string roomName = "Pantry"; 
      std::string roomDiscription = ""; 
      int roomId = 33; 
      Monster objectFairy; 
      objectFairy.fairy(); 
      int exitN = 32; 
      int exitS = 34; 
      int exitE = 43; 
      int exitW = 23; 
     } 
     void room34() { 
      std::string roomName = "Master chambers"; 
      std::string roomDiscription = ""; 
      int roomId = 34; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 33; 
      int exitS = 35; 
      int exitE = 44; 
      int exitW = 24; 
     } 
     void room35() { 
      std::string roomName = "Guard house"; 
      std::string roomDiscription = ""; 
      int roomId = 35; 
      Monster objectFairy; 
      objectFairy.fairy(); 
      int exitN = 34; 
      int exitS = 36; 
      int exitE = 45; 
      int exitW = 25; 
     } 
     void room36() { 
      std::string roomName = "Lavatory"; 
      std::string roomDiscription = ""; 
      int roomId = 36; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 35; 
      int exitS = 37; 
      int exitE = 46; 
      int exitW = 26; 
     } 
     void room37() { 
      std::string roomName = "Foyer"; 
      std::string roomDiscription = ""; 
      int roomId = 37; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 36; 
      int exitS = 38; 
      int exitE = 47; 
      int exitW = 27; 
     } 
     void room38() { 
      std::string roomName = "Gatehouse"; 
      std::string roomDiscription = ""; 
      int roomId = 38; 
      Treasure objectStarterChest; 
      objectStarterChest.startChest(); 
      int exitN = 37; 
      int exitS = 0; 
      int exitE = 48; 
      int exitW = 28; 
     } 
     void room43() { 
      std::string roomName = "Walk-in closet"; 
      std::string roomDiscription = ""; 
      int roomId = 43; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 42; 
      int exitS = 44; 
      int exitE = 53; 
      int exitW = 33; 
     } 
     void room44() { 
      std::string roomName = "Sitting room"; 
      std::string roomDiscription = ""; 
      int roomId = 44; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 43; 
      int exitS = 45; 
      int exitE = 54; 
      int exitW = 34; 
     } 
     void room45() { 
      std::string roomName = "Great Hall"; 
      std::string roomDiscription = ""; 
      int roomId = 45; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 44; 
      int exitS = 46; 
      int exitE = 55; 
      int exitW = 35; 
     } 
     void room46() { 
      std::string roomName = "Kitchen"; 
      std::string roomDiscription = ""; 
      int roomId = 46; 
      Treasure objectSmallChest; 
      objectSmallChest.smallChest(); 
      int exitN = 45; 
      int exitS = 47; 
      int exitE = 56; 
      int exitW = 36; 
     } 
     void room47() { 
      std::string roomName = "Buttery"; 
      std::string roomDiscription = ""; 
      int roomId = 47; 
      Monster objectUnicorn; 
      objectUnicorn.unicorn(); 
      int exitN = 46; 
      int exitS = 48; 
      int exitE = 57; 
      int exitW = 37; 
     } 
     void room48() { 
      std::string roomName = "Storage room"; 
      std::string roomDiscription = ""; 
      int roomId = 48; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 47; 
      int exitS = 0; 
      int exitE = 58; 
      int exitW = 38; 
     } 
     void room53() { 
      std::string roomName = "Hallway"; 
      std::string roomDiscription = ""; 
      int roomId = 53; 
      Treasure objectMediumChest; 
      objectMediumChest.mediumChest(); 
      int exitN = 52; 
      int exitS = 54; 
      int exitE = 0; 
      int exitW = 43; 
     } 
     void room54() { 
      std::string roomName = "Compost shed"; 
      std::string roomDiscription = ""; 
      int roomId = 54; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 53; 
      int exitS = 55; 
      int exitE = 0; 
      int exitW = 44; 
     } 
     void room55() { 
      std::string roomName = "Tool shed"; 
      std::string roomDiscription = ""; 
      int roomId = 55; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 54; 
      int exitS = 56; 
      int exitE = 0; 
      int exitW = 45; 
     } 
     void room56() { 
      std::string roomName = "Blacksmith"; 
      std::string roomDiscription = ""; 
      int roomId = 56; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 55; 
      int exitS = 57; 
      int exitE = 0; 
      int exitW = 46; 
     } 
     void room57() { 
      std::string roomName = "Guardroom"; 
      std::string roomDiscription = ""; 
      int roomId = 57; 
      Treasure objectMediumChest; 
      objectMediumChest.mediumChest(); 
      Monster objectWisp; 
      objectWisp.wisp(); 
      int exitN = 56; 
      int exitS = 58; 
      int exitE = 0; 
      int exitW = 47; 
     } 
     void room58() { 
      std::string roomName = "South-East corner"; 
      std::string roomDiscription = ""; 
      int roomId = 58; 
      RandomSpawn objectEnemy; 
      objectEnemy.spawnEnemy(); 
      int exitN = 57; 
      int exitS = 0; 
      int exitE = 0; 
      int exitW = 48; 
     } 
}; 

class Direction { 
    public: 
     void roomChanging() { 
      if (findDirect == 11) { 
       Room objectRoom11; 
       objectRoom11.room11(); 
      } 

      if (findDirect == 12){ 
       Room objectRoom12; 
       objectRoom12.room12(); 
      } 

      if (findDirect == 13){ 
       Room objectRoom13; 
       objectRoom13.room13(); 
      } 

      if (findDirect == 14){ 
       Room objectRoom14; 
       objectRoom14.room14(); 
      } 

      if (findDirect == 15){ 
       Room objectRoom15; 
       objectRoom15.room15(); 
      } 

      if (findDirect == 16){ 
       Room objectRoom16; 
       objectRoom16.room16(); 
      } 

      if (findDirect == 17){ 
       Room objectRoom17; 
       objectRoom17.room17(); 
      } 

      if (findDirect == 18){ 
       Room objectRoom18; 
       objectRoom18.room18(); 
      } 

      if (findDirect == 21){ 
       Room objectRoom21; 
       objectRoom21.room21(); 
      } 

      if (findDirect == 22){ 
       Room objectRoom22; 
       objectRoom22.room22(); 
      } 

      if (findDirect == 23){ 
       Room objectRoom23; 
       objectRoom23.room23(); 
      } 

      if (findDirect == 24){ 
       Room objectRoom24; 
       objectRoom24.room24(); 
      } 

      if (findDirect == 25){ 
       Room objectRoom25; 
       objectRoom25.room25(); 
      } 

      if (findDirect == 26){ 
       Room objectRoom26; 
       objectRoom26.room26(); 
      } 

      if (findDirect == 27){ 
       Room objectRoom27; 
       objectRoom27.room27(); 
      } 

      if (findDirect == 28){ 
       Room objectRoom28; 
       objectRoom28.room28(); 
      } 

      if (findDirect == 31){ 
       Room objectRoom31; 
       objectRoom31.room31(); 
      } 

      if (findDirect == 32){ 
       Room objectRoom32; 
       objectRoom32.room32(); 
      } 

      if (findDirect == 33){ 
       Room objectRoom33; 
       objectRoom33.room33(); 
      } 

      if (findDirect == 34){ 
       Room objectRoom34; 
       objectRoom34.room34(); 
      } 

      if (findDirect == 35){ 
       Room objectRoom35; 
       objectRoom35.room35(); 
      } 

      if (findDirect == 36){ 
       Room objectRoom36; 
       objectRoom36.room36(); 
      } 

      if (findDirect == 37){ 
       Room objectRoom37; 
       objectRoom37.room37(); 
      } 

      if (findDirect == 38){ 
       Room objectRoom38; 
       objectRoom38.room38(); 
      } 

      if (findDirect == 41){ 
       Room objectRoom41; 
       objectRoom41.room41(); 
      } 

      if (findDirect == 42){ 
       Room objectRoom42; 
       objectRoom42.room42(); 
      } 

      if (findDirect == 43){ 
       Room objectRoom43; 
       objectRoom43.room43(); 
      } 

      if (findDirect == 44){ 
       Room objectRoom44; 
       objectRoom44.room44(); 
      } 

      if (findDirect == 45){ 
       Room objectRoom45; 
       objectRoom45.room45(); 
      } 

      if (findDirect == 46){ 
       Room objectRoom46; 
       objectRoom46.room46(); 
      } 

      if (findDirect == 47){ 
       Room objectRoom47; 
       objectRoom47.room47(); 
      } 

      if (findDirect == 48){ 
       Room objectRoom48; 
       objectRoom48.room48(); 
      } 

      if (findDirect == 51){ 
       Room objectRoom51; 
       objectRoom51.room51(); 
      } 

      if (findDirect == 52){ 
       Room objectRoom52; 
       objectRoom52.room52(); 
      } 

      if (findDirect == 53){ 
       Room objectRoom53; 
       objectRoom53.room53(); 
      } 

      if (findDirect == 54){ 
       Room objectRoom54; 
       objectRoom54.room54(); 
      } 

      if (findDirect == 55){ 
       Room objectRoom55; 
       objectRoom55.room55(); 
      } 

      if (findDirect == 56){ 
       Room objectRoom56; 
       objectRoom56.room56(); 
      } 

      if (findDirect == 57){ 
       Room objectRoom57; 
       objectRoom57.room57(); 
      } 

      if (findDirect == 58){ 
       Room objectRoom58; 
       objectRoom58.room58(); 
      } 
     } 
}; 

int main() 
{ 
    cout << "Welcome\n\n"; 
    cout << "You are playing 'Deep Delver'.\n\n"; 
    cout << "The object of this game is to enter this old castle and emerge with as much gold as possible.\n\n"; 
    cout << "If you lose all your gold, you lose.\n\n"; 
    cout << "Here are the controls:\n Enter 'N', 'S', 'E', or 'W'. to go in that direction.\n"; 
    cout << "To see the description of the room your in press 'L'.\n\nGood luck!\n\n"; 
    cout << "Enter anything to start: "; 
    cin >> start; 

    Room objectStartingRoom; 
    objectStartingRoom.room38(); 

    do { 
     cout << "\n\nWhich way would you like to go?\n\n"; 
     stupid: 
     cout << "Cardnal direction: "; 
     cin >> direction; 
    cout << endl << endl; 
     if (direction == "W" || direction == "w") 
      findDirect = exitW; 
     else if (direction == "E" || direction == "e") 
      findDirect = exitE; 
     else if (direction == "N" || direction == "n") 
      findDirect = exitN; 
     else if (direction == "S" || direction == "s") 
      findDirect = exitS; 
     else if (findDirect == 0) 
     { 
      cout << "You bump into a wall...like an idiot. Go a different direction.\n\n"; 
      goto stupid; 
     } 
     else 
     { 
      cout << "That is not a direction...stupid.\n\n"; 
      goto stupid; 
     } 

     Direction objectDirection; 
    objectDirection.roomChanging(); 

    cout << roomName << endl << endl; 
    cout << roomDiscription << endl << endl; 
    } while (exitE != 62); 

    cout << "\n\nCongradulations!!!\n\n"; 
    cout << "You have found your way out with " << playerGold << " gold. Don't spend it all in one place!"; 
} 
+0

'而(exitE = 62!);'? – Iluvatar

+0

@Ivvatar這可能是一個錯誤,但它不是編譯器錯誤 – Erix

+0

'roomChanging()'函數末尾缺少括號 – Yousaf

回答

1

您錯過了大括號。您從不關閉roomChanging()功能。

+0

使用'clang-format'來不斷地爲你清理你的代碼,這種事情將是顯而易見的:) –

1

這段代碼很不尋常。你知道範圍嗎?

void foo() { 
    Widget w; // create a widget which exists inside function foo() 
} // w is now destroyed 

很多您的職能沒有做任何事情

void room11() { 
    std::string roomName = "North-West corner"; // gets destroyed at } 
    std::string roomDiscription = "";   // gets destroyed at } 
    int roomId = 11;       // gets destroyed at } 
    Monster objectUnicorn; // does something before being destroyed at } 
    objectUnicorn.unicorn(); // increases player's gold 
    int exitN = 0;   // gets destroyed at } 
    int exitS = 12;   // gets destroyed at } 
    int exitE = 21;   // gets destroyed at } 
    int exitW = 0;   // gets destroyed at } 
} // poof! Nearly everything is destroyed without doing anything 

更通常的方法是描述一個房間一個類的概念:

class Room { 
private: 
    int id_; 
    std::string name_; // there's a few naming conventions; one is a trailing 
    std::string desc_; // underscore for private class data members 
    // forget the monster and exits for the moment 

public: 
    Room(int id, const std::string &name, const std::string &desc) 
     : id_(id), name_(name), desc_(desc) {} 
    int id() {return id_;} 
    std::string name() {return name_;} 
    std::string desc() {return desc_;} 
}; 

這說每個房間都有一個名稱,描述和ID。然後創建房間,你這樣做:

Room room11(11, "North-West corner", "The corners are sharp on the eyes."); 
Room room21(21, "Storage room", "It's full of dust and boxes."); 
Room room31(31, "Grey room", "Tell my wife, hello.") 

cout << room31.desc(); // outputs "Tell my wife, hello" 

這是使用類反正的開端......

+0

Bingo。不是OP所要求的,但從長遠來看,它比「你錯過了」更有用得多。「 [鏈接到範圍的解釋。](https://en.wikipedia.org/wiki/Scope_(computer_science)) – user4581301

+0

謝謝,更有幫助理解我的他媽的整體。但是有沒有什麼辦法可以保存這個程序而不用重寫呢? –

相關問題