ENIGMA Forums

Outsourcing saves money => Programming Help => Topic started by: Darkstar2 on April 29, 2014, 05:33:50 pm

Title: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 29, 2014, 05:33:50 pm
Ok I am aware of the variable types, int, double, char, bool, etc, and the operators, if / then etc.

Here is the scenario:

Ask a person to enter a number from 1 to 100.
On the condition that a valid range is entered,
output "The number you entered is (insert number here)".
Otherwise output "Invalid entry - try again"
and it would proceed to asking the question again
until a valid entry is entered.

Now I would know how to build this, and to declare an int variable, I know how to use cout and cin,
brackets, int main(), namespace, includes, etc.
and I know how to check variables with if/then.

There are 2 ways to do this

1)  If the user does not input the correct range a message informs them that the entry is invalid and that the program is terminated.

2) The user is informed of an invalid range and is is informed to try again, where the same question will be asked ,for as long as the user enters the valid range.

How to do number 2 ????

In BASIC

10 PRINT"Enter a number from 1 to 100"
20 INPUT A
30 IF A < 1 OR A > 100 THEN PRINT "Invalid - try again":GOTO 10
40 PRINT"The number you entered is "A
50 END

In ASM you have JNE / JEQ  to jump back to the code.

What do you do in C++ ?

Title: Re: How to do conditional branching in C++ ?
Post by: TheExDeus on April 29, 2014, 06:25:24 pm
Code: [Select]
not_in_range = true;
while (not_in_range){
if ( in_range_check ) not_in_range = false;
}
Or a while(true) with a break.
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 29, 2014, 07:15:35 pm
What about GOTO is there such a thing as a functional GOTO in C++ I heard that yes, but heard that people use it for complicated loops, is it bad practice to use it otherwise ?
Would I be complete laughing stock of the C++ community if I used

:startQ
(code to ask question) + conditionals

if criteria not met GOTO :startQ
thing ? 

If it's valid why not use it right ? :D  I remember using this in QBASIC, would this work in C++ ?

Title: Re: How to do conditional branching in C++ ?
Post by: Goombert on April 29, 2014, 08:16:10 pm
Josh uses goto in some places in ENIGMA/JDI, goto also works in ENIGMA as in you can use it in your game.
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 29, 2014, 08:24:22 pm
Josh uses goto in some places in ENIGMA/JDI, goto also works in ENIGMA as in you can use it in your game.

I tried once it said function does not exist !

How would I use goto in LGM ?

Title: Re: How to do conditional branching in C++ ?
Post by: Goombert on April 29, 2014, 08:27:58 pm
Just like you would in C++, it's not a function, you have to create a label.

http://msdn.microsoft.com/en-us/library/b34dt9cd.aspx
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 29, 2014, 09:38:52 pm
Yes I know about labels, I used those quite a lot in QB45 lol!

Strange I tried goto once in LGM and it did not work.

2 questions:

1) Is there a gosub type command where code can RETURN back to where it was called and continue code ?

2) in C++ if I use cout I am outputting to the console.
Can I use LGM to create console apps? (no not console as in android, ios lol) you know what I mean.

I tried using cout, it compiled, but nothing was output, and yes I tried executing my EXE from console. :D
Title: Re: How to do conditional branching in C++ ?
Post by: Goombert on April 29, 2014, 09:42:17 pm
Uhm, I am not sure, we have built-in print functions which are the ones you should really use, but, they may be getting removed because of the following issue.
https://github.com/enigma-dev/enigma-dev/issues/701
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 29, 2014, 10:10:08 pm
What do you recommend for working in C++ ?

Some people say Visual C++, visual studio, some swear by Eclipse...... Will all my compiled C++ to EXE be stand-alone or require dependencies ?
I know they might if I am using windows related calls, but for a custom program where I am not using any windows dialogs, etc, if I use VS C++ will my EXE require the Visual C++ redist ?   IN which situations will my EXE require dependencies and which ones where I can make a fully standalone one?
Title: Re: How to do conditional branching in C++ ?
Post by: Goombert on April 29, 2014, 10:18:15 pm
You're fine to use Visual C++ as long as you don't use any of the .NET framework. But I would also have to recommend Eclipse, it is amazing. You should also try Qt Creator, the Qt Framework is becomming the .NET of Linux platforms and it is pretty damn amazing.

(http://faces.eti.br/wp-content/uploads/2009/06/qtcreator_mainwindow.jpg)
https://www.google.com/#q=qt+framework

It also has JavaScript and LLVM, Java itself is also very awesome for applications, I would never use Microsoft products for software development or any kind of programming.
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 29, 2014, 10:29:33 pm
1) Do all those compile the same EXE, is there performance difference amongst those ?

So if I understand correctly say for example I use eclipse and make an EXE, no dependencies will be required right ?

If there are dependencies will the program tell me which ones?

2) BTW I figured something out in regards to my question on goto, instead of using goto I could use this instead:

Quote
int rate = 0;

