How to stop money from going over

Pages: 123... 7
Ok so i have a game and in it you can buy stuff but when you do you can go over the price once then it detects you dont have anymore, i want it to detect it before it goes over, here is a simple program i made to demonstrate.

I wont post my game because its over 650 lines of code.

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
#include <iostream>

using namespace std;

int main()
{
    int money = 5000;
    int buy;
    bool endLoop = false;

    cout << "buy stuff" << endl;

    while(endLoop != true)
    {
        cin >> buy;

        if(buy == 1)
        {
            if(money > 100)
            {
                money -= 800;
                cout << money << endl;
            }
            if(money < 100)
            {
                cout << "Money depleted" << endl;
            }
        }
    }
}
Also how can i manage large code base and keep everything neat and be able to go through it with ease, i comment my code and do my indents and break things up nto neat functions and keep everything consistant but there is sooooo much code its hard to go through. i only have 600+ lines of code and its hard for me i cant even imagine how Windows engineers do it with over 50 million lines of code!!!
Last edited on
Maybe your problem is because you are checking it is greater than 100 then you are taking off 800 dollars and anyways shouldn't you be using an if/else or ternary operator? Otherwise it will call the first if then call the second so or did you do it on purpose to say you have 800 then buy afterwards you have 0 then it outputs saying you have 0 dollars? Ps I would recomend using >= since technically if someone costs 100$ you can still buy it with 100$ you do not need 101$.

And as far as your problem.
use a if statement for you input maybe?
if( money >= 800 ) std::cin >> buy;
ok, how about my other question?

"Also how can i manage large code base and keep everything neat and be able to go through it with ease, i comment my code and do my indents and break things up nto neat functions and keep everything consistant but there is sooooo much code its hard to go through. i only have 600+ lines of code and its hard for me i cant even imagine how Windows engineers do it with over 50 million lines of code!!!"
millions of lines of code collectively. Many people working on sections rather than the whole, with the code split up on classes and files
How are you organizing the game components?

In my experience, breaking down a game into its components and then referencing them in your main source file makes it a lot easier to check and update code.

For example, in your code, you could've made a MONEY class:
1
2
3
4
5
6
7
8
9
10
11
12
class MONEY
{
    public:
        MONEY();
        ~MONEY();

        void Buy();
        int GetCurrentMoney();

    protected:
        int CurrentMoney;
};


That way, you only need to implement the MONEY class in a separate header (say money.h, for example), define the methods in its own source file (money.cpp referencing money.h in the includes), then instantiate them inside your main source when needed, instead of messing up your main code with lots of component codes. This is applicable to every game component actually. It's what I did with my Direct3D9 code, implementing the wrapper methods in separate classes to make debugging and code optimization easier for one person.

In short, write only the game code inside the main source file. Write the components in their own header/source files.
Last edited on
Well i dont like making classes just for one variable it seems like just a waste of time space and code to me, but i do group my variables into one struct and that helps alot but someone told me not to put functions in header files, although that would help alot im not sure why i shouldnt do it. I know i can put classes and structs in header files but why not functions?
You can declare functions in header files, but their definitions should be in a source file. Otherwise, your compiler is going to throw redefinition errors, unless you are using inline or static. That would be inefficient, however.

Classes and structs *are* the same in C++, except that classes have private members (members can only be accessed by its own methods) by default, and structs are public (members are accessible outside the class).

Classes and structs are just a way to make codes easier to organize and read. Small projects usually don't need them to work, but bigger projects make them a godsend.
Last edited on
Well my problem is scrolling through 600 lines of text, it's just hard to do and i dont want to make a class for everything, i only need one class anyways, and i cant put my functions in a seperate header file so what can i do?
You CAN declare functions in a separate header file. Then define them in a source file.

myheader.h
1
2
3
4
5
#ifndef __myheader__
    #define __myheader__
    void myfunction();
    void dosomething();
#endif 


myheader.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "myheader.h"

void myfunction()
{
    //do stuff
}

void dosomething()
{
   //do stuff
}


main.cpp
1
2
3
4
5
6
7
8
9
#include "myheader.h"

int main()
{
    //will call the definition in myheader.cpp
    dosomething();
    myfunction();
    return 0;
}


EDIT:
corrected as posted below
Last edited on
I thought main always had to be in a .cpp file?
he meant main.cpp
ok cool, so where would i put a class if i made one? would i make another .h file or put it in myheader.h and put the function prototypes in the class? Also i know i shouldnt split up every function into a different file, so how do i decide what functions go into one file and what ones go into another?
Last edited on
Place the class prototypes in a header and their definitions in a source file like nenekonesha mentioned. I would group the functions that are similar together.
class prototype? i dont think ive ever made a class prototype before what does it look like? so i have something like this:

main.cpp

1
2
3
4
5
6
7
8
9
#include <iostream>
#include "test.h"

using namespace std;

int main()
{
    start();
}



test.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "test.h"
#include <iostream>
#include <string>

using namespace std;

void start()
{
    string name;
    cout << "This is the beginning" << endl;
    getline(cin, name);

    cout << name << endl;
}



test.h

1
2
3
4
5
6
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

void start();

#endif // TEST_H_INCLUDED 


so where would the class prototype go and where would the definition go?
well I meant the prototypes that are inside of the class sorry but to prototype an actual class you would do this; class Someclass;

That would work though the way you have it setup.

Most of the time in teh header you would put the class
1
2
3
4
5
6
class Someclass
{
    public:
        Someclass();//prototype
        Someclass( int ); //prototype
};


then in the source for the header you would put the definitions
1
2
3
4
#include "someheader.h"

Someclass::Someclass(){} //definiton
Someclass::Someclass( int ){} //defintion 

oh ok but i dont need to do that though right? i can just make a .h file with a class in it and just include the class in the main.cpp right? There was something else i was going to ask but now i forget...


also when i make a class and try to make a string it says that string does not name a type what do i do? i need to enter #include <string> but your not supposed to do that in header files so what do i do?
Last edited on
I'm not sure I just #include <string> in the header otherwise I guess use const char* maybe? And yeah you won't need a prototype unless you are calling a class b inside of class a when class b is declared after class a.
ex
1
2
3
4
5
6
7
8
9
10
11
class b;
class a
{
    public:
          a( b );
};
class b
{
    public:
        b();
};
ok so can you help me im getting errors left and right, i understand the part with functions but trying to do it with a class is causing me trouble.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "test.h"
#include "Test_class.h"

using namespace std;

int main()
{
    testClass tc;

    tc.start();
}



test.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "test.h"
#include "Test_Class.h"
#include <iostream>
#include <string>

using namespace std;

void testClass::start()
{
    name;
    cout << "This is the beginning" << endl;
    getline(cin, name);

    cout << name << endl;
}


test.h

1
2
3
4
5
6
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

class testClass;

#endif // TEST_H_INCLUDED 


Test_Class.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef TEST_CLASS_H_INCLUDED
#define TEST_CLASS_H_INCLUDED

#include <string>

class testClass
{
    public:
        testClass();
        ~testClass();
        void start();

    private:
        std::string name;
};

#endif // TEST_CLASS_H_INCLUDED 
The test.h seems pointless you don't need that. or just put the stuff from Test_Class into test.h.
also whats up with line 10 on test.cpp? other than that you need to create a consturctor
testClass::testClass() to call the start function.
Pages: 123... 7