2017-02-27 6 views
0

私はRPGショップを作成しています。商品、金、商品価格が必要です。本質的に目録を作成する。私が達成しようとしているのは、選手のゴールドが0の場合、彼らはインベントリにアイテムを追加することができず、ネガティブゴールドを持つことができないということです。余りを持つ配列にアイテムを追加するカウンタ

私のコードをデバッグモードで実行しているときは、私が望むことをしているように見えますが、関数が要求した金額を出ても、プレーヤーが要求した金額は返却されていません。

私はまだC++を初めてお使いです。

おかげ

#include <iostream> 
#include <string> 

using namespace std; 
// Global consts 
const int numItems = 4; 
const string items[numItems] = {"boots", "hats", "cats", "bats"}; // create string array of numItems items. 


// Create stuct, that holds: 
// Item, gold, price. 

struct Inv { 
    int pInv[numItems] = {0, 0, 0, 0}; 
    int gold = 100; 
    int itemPrice[numItems] = { 10, 6, 12, 15 }; 
}inv; 

void iniItems(); 
void printItems(); 
bool buyItems(); 
bool sellItems(); 

int main() { 

    bool isDone = false; 
    iniItems(); 

    while (isDone == false) { 
     printItems(); 
     int choice; 
     bool x = false; 
     cout << "\nWhat would you like to do? Enter (" << 1 << "-" << 2 << "): " << endl; 
     cout << "1: Buy Items. \n2: Sell Items." << endl; cin >> choice; cout << endl; 
     while (x == false) { 
      if (choice == 1) { 
       x = buyItems(); 
      } 
      if (choice == 2) { 
       x = sellItems(); 
      } 
     } 
    } 


    system("pause"); 

    // dynamic memory not implemented yet. Must wait for working fix of shoppe.cpp 
} 

void iniItems() { 
    cout << "** Shop Inventory: **" << endl; 
    for (int i = 0; i < numItems; i++) { 
     cout << i + 1 << " - " << items[i] << " - price: $" << inv.itemPrice[i] << endl; 
    } 
} 

void printItems() { 
    cout << "\n** Player Inventory: **" << endl; 
    cout << "Gold: $" << inv.gold << endl; 
    for (int i = 0; i < numItems; i++) { 
     cout << inv.pInv[i] << " x " << items[i] << endl; 
    } 
} 

bool buyItems() { 
    bool exit = false; 
    int amount; 
    const int remainder = 10; 
    printItems(); 
    cout << "\nEnter -1 to quit." << endl; 
    cout << "What would you like to buy? Enter (" << 1 << "-" << 4 << "): " << endl; 
    // Get item info. 
    while (exit == false) { 
     int inp; 
     cout << "Item: "; cin >> inp; cout << endl; 
     cout << "Amount: "; cin >> amount; cout << endl; 
     // Check if input is valid. 
     if (inp > 0 && inp <= numItems) { 
      if (amount >= 0) { 
       inv.pInv[inp - 1] = 1 * amount; 
       inv.gold = inv.itemPrice[inp - 1]/amount; 
      } 
      // If gold is 0, make sure the user cannot gain more items. 
      if (inv.gold <= 0) { 
       int tmp; 
       inv.gold = 0; 
       tmp = remainder - amount; 
       for (int i = tmp; i >= 0; i++) { 
        inv.pInv[inp - 1]--; 
       } 
       return inv.pInv[inp - 1]; 
      } 

      if (inp == -1) { 
       return true; 
      } 
      if (inp > numItems) { 
       cout << "Enter valid number." << endl; 
       return false; 
      } 
      else return false; 
     } 
    } 
    if (exit == true) { 
     return true; 
    } 
} 

答えて

0

は、だから私はbuyItems()関数で金のためにカウンターでループしながら、ダウンDOにコードを制限しました。 誰かが興味を持っているなら、それはここです。

do { 
    inv.gold -= inv.itemPrice[inp - 1]; 
    ++(inv.pInv[inp - 1]); 
} while (inv.gold > 0); 

if (inv.gold < 0) { 
    inv.gold = 0; 
    inv.pInv[inp - 1]--; 
} 

printItems(); 
関連する問題