2016-12-08 22 views
-1

ここにコード全体を貼り付けることができませんでした。本体には大きすぎます。フルペーストはこちら(リンクは申し訳ありませんが、少し信用できないに見えます。)C++は特定の変数を表示しません

http://paste4btc.com/C4d9wg7X

しかし、私はエラーがどこかにこれらのブロックであることをかなり確信しています。

これはすべての項目、変数、値のリストです。このコードで

void item1() 
{ 
    if(!isItem1Randomized) 
    { 
     shopItem1 = rand()%4+1; 
     isItem1Randomized = true; 
    } 
    if(shopItem1 == 1) 
    { 
     shopItem1Name = purchasableItem1Name; 
     shopItem1Description = itemHealthUpDescription; 
     shopItem1Price = purchasableItem1Price; 
    } 
    else if(shopItem1 == 2) 
    { 
     shopItem1Name = purchasableItem2Name; 
     shopItem1Description = itemWoodenSwordDescription; 
     shopItem1Price = purchasableItem2Price; 
    } 
    else if(shopItem1 == 3) 
    { 
     shopItem1Name = purchasableItem3Name; 
     shopItem1Description = itemLeatherBootsDescription; 
     shopItem1Price = purchasableItem3Price; 
    } 
    else if(shopItem1 == 4) 
    { 
     shopItem1Name = purchasableItem4Name; 
     shopItem1Description = itemGarnetNecklaceDescription; 
     shopItem1Price = purchasableItem4Price; 
    } 
} 
void item2() 
{ 
    if(!isItem2Randomized) 
    { 
     shopItem2 = rand()%4+1; 
     isItem2Randomized = true; 
     cout << shopItem2; 
    } 
    if(shopItem2 == 1) 
    { 
     shopItem2Name = purchasableItem5Name; 
     shopItem2Description = itemSpookyMemeStaffDescription; 
     shopItem2Price = purchasableItem5Price; 
    } 
    else if(shopItem2 == 2) 
    { 
     shopItem2Name = purchasableItem6Name; 
     shopItem2Description = itemLeatherHelmetDescription; 
     shopItem2Price = purchasableItem6Price; 
    } 
    else if(shopItem2 == 3) 
    { 
     shopItem2Name = purchasableItem7Name; 
     shopItem2Description = itemHealthPotionDescription; 
     shopItem2Price = purchasableItem7Price; 
    } 
    else if(shopItem2 == 4) 
    { 
     shopItem2Name = purchasableItem8Name; 
     shopItem2Description = itemManaPotionDescripton; 
     shopItem2Price = purchasableItem8Price; 
    } 
} 
void item3() 
{ 
    if(!isItem3Randomized) 
    { 
     shopItem3 = rand()%4+1; 
     isItem3Randomized = true; 
    } 
    if(shopItem3 == 1) 
    { 
     shopItem3Name = purchasableItem9Name; 
     shopItem3Description = itemHealthUpDescription; 
     shopItem3Price = purchasableItem9Price; 
    } 
    else if(shopItem3 == 2) 
    { 
     shopItem3Name = purchasableItem10Name; 
     shopItem3Description = itemWoodenSwordDescription; 
     shopItem3Price = purchasableItem10Price; 
    } 
    else if(shopItem3 == 3) 
    { 
     shopItem3Name = purchasableItem11Name; 
     shopItem3Description = itemLeatherBootsDescription; 
     shopItem3Price = purchasableItem11Price; 
    } 
    else if(shopItem3 == 4) 
    { 
     shopItem3Name = purchasableItem12Name; 
     shopItem3Description = itemGarnetNecklaceDescription; 
     shopItem3Price = purchasableItem12Price; 
    } 
} 
void item4() 
{ 
    if(!isItem4Randomized) 
    { 
     shopItem4 = rand()%4+1; 
     isItem4Randomized = true; 
    } 
    if(shopItem4 == 1) 
    { 
     shopItem4Name = purchasableItem13Name; 
     shopItem4Description = itemHealthUpDescription; 
     shopItem4Price = purchasableItem13Price; 

    } 
    else if(shopItem4 == 2) 
    { 
     shopItem4Name = purchasableItem14Name; 
     shopItem4Description = itemWoodenSwordDescription; 
     shopItem4Price = purchasableItem14Price; 
    } 
    else if(shopItem4 == 3) 
    { 
     shopItem4Name = purchasableItem15Name; 
     shopItem4Description = itemLeatherBootsDescription; 
     shopItem4Price = purchasableItem15Price; 
    } 
    else if(shopItem4 == 4) 
    { 
     shopItem4Name = purchasableItem16Name; 
     shopItem4Description = itemGarnetNecklaceDescription; 
     shopItem4Price = purchasableItem16Price; 
    } 
} 

