need some help please

This is a discussion on need some help please within the C++ Programming forums, part of the General Programming Boards category; Need help changing a stored variable inside a string: Both parts of the code are on here but if you ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    Unhappy need some help please

    Need help changing a stored variable inside a string:
    Both parts of the code are on here but if you want to see with syntax colouring i have uploaded onto codepad
    Click here for code with syntax highlights
    Here are the variables so it isnt confusing:
    Code:
    string UserName;
    string PlayerInv = "\nInventory:";
    string ARROW = "\nArrows x";
    stringstream ss;
    int Money;
    int PlayerHP = 75;
    int Foodx = 0;
    int Drinkx = 0;
    int arrowx = 0;
    bool arrow_owned = false;
    enum ShopMisc{Arrow = 2, Food = 10, Drink = 10,WeaponPoison = 35,} //Ignore all but arrow
    What i want to do is that if the user chooses to buy arrows and they already have some in there inventory [arrow_owned == true] instead of when they buy more i dont want it to say this in the inventory
    Example:
    Inventory:
    Food
    weapon poison
    Arrow x10 <<<<
    Arrow x23 <<<<
    Bow

    instead when they are already owned i want the variable 'arrowx' to be changed by arrowchoice[how many more the user chooses to buy]
    ive been stuck with it for AGEESSSS so decided to join this forum in search of help :S
    Here is the main part of code im stuck with:

    Code:
    case 1:
                     {
                       cout << "How many Arrows would you like? :";
                       cin >> arrowchoice;
                       Sleep(1000);
                       cout << "\nYou have chose "<<arrowchoice<< "  Arrows costing " <<(Arrow*arrowchoice)<<" Gold peices\n";
                       if(Money - (Arrow*arrowchoice) >= 0)
                       {
                                if(arrow_owned == true)
                                {                                                           //just a guess so its obv wrong
                                    ss >> PlayerInv >> ARROW >> arrowx;
                                    Money = (Money - (Arrow*arrowchoice));
                                    cout << "You now have: " << Money << " Gold peices\n";
                                    arrowx = (arrowx + arrowchoice);
                                    ss << PlayerInv << ARROW << arrowx;
                                    PlayerInv = ss.str();
                                    goto SHOPEND;
                                }                 
                       Money = (Money - (Arrow*arrowchoice));
                       cout << "You now have: " << Money << " Gold peices\n";
                       arrowx = (arrowx + arrowchoice);
                       ss <<PlayerInv << ARROW << arrowx;
                       PlayerInv = ss.str();      
                       arrow_owned = true;
                       goto SHOPEND;
                       }
                       else
                       {
                       cout << "You dont have enough gold for "<< arrowchoice << "arrows\n";
                       goto SHOPEND;                  
                       }
                     }
    All works fine apart from the part if(arrow_owned==true){}
    it charges for more than 1 and doesnt increase the amount owned

    any help ?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    I don't think you're using arrow_owned in a useful way so you might as well scrap that whole section. That would avoid charging the player twice.

    I'm not sure what your problem is with reporting the amount of arrows the player has, but there is nothing wrong with arrowx = arrowx + arrowchoice; maybe you defined another, more localized version of arrowx.

    Code:
    int arrowx = 30;
    {
       int arrowchoice;
       int arrowx = 0; // more local version of arrowx
       cout << "How many arrows? ";
       cin >> arrowchoice;
       arrowx = arrowx + arrowchoice; // arrowchoice + 0 here
    }
    cout << "Arrow x" << arrowx << "\n"; // and 30 here
    That's the best I can think of, but this logical error rarely looks exactly like what I showed. If you can find something like this, great, but if you can't, just post again with updated code.
    Last edited by whiteflags; 08-24-2009 at 09:53 AM.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    Noo, im not trying to get it to just output it.. i could do that
    im trying to Get the variable into the string which i can do using string stream
    But if arrows are bought twice
    for example
    first time 10 arrows are bought
    second time 30 arrows are bought it will say:

    Inventory:
    Arrows x 10
    Arrows x 30

    instead of what i want which is for them to stack up:
    Inventory:
    Arrows x 40

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    Oh well, forgive me for being thick.

    When you push stuff onto a string stream the data gets tacked to the end. Likewise, if you also pull data from the string stream, it doesn't disappear, the position marker only moves. When you use the same stream to do both of these things it can be confusing.

    C++ makes it possible to refresh your stream in a pretty unintuitive way.

    Code:
    ss >> PlayerInv >> ARROW >> arrowx; // pull the initial state
    // buy more arrows here
    ss.str(string()); // the refresh
    ss << PlayerInv << ARROW << arrowx; // push the current state
    You can only replace the internal buffer. I admit that wasn't obvious to me, and I had to look it up.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21