How to "Stack" a number?

So in my code I am trying to build a basic choice based game where you either gain or lose gold based on your choices once you lose all your gold you lose the game and if you reach 3000 gold you win

(I am still a newbie so hopefully my code isn't to hard to make out)

right now my problem is how to get my numbers to stack ...i'm using float since this is the only one I know (ya i know i'm an idiot) but if you can tell me a better thing to use or point out any problems it would be much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include <cstdlib>
#include <iostream>
#include <iostream>

using namespace std;

int main()
{
    string response1; //what they reply
    float gold = 1000;
    int r1;
    
    string x = "Hello Player what is your name? \n";
    cout << x ;
    cin >> response1;
    cout 
           << "Hello " 
           << response1 
           << " welcome to the world of Hunne.\n" 
           << "\nAfter your long journey you enter a tavern!\nWould you like to buy some soup for 100 Gold?\n"
           << "\nYou currently have " 
           << gold 
           << " Gold.\n"
           << "\n0 = yes, 1 = no\n";
           
    cin >> r1;
           
           if (r1 == 0)
           {
                  cout 
                       << "Your soup seems to have many odd floating objects in it, you feed it to the dog.\n" 
                       << "You now have " << gold-100 << " Gold.\n"
                       << "As you exit the tavern you see someone getting mugged.\n"
                       << "Should you save them?\n"
                       << "\n0 = yes, 1 = no\n";
                       
                  cin >> r1;
                       
                       if (r1 ==0) 
                       {
                              cout 
                                   << "You rush in and attack the mugger."
                                   << "\nHe turns his attention to you. He stole 200 gold\n"
                                   << "He stole 200 Gold!!\nYou now have "
                                   << gold - 200
                                   << " Gold.\n";
                       }
                       else 
                       {
                            if (r1 != 0)
                            {
                                cout
                                    << "You turn the other direction and allow the man to be mugged."
                                    << "\nYou find 100 Gold on the ground"
                                    << "You now have " << gold + 100 << " Gold.\n";
                                    }
                  }
           }
           
           
           
           else 
           {
                if (r1 != 0)
                {
                       cout 
                            << "You leave the tavern. !!A mugger appears!!\n" 
                            << "He stole 200 Gold!!\nYou now have "
                            << gold - 200
                            << " Gold.\n" 
                       ;
                }     
           }
    
    
    
    char c;
    cin >>c;
    
    system ("Pause");
    return 0;
}


<< "You now have " << gold-100 << " Gold.\n"
This doesn't do what you think it does.
First, it prints "You now have ". Then, it prints gold-100, followed by "Gold.\n".
At no point do you ever modify the actual value of gold.
<iostream> is included twice, the second one will have no effect.
@xismn:
i don't know if you read the code carefully, this is exactly what OP need.

@OP:
you can't make any program by hard coding it like this one.
if you want to make a game, try using OOP, it's a lot better.

if you don't know how to use OOP, and still insist to try this game, i suggest you try the following:
- the game body should be in a loop.
- the assignment and binary operators are there to change variables, so use them:
instead of:
cout<< "You now have " << gold-100 << " Gold.\n";
use this:
1
2
gold -= 100;
    cout<<"you now have "<<gold<<" Gold."<<std::endl;

do the same for all similar cases.
- don't let the user know you use number to represent commands, you request a command from the user as a string, and transforming it to a unique number is your choice, you can still use string::compare().
- your code shall not contain the conversation of the game, you should store the conversation in another place (like a two dimensional array), and the code must be able to work on any conversation provided.
@Rechard3:
I do not plan for this to be any type of "game", it is going to be a command prompt 0,1 that wastes space on my hard drive ...i'm using it to explore c++ since I couldn't think of a better way. However i will try to use string to give my cout a variable so it doesn't sit in my code ( if this is even the right method to use should my string be at the bottom of the main with a reference up top or in a totally new source file? i'll try to do both for now)

I will fool around with this OOP thing (and google it to find out what it is) and i'll try the gold -= 100; to see if that 100 carries to later parts of the code.

@xismn
you are half right gold-100 doesn't do what I want it to do however if you look gold-100 doesn't have " " around it so only the calculation is presented.
So i've changed my code the

gold -= 100;
cout<<"you now have ">>gold<<" Gold";

is working perfectly ...However I have a new problem (if this should be posted in a separate forum post since it is a different topic I will do so)

I agree that my code should not include my dialogue so I am looking for a way to move it before I used
string x = "Hello Player what is your name? \n";
cout << x ;

but that is messy so I would like to put it in a different Source File and link it to my code ...is this even possible to do?
I don't see anything wrong with your code to prompt the user being in your program. If you were building a decently-sized game or interactive program that was concerned with clean code then it might be different. But if you're doing this to get your feet wet, I wouldn't worry about it. Unless of course you want to explore those opportunities.

If you do want to do something different, try handling all prompts and user input via functions first. It might give you some practice with program flow and modularity. OR, you could read in all of your game's story prompts and such from a text file instead of having it all hard-coded. Which makes your code easier to modify and more readable.
Last edited on
Is there a big list of functions and what they do? ...unity has one for java does c++ have one

right now i'm trying to create a random number then make a true/false statement based on whether the number is odd/even I keep getting an error report

expected primary-expression before '%' token

float chance = %9;
(this is the line which is messing up ...i've tried int, char, float, string I can't get it right unfortunately so a big list would help a ton)
in C++, the character % is not a percentage mark, instead, it is the division remainder operator.
you probably meant to do this:
float chance = 9.0f/100.0f;
looks like you're a self learner with no organised path to learn through, can i suggest you read one of the following:
C++ Primer 5th edition, Stanley B. Lippman.

C++ the complete reference , Herbert Schildt.

any should be enough for a start, i strongly recommend you read one of them before starting with this language.
@Rechard3
you clearly did not understand me ...i was using %9 to give a random number from 0-9 which did not work

float chance = 9.0f/100.0f;

also did not work ...unless it is 100.0f/100.0f it will turn out a false

What I want is a 50/50 or 70%-90% chance of a true

I am not a self learner that is impossible ...I am going to use people from the internet to teach me until I start my scripting course in a few weeks

I looked at your book references ...they are crud and not what I was looking for nothing inside was helpful if you know of " a big list of functions and what they do" then please reference it
this same site you are posting in contains a full reference on the standard features of the language.
http://www.cplusplus.com/reference/

EDIT -
and if you want a random number, use the standard function rand() contained in the <cstdlib> header.
http://www.cplusplus.com/reference/cstdlib/rand/?kw=rand

i think i'm outdated, because i haven't started with the new standard.
the 2011 standard offers another library that you might want to look at:
http://www.cplusplus.com/reference/random/
Last edited on
this is exactly what I was looking for thanks
Topic archived. No new replies allowed.