void itemSelector() 
{ 
    item1(); 
    item2(); 
    item3(); 
    item4(); 
} 
void buyMenu() 
{ 
    if(mazeActive == 2) 
    { 
     if((shop1[17][17] == shop1[playerY][playerX])) 
     { 
      isEnteredShop = true; 
     } 
     else 
     { 
      isEnteredShop = false; 
     } 
    } 
    if(isEnteredShop) 
    { 
     itemSelector(); 
     cout << "You have entered the shop. Items to buy: \n"; 

     if(!isItem1Bought) 
     { 
      cout << "1. " << shopItem1Name << ":\n" 
       << shopItem1Description << "\n" << "$" << shopItem1Price << "\n \n"; 
     } 
     if(!isItem2Bought) 
     { 
      cout << "2. " << shopItem3Name << ":\n" 
       << shopItem3Description << "\n" << "$" << shopItem3Price << "\n \n"; 
     } 
     else 
     { 
      cout << "Something broke"; 
     } 
     if(!isItem3Bought) 
     { 
      cout << "3. " << shopItem2Name << ":\n" 
       << shopItem2Description << "\n" << "$" << shopItem2Price << "\n \n"; 
     } 
     if(!isItem4Bought) 
     { 
      cout << "4. " << shopItem4Name << ":\n" 
       << shopItem4Description << "\n" << "$" << shopItem4Price << "\n \n"; 
     } 

     cout << "5. Exit shop. \n"; 

     cin >> shop1ItemBought; 
     if((shop1ItemBought == 1) && (shopItem1Name == purchasableItem1Name)) 
     { 
      playerHealthPoints = playerHealthPoints + itemHeartCanisterPlayerStatisticChanges; 
      playerCurrency = playerCurrency - purchasableItem1Price; 
      isItem1Bought = true; 
      isMoved = true; 
     } 
     else if((shop1ItemBought == 1) && (shopItem1Name == purchasableItem2Name)) 
     { 
      playerDamage++; 
      playerCurrency = playerCurrency - purchasableItem2Price; 
      isItem1Bought = true; 
      isMoved = true; 
     } 
     else if((shop1ItemBought == 1) && (shopItem1Name == purchasableItem3Name)) 
     { 
      speedStatistic++; 
      playerCurrency = playerCurrency - purchasableItem3Price; 
      isItem1Bought = true; 
      isMoved = true; 
     } 
     else if((shop1ItemBought == 1) && (shopItem1Name == purchasableItem4Name)) 
     { 
      playerDamage++; 
      playerCurrency = playerCurrency - purchasableItem4Price; 
      isItem1Bought = true; 
      isMoved = true; 
     } 
     else if((shop1ItemBought == 2) && (shopItem1Name == purchasableItem5Name)) 
     { 
      playerCurrency =- purchasableItem5Price; 
      hasSpookyMemeStaff = true; 
      isItem2Bought = true; 
     } 
     else if((shop1ItemBought == 2) && (shopItem1Name == purchasableItem6Name)) 
     { 
      playerCurrency = playerCurrency - shopItem2Price; 
      playerDefense+=2; 
      isItem2Bought = true; 
     } 
     else if((shop1ItemBought == 2) && (shopItem1Name == purchasableItem7Name)) 
     { 
      playerCurrency = playerCurrency - shopItem2Price; 
      playerHealthPoints++; 
      isItem2Bought = true; 
     } 
     else if((shop1ItemBought == 2) && (shopItem1Name == purchasableItem8Name)) 
     { 
      playerCurrency = playerCurrency - shopItem2Price; 
      playerMana+=10; 
      isItem2Bought = true; 
     } 
     isEnteredShop = false; 
    } 
} 

