#Loop through a string with spaces in it?

1 messages · Page 1 of 1 (latest)

limpid basalt
#

I am trying to have an array of strings that I want to loop through as a shop for a text game. I want to print out the array and then if the user enters that number then it will remove it from the array, but I'm not sure if I am doing the array wrong where it cant have spaces, or if it needs to be something else, or if my solution is wrong.

int choice() {
  int choice;
  std::cout << "\n\nWhat will you choose? ";

  while (true) {
    std::cin >> choice;
    if (!std::cin) {
      std::cout << "\n\nInvalid Choice. Enter again ";
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      continue;
    } else
      break;
  }

  return choice;
}

std::string items[17] = {
      "Potion of Healing                3 Gold Pieces",
      "Potion of Plant Control                2 Gold Pieces",
      "Potion of Stillness                3 Gold Pieces",
      "Potion of Insect Control            2 Gold Pieces",
      "Potion of Anti-Poison                2 Gold Pieces",
      "Holy Water                    3 Gold Pieces",
      "Ring of Light                3 Gold Pieces",
      "Boots of Leaping                2 Gold Pieces",
      "Rope of Climbing                3 Gold Pieces",
      "Net of Entanglement                3 Gold Pieces",
      "Armband of Strength                3 Gold Pieces",
      "Glove of Missile Dexterity            2 Gold Pieces",
      "Rod of Water-finding                 2 Gold Pieces",
      "Garlic Buds                    2 Gold Pieces",
      "Headband of Concentration            3 Gold Pieces",
      "Fire Capsules                    3 Gold Pieces",
      "Nose Filters                    3 Gold Pieces"};

for (int i = 0; i < sizeof(items); i++) {
    std::cout << items[i];
  }
  std::cout << "0. Exit" << std::endl;
  std::cout << "\nWhat items will you Purchse?" << std::endl;
  pick = choice();
sage riverBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

noble timber
#

First of all, flushing an input stream (what you're doing when doing std::cin.clear()) is UB (or at least with fflush it is for sure).

Secondly: Don't use raw C-style arrays. Use std::array if you want a fixed size container or std::vector if you want a dynamically sized container

#

Since you want to remove stuff, probably a std::vector will be better

limpid basalt
limpid basalt
# noble timber Since you want to remove stuff, probably a `std::vector` will be better

this worked! thank you!

  std::vector<std::string> items = {
      "1.\tPotion of Healing\t\t\t\t3 Gold Pieces",
      "\n2.\tPotion of Plant Control\t\t\t\t2 Gold Pieces",
      "\n3.\tPotion of Stillness\t\t\t\t3 Gold Pieces",
      "\n4.\tPotion of Insect Control\t\t\t2 Gold Pieces",
      "\n5.\tPotion of Anti-Poison\t\t\t\t2 Gold Pieces",
      "\n6.\tHoly Water\t\t\t\t\t3 Gold Pieces",
      "\n7.\tRing of Light\t\t\t\t\t3 Gold Pieces",
      "\n8.\tBoots of Leaping\t\t\t\t2 Gold Pieces",
      "\n9.\tRope of Climbing\t\t\t\t3 Gold Pieces",
      "\n10.\tNet of Entanglement\t\t\t\t3 Gold Pieces",
      "\n11.\tArmband of Strength\t\t\t\t3 Gold Pieces",
      "\n12.\tGlove of Missile Dexterity\t\t\t2 Gold Pieces",
      "\n13.\tRod of Water-finding\t\t\t\t2 Gold Pieces",
      "\n14.\tGarlic Buds\t\t\t\t\t2 Gold Pieces",
      "\n15.\tHeadband of Concentration\t\t\t3 Gold Pieces",
      "\n16.\tFire Capsules\t\t\t\t\t3 Gold Pieces",
      "\n17.\tNose Filters\t\t\t\t\t3 Gold Pieces"};

for (int i = 0; i < items.size(); i++) {
    // std::cout << items[i];
    std::cout << items[i];
  }
sage riverBOT
#

@limpid basalt Has your question been resolved? If so, type !solved :)

noble timber
noble timber
fallow sun
noble timber
fallow sun
#