while(rate < 1 || > 100)
{
    cout <<"Rate my ass from 1-100" << endl;
    cin >> rate;
    if (rate < 1 || > 100)
        {
         cout <<"Invalid range - Please try again!" << endl;
         }
}
cout <<"Thank you!";

return 0

So in this case I would not need the goto command
and it would be a cleaner way of doing things :D

I currently cannot even test this as I do not have a C++
tool yet. :D

Title: Re: How to do conditional branching in C++ ?
Post by: Goombert on April 29, 2014, 10:39:05 pm
The dependencies are up to you, all of cin/cout is in the standard c library. Say you stat using Box2D headers, then you have a Box2D dependency, say you start using Win32 headers then you have a Windows dependency and have to compile that code with the headers by either using Visual Studio or MinGW, as MinGW has most of the Windows SDK headers including Direct3D and DirectSound.
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 29, 2014, 10:41:41 pm
Correction to the above

Quote
int rate = -1;

while(rate < 0 || > 100)
{
    cout <<"Rate my ass from 0-100" << endl;
    cin >> rate;
    if (rate < 0 || > 100)
        {
         cout <<"Invalid range - Please try again!" << endl;
         }
}
cout <<"Thank you!";

return 0
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 29, 2014, 10:43:18 pm
The dependencies are up to you, all of cin/cout is in the standard c library. Say you stat using Box2D headers, then you have a Box2D dependency, say you start using Win32 headers then you have a Windows dependency and have to compile that code with the headers by either using Visual Studio or MinGW, as MinGW has most of the Windows SDK headers including Direct3D and DirectSound.

Eclipse can use MinGW right ?  DO I have to install MinGW separately and then tell Eclipse to use whatever compiler I want or do the compilers already come with Eclipse, QT, etc.?
Title: Re: How to do conditional branching in C++ ?
Post by: Goombert on April 29, 2014, 10:45:34 pm
No you have to install the compiler separately with Eclipse, because it is mainly used for Java programming, so you have to install JDK/JRE separately. Qt offers downloads that do include the compiler already for you which are usually the ones you want because the modify GNU Make to have QMake.

http://qt-project.org/downloads
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 29, 2014, 11:04:43 pm
Can you create GUI apps in QT using C++ ?

Also about all those includes / headers you have to include
at the beginning, how do you know which ones to use?
How can one memorize it all ?  How do I determine the headers to use based on what I want to do?

Title: Re: How to do conditional branching in C++ ?
Post by: Goombert on April 29, 2014, 11:09:52 pm
Yes Qt is a generic framework for software like .NET except more fully featured and more lightweight because it is C++, it is often used to make games as well.

And you just pretty much have to memorize the headers, sometimes I Google when I forget some, but usually that is why documentation is provided for API's to tell you what headers to use. And Qt is well known for its great documentation, C# and Java are the same except Eclipse IDE will tell you where to import from and automatically import the packages for you, kind of like if C++ had a thing that would auto write your include statements for you.

Why don't you start off trying to build LateralGM? It's a pretty big application but it is easy to build.
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 29, 2014, 11:22:36 pm
LMAO you are kidding me right ?

That's probably the funniest thing I read today :P

Yeah I can barely build shit with C++ yet, but thanks for your confidence  ;D


Title: Re: How to do conditional branching in C++ ?
Post by: Goombert on April 29, 2014, 11:26:34 pm
No I am being serious, I meant for you to do it as a stepping stone, it is extremely easy to build, you just have to download LGM's source and download eclipse, then import LGM, and build it.

1) Download LGM's source code. Visit the GitHub link and select "Download ZIP"
https://github.com/IsmAvatar/LateralGM

2) Download Eclipse stand alone, the current stable which is what I use.
https://www.eclipse.org/downloads/

3) Extract LGM's source code and Eclipse from their respective ZIP archives, then launch eclipse.

4) File->Import, and select "Existing Projects into Workspace", find LGM and import her.

5) When it finishes importing, click the green run button, when it asks what you run as select "Run as Application"

And there you go, you now know how to build LGM, so go on, try it!  (Y)
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 29, 2014, 11:34:43 pm
I thought you said LGM was done in JAVA, I know nothing of JAVA and I don't want to either, i'm in over my head, I barely know enough GML to make advanced stuff.   I still have lots to do here before getting to that but will definitely keep this in mind just out of curiosity now that you have me curious :D  First I would like to learn B2D since physics is still broken and not included in the IDE.  BTW did you get a chance to check my file (EGM) ?

Hopefully I could eventually do some creative stuff I could
add to LGM........  :P
Title: Re: How to do conditional branching in C++ ?
Post by: Goombert on April 29, 2014, 11:43:15 pm
I don't have any need to check that file because you said instance create was working fine, as long as instance create is not broken IDC. But I did just fix the compiling errors so the Studio functions will build but they aren't finished. Anyway, that file has no use to me because I already know what needs done to add full compatibility for Studio physics.