すべての作品、ショップで1つのセクション以外:

void itemHeartCanister() 
    { 
     purchasableItem1Name = "Heart Canister"; 
     itemHealthUpDescription = "Increases your maximum health by 1."; 
     purchasableItem1Price = 25; 
    } 

void itemWoodenSword() 
{ 
    purchasableItem2Name = "Wooden Sword"; 
    itemWoodenSwordDescription = "Increases your damage by 2."; 
    purchasableItem2Price = 40; 
} 

void itemLeatherBoots() 
{ 
    purchasableItem3Name = "Leather Boots"; 
    itemLeatherBootsDescription = "Increases your speed and defense by 1."; 
    purchasableItem3Price = 30; 
} 

void itemGarnetNecklace() 
{ 
    purchasableItem4Name = "Garnet Necklace"; 
    itemGarnetNecklaceDescription = "Increases your luck by 1."; 
    purchasableItem4Price = 50; 
} 

void itemSpookyMemeStaff() 
{ 
    purchasableItem5Name = "Spooky Meme Staff"; 
    itemSpookyMemeStaffDescription = "Fires spooky memes that do 4.20 damage each"; 
    purchasableItem5Price = 69; 
} 

void itemLeatherHelmet() 
{ 
    purchasableItem6Name = "Leather Helmet"; 
    itemLeatherHelmetDescription = "A leather helmet that increases your defense by 2"; 
    purchasableItem6Price = 35; 
} 

void itemHealthPotion() 
{ 
    purchasableItem7Name = "Health Potion"; 
    itemHealthPotionDescription = "A health potion that restores one health point"; 
    purchasableItem7Price = 20; 
} 

void itemManaPotion() 
{ 
    purchasableItem8Name = "Mana Potion"; 
    itemManaPotionDescripton = "A potion that heals 10 mana points."; 
    purchasableItem8Price = 20; 
} 

void itemEnergyPotion() 
{ 
    purchasableItem9Name = "Energy Potion"; 
    itemEnergyPotionDescription = "A potion that heals 10 points of health and mana."; 
    purchasableItem9Price = 45; 
} 

void itemStatusPotion() 
{ 
    purchasableItem10Name = "Status Potion"; 
    itemStatusPotionDescription = "A potion that removes all bad status effects."; 
    purchasableItem10Price = 120; 
} 

void itemIceSword() 
{ 
    purchasableItem11Name = "Ice Sword"; 
    itemIceSwordDescription = "A sword made of ice. Has a 40% chance to freeze enemies for 2 seconds."; 
    purchasableItem11Price = 150; 
} 

void itemBomb() 
{ 
    purchasableItem12Name = "Bomb"; 
    itemBombDescription = "A bomb. Blows stuff up."; 
    purchasableItem12Price = 30; 

} 

void itemToyPlane() 
{ 
    purchasableItem13Name = "Toy Plane"; 
    itemToyPlaneDescription = "A toy plane. Does absolutely nothing."; 
    purchasableItem13Price = 50; 
} 
void shopItemDatabase() 
{ 
    itemHeartCanister(); 
    itemWoodenSword(); 
    itemLeatherBoots(); 
    itemGarnetNecklace(); 
    itemSpookyMemeStaff(); 
    itemLeatherHelmet(); 
    itemEnergyPotion(); 
    itemStatusPotion(); 
    itemIceSword(); 
    itemBomb(); 
    itemToyPlane(); 
} 

この

は買いメニューのためのコードの残りの部分です。 3番目の項目以外のすべての項目をショップに印刷します。私は周りの項目を切り替えることができ、それらの特定の項目がどこにあっても、それらは印刷されません。どんな助けもありがとうございます。

+7

