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.
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!!!
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;
"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!!!"
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.
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.
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?
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?
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.
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?
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();
};
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.