Anyway, if you would give Java a try you would see it is exceptionally easier than using GML, Eclipse has beautiful automatic completion, and you can read documentation as you type functions, it is way easier than digging through pages to find functions like you do in GML, plus you can Google and find a million results for Java programming, where as GML is not you know a widely used programming language. Java is also amazing with eclipse because you can click in the error log and be taken right to the exact line that caused the error and get a full stack trace showing the whole chain of events. Similar arguments apply to C# as well, but C# is not as good at this stuff, this is why people rave about Eclipse as an IDE. Anyway, this is exactly why C# and Java are popular to newbies, if you would give them a try you'd see they are much easier to learn than GML, and you'll learn proper programming and that knowledge can then be applied to learning C++.
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 30, 2014, 12:09:12 am
I don't have any need to check that file because you said instance create was working fine, as long as instance create is not broken IDC. But I did just fix the compiling errors so the Studio functions will build but they aren't

You said you fixed B2D physics with that fix, which it was not fixed ,that's the initial reason I sent the EGM, the instnace thing was just a small detail.  I don't care about the studio physics extension, but you said B2D was functional and asked how do I apply B2D with the project I did in that EGM, because nothing moves.....Do I have to
do it all manually with the B2D commands, will it work, if so I will just make a script to make it easier but need to make sure B2D physics does work as to not do this for nothing, you did mention B2D was fixed. :)  I could care less about Studio compatibility for now if B2D can do more.

Quote
Anyway, if you would give Java a try you would see it is exceptionally easier than using GML, Eclipse has beautiful automatic completion, and you can read documentation as you type functions, it is way easier than digging through pages to find functions like you do in GML, plus you can Google and find a million results for Java programming, where as GML is not you know a widely used programming language. Java is also amazing with eclipse because you can click in the error log and be taken right to the exact line that caused the error and get a full stack trace showing the whole chain of events. Similar arguments apply to C# as well, but C# is not as good at this stuff, this is why people rave about Eclipse as an IDE. Anyway, this is exactly why C# and Java are popular to newbies, if you would give them a try you'd see they are much easier to learn than GML, and you'll learn proper programming and that knowledge can then be applied to learning C++.

One step at a time. 

Stuff I want to eventually get into:

* Learning more GML / advanced
* Learn how to do things manually such as B2D
* Experimenting with ENIGMA more and building stuff
* Getting to know more about C++
* Going through LGM, and other sources to see how things are done.

I did not do BASIC and ASM overnight :D

Title: Re: How to do conditional branching in C++ ?
Post by: Goombert on April 30, 2014, 12:16:53 am
You to do it all through code, literally all the functions are documented on the Wiki.
http://enigma-dev.org/docs/Wiki/Box2D_Functions

I just built my example game and it worked fine.
https://www.dropbox.com/s/11svwsiwvd31yim/enigmaphysicstest.egm
Title: Re: How to do conditional branching in C++ ?
Post by: Darkstar2 on April 30, 2014, 04:38:30 pm
You to do it all through code, literally all the functions are documented on the Wiki.
http://enigma-dev.org/docs/Wiki/Box2D_Functions

I just built my example game and it worked fine.
https://www.dropbox.com/s/11svwsiwvd31yim/enigmaphysicstest.egm

Not all are documented :P  b2d world update settings are not listed anywhere, only the b2d world update.
Also certain things are not documented right,
and they document each individual commands but don't give an example of what is required step by step COMBINED, so for a new person it is very confusing.  I looked at your example, you also made
some mistakes in the coding, I wonder if at the time you made this there were issues with ENIGMA where you had to compensate by tweaking things.

For example, why did you set room speed to 1000 ?
This causes the smiley square player to go way too fast, so much that it disappears :P  I fixed that by setting room speed down to 30, it did not affect other objects.

Also very confused about the fps/1 and fps/30 and world update settings, that was not documented.
b2d_world_update_settings(global.world, timestep, 8, 3);
b2d_world_update(global.world);

So it should be this:
b2d_world_update_settings(index, timeStep,velocityIterations, positionIterations)

I think you figured out where I found this out ! lol!

Also b2d world update is documented as
b2d_world_update()
shouldn't it be b2d_world_update(id) ?

Your project is quite different than what I want to do so I just removed stuff and isolated stuff and will attempt to see if I can adapt this to what I want to do, but think I have the general idea.

If for example I have a sprite of a ball,
I have to create a body, apply a shape in this case
circle and its radius, then apply a fixture and
set my world (equivalent to the room physics in IDE)
am I correct ?  and apply the world update thingy
in step.  You have to understand that coming from GMStupido I have to relearn because in GMS all that is handled for you :P all you do is set the object shape type and points and add null collisoins to objects.  So once I figure this shit out I will script it.

Thanks for the example though without that I would have been lost :P

Also noticed the balls and boxes when they fall down on the floor they don't bounce, they fall as though they are heavy bowling balls or bricks :D  in normal physics objects would at least have a slight bounce when they touch the floor.  Is that a limitation or a setting ?

Thanks.