that's also good, just don't know if the op knows how to do that

limpid basalt
limpid basalt
noble timber
limpid basalt
#

i dont think i need it this complicated since its just a terminal rpg game, where if the user inputs that item then it removes it from the list and adds it to their inventory and it will check if that item is used later. so this solution would make more sense, i never knew you could do so much with cpp

limpid basalt
# noble timber I would use a vector of `Item` objects with smth like: ```cpp #include <iostream...

if i were to use this as a data struct, should it go in my player class? im confused on how i would have an inventory that would use these items. should i have a vector in the class that uses this struct type and then move items from the shop vector into the player vector?

class Player {

public:
  // Determines swordsmanship and general fighting expertise, higher is better.
  int initSkill = 0;
  // Stamina determins you general constitution, will to survive, determination,
  // and overall fitness. Higher means surviving longer.
  int initStamina = 0;
  // Luck indicates how naturally lucky you are.
  int initLuck = 0;

  // Current skill of the player, based on initSkill. Can be buffed or debuffed.
  int currSkill = 0;
  // Current Statmina of the player, based on initStamina. Will be decreased
  // with use and restored with rest.
  int currStamina = 0;
  // Current Luck, based on initLuck.
  int currLuck = 0;

  int gold = 0;
  // 1 Potion of SKILL
  // 2 Potion of STRENGTH
  // 3 Potion of LUCK
  int starterPotion = 0;

  Player() {
    srand(time(NULL));

    initSkill = (rand() % 6 + 1) + 6;
    initStamina = (rand() % 12 + 1) + 12;
    initLuck = (rand() % 6 + 1) + 6;

    currSkill = initSkill;
    currStamina = initStamina;
    currLuck = initLuck;
    gold = 30;
  }
};
noble timber
#

;compile

abc line 1
can't convert me
123
#include <iostream>
#include <vector>

struct Item {
    Item(const std::string& name, int price) : _name{name}, _price{price} { }

    friend std::ostream& operator<<(std::ostream& out, Item& item) {
        out << item._name << "\t\t\t" << item._price << "\n";
        return out;
    }

    private:
    std::string _name;
    int _price;
};

int user_num() {
    int n;
    std::string temp;
    std::cout << "Enter: ";
    while (true) {
        std::getline(std::cin, temp);
        try {
            n = std::stoi(temp);
            break;
        } catch (...) {
            std::cout << "Enter again: ";
        }
    }

    return n;
}

int main() {
    int a = user_num();
    std::cout << "\n" << a << "\n";

    Item i1{"golden item", 5};
    Item i2{"bronze magic", 2};
    std::cout << i1 << i2;
}
fast mountainBOT
#
Program Output
Enter: Enter again: Enter again: 
123
golden item5
bronze magic2
noble timber
#

^ I should mention that the bot here doesn't properly print tab characters, which is why it looks messed up

limpid basalt
#

i am trying to make a vector array of the Item data type, but im getting a multiple overloads of 'address' instantiate to the same signature

#

;compile

#include <iostream>
#include <string>
#include <vector>
struct Item {
  Item(const std::string &name, int price) : _name{name}, _price{price} {}

  friend std::ostream &operator<<(std::ostream &out, Item &item) {
    out << item._name << "\t\t\t" << item._price << "\n";
    return out;
  }

private:
  std::string _name;
  int _price;
};
int main(){
std::vector<Item()> items = {{"Potion of Healing", 3}};
}
fast mountainBOT
#
Compiler Output
In file included from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/x86_64-linux-gnu/bits/c++allocator.h:33,
                 from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/allocator.h:46,
                 from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/string:43,
                 from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/locale_classes.h:40,
                 from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/ios_base.h:41,
                 from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/ios:44,
                 from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/ostream:40,
                 from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/iostream:41,
                 from <source>:1:
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/new_allocator.h: In instantiation of 'class std::__new_allocator<Item()>':
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/allocator.h:128:11: 
limpid basalt
#

oops its because i had <Item()> when it should be <Item>

#

okay that worked!

#

!solved

sage riverBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

limpid basalt
#

!solved