デバッガを使用してコードをステップ実行する方法を学ぶ必要があるようです。良いデバッガを使用すると、プログラムを1行ずつ実行し、どこからずれているかを確認することができます。これはプログラミングをする場合に不可欠なツールです。詳しい読書:** [小さなプログラムをデバッグする方法](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver

+1

[最小、完全、 – cpplearner

+3

[mcve](最小限の強調)を投稿すると、誰かがあなたのコードをデバッグするのを迷惑に思うかもしれません。 –

答えて

1

一見したところで、2と3を混ぜたように見えます。

if(!isItem2Bought) 
    { 
     cout << "2. " << shopItem3Name << ":\n" 
      << shopItem3Description << "\n" << "$" << shopItem3Price << "\n \n"; 
    } 
    else 
    { 
     cout << "Something broke"; 
    } 
    if(!isItem3Bought) 
    { 
     cout << "3. " << shopItem2Name << ":\n" 
      << shopItem2Description << "\n" << "$" << shopItem2Price << "\n \n"; 
    } 

はおそらく

があなたのプログラミングスタイルは、コードの重複の多くが含ま
3

、これはエラーを誘うなど、shopItem2Name、ないshopItem3NameisItem2Boughtを維持したいです。 @AJNeufeldはすでに特定の変数名をどのように混在させているかを示しました。変数名に数値があり、その数値が2より大きい場合は、代わりに配列を使用する必要があります。 もう1つの問題は、shopItemXNameshopItemXDescriptionなどがある、そうでなければ、店舗アイテムのプロパティがたくさんあることです。そのようなものを構造体にまとめることが最善です。そしてそのように、forループの中にrepetetive多くのコードをリファクタリングすることができます

struct ShopItem { 
    char *name; 
    char *description; 
    int price; 
    bool bought; 
}; 

const int nItems = 4; 
struct ShopItem shopItems[nItems]; 

後で上:次に、あなたはこのような何かを行うことができます

for (int i = 0; i < nItems; i++) { 
    if (!shopItems[i].bought) { 
     cout << i << ". " << shopItems[i].name << ":\n" 
      << shopItems[i].description << "\n" 
      << "$" << shopItems[i].price << "\n \n"; 
    } 
} 

これはまた、あなたの多くを保存し、明らかにしますあなたのお店が5つのアイテムを販売するように、後で変更する方が簡単です。

3

私の助言は、キーボードから戻ってくることです。深呼吸する。 1000から200まで7秒単位でカウントダウンしてください。

ここであなたが何をしているのかについて考えてみましょう。あなたがここに持っているコードのほんの少しは、まさにデータです。あなたが実際のデータとしてそのデータを表現すると、(少なくとも私の意見では)はるかに良いでしょう。

あなたの商品リストから始めましょう。各項目には、名前、説明、価格の3つの要素があるようです。それでは、そのための構造を定義してみましょう:私はあなたがしたいものを、物事を理解し、少なくともとして、

std::vector<item> database { 
    { "Heart Canister", "Increases your maximum health by 1.", 25 }, 
    { "Wooden Sword", "Increases your damage by 2.", 40 }, 
    /* ... */ 
    { "Toy Plane", "A toy plane. Does absolutely nothing.", 50 } 
}; 

:それと

struct item { 
    std::string name; 
    std::string description; 
    int price; 
}; 

を、私たちはこのような何か項目の私達のテーブルを定義することができます表示する4つのアイテムを選択します - データベースの最初の4つから1つ、2番目から4番目まで、など。

まず、範囲内の数値を生成する小さな関数を記述しましょう。現時点では、私たちは本当に、ただそれをできるだけシンプルにするために物事の最善の方法はないつもりだ:

int rand_range(int lower, int upper) { 
    int range = upper - lower; 

    return rand() % range + lower; 
} 

当社の商品は、ベクトルであるので、私たちは数で4つの項目を選択することができます:

std::vector<size_t> chosen_items; 

for (int i=0; i<16; i+= 4) 
    chosen_items.push_back(rand_range(i, i+4)); 

これらの項目を表示できるようにする必要があります。その後、我々はこのような何か私たちの項目を表示することができます

void display(item const &i) { 
    std::cout << i.name << "\t" i.description << "\t" << i.price << "\n"; 
} 

:項目を表示するために少し機能を起動してみましょう。これは、まださらなる改善の多くに開いている

for (int i : chosen_items) 
    display(database[i]); 

- しかし、私は、一般的なルートは、あなたのコードが行っていた方向よりもはるかに満足のいく結果を生み出すだろうと思います。

関連する問題