Pages: 1 2 »
  Print  
Author Topic: How to do conditional branching in C++ ?  (Read 2754 times)
Offline (Unknown gender) Darkstar2
Posted on: April 29, 2014, 05:33:50 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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++ ?

Logged
Offline (Unknown gender) TheExDeus
Reply #1 Posted on: April 29, 2014, 06:25:24 PM

Developer
Joined: Apr 2008
Posts: 1872

View Profile
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.
Logged
Offline (Unknown gender) Darkstar2
Reply #2 Posted on: April 29, 2014, 07:15:35 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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++ ?

Logged
Offline (Male) Goombert
Reply #3 Posted on: April 29, 2014, 08:16:10 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3108

View Profile
Josh uses goto in some places in ENIGMA/JDI, goto also works in ENIGMA as in you can use it in your game.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) Darkstar2
Reply #4 Posted on: April 29, 2014, 08:24:22 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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 ?

Logged
Offline (Male) Goombert
Reply #5 Posted on: April 29, 2014, 08:27:58 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3108

View Profile
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
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) Darkstar2
Reply #6 Posted on: April 29, 2014, 09:38:52 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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
Logged
Offline (Male) Goombert
Reply #7 Posted on: April 29, 2014, 09:42:17 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3108

View Profile
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
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) Darkstar2
Reply #8 Posted on: April 29, 2014, 10:10:08 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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?
Logged
Offline (Male) Goombert
Reply #9 Posted on: April 29, 2014, 10:18:15 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3108

View Profile
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.


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.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) Darkstar2
Reply #10 Posted on: April 29, 2014, 10:29:33 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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

« Last Edit: April 29, 2014, 10:31:57 PM by Darkstar2 » Logged
Offline (Male) Goombert
Reply #11 Posted on: April 29, 2014, 10:39:05 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3108

View Profile
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.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) Darkstar2
Reply #12 Posted on: April 29, 2014, 10:41:41 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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
Logged
Offline (Unknown gender) Darkstar2
Reply #13 Posted on: April 29, 2014, 10:43:18 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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.?
Logged
Offline (Male) Goombert
Reply #14 Posted on: April 29, 2014, 10:45:34 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3108

View Profile
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
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Pages: 1 2 »
  Print