Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Lethn's Programming Questions Thread  (Read 59185 times)

0 Members and 3 Guests are viewing this topic.

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Lethn's Programming Questions Thread
« Reply #45 on: July 20, 2013, 12:23:51 am »
OKAY, so it's good to hear you are reading through a c++ book but sad to see you not comprehending some of the basics.

Integers are numbers.  whole numbers, real numbers pure and simple.  type int can hold a number that ranges from –2,147,483,648 to 2,147,483,647, unsigned int can hold a number 0 to 4,294,967,295 typically.  There is nothing magical or hard to understand about integers, they are numbers, you can perform arithmetic with them, addition, subtraction, multiplication, division, modulus.

You are on the right track kinda with your loop statement.  The thing you don't seem to realize is you already have a loop running.  your while(window.open()) { ... } is a loop.  So to give you a working example of how to add 1 to your gold amount each iteration through your main program loop and print it to the screen I give you this.

#include <iostream>
#include <SFML/Graphics.hpp>
#include <sstream>

int main()
{

    sf::RenderWindow window(sf::VideoMode(800, 600), "C++ Programming Test");

    sf::Font arial;
    if ( !arial.loadFromFile ( "arial.ttf" ) )
    { }

    int goldamount = 0;

    std::ostringstream myStream;
    myStream << "You have " << goldamount << " Gold";



    sf::Text goldtext;
    goldtext.setFont ( arial );
    goldtext.setCharacterSize ( 12 );
    goldtext.setColor ( sf::Color::Yellow );
    goldtext.setPosition ( 20, 20 );

    goldtext.setString ( myStream.str () );

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw ( goldtext );
        window.display();

        goldamount++; //adds 1 to our goldAmount integer
        myStream.str(""); //clears the string stream, correct way should be myStream.str(std::string())
        myStream << "You have " << goldamount << " gold"; //restuff the stream
        goldtext.setString(myStream.str()); //get string from the stream and set to text
    }
    return 0;
}

 

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #46 on: July 20, 2013, 12:25:00 pm »
Okay that makes more sense now I think and I'm seeing where I went wrong, a lot of the solutions I saw seemed far too complicated for what I was trying to achieve. So if I'm right, the blank quotation marks clear the numbers and then you simply repeat the original function after to add the number?

It looks to me like this function would only work once, what if I wanted it to increase in multiples of ten or something? I keep thinking I'd have to use something like symbolic constants or floating point types but I wouldn't know the proper syntax in SFML, may just need to experiment with this example and see what I can come up with :D

« Last Edit: July 20, 2013, 01:06:53 pm by Lethn »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Lethn's Programming Questions Thread
« Reply #47 on: July 20, 2013, 01:23:55 pm »
for increasing by another amount, say 10 would look like
goldamount = goldamount + 10;
or
goldamount += 10;
for a shorter version.

Symbolic constraints?  floating point types?  Like seriously you are overthinking a simple concept as numeric arithmetic.  Floating point is also just the fancy for NUMBERS WITH A DECIMAL POINT.

And why would you think the addition to goldamount would only work once?  cuz its only written once?  Look where it's written, its in the main while() loop of your program.  And what does a while() loop do?  It LOOPS and runs all the code written inside of it over and over and over and over and over and over and over again until its termination requirement has been met.  The program thread doesn't just decide to stop at the end of the loop and wait, it just goes and goes.

If the simple concept of a control structure like a while() loop and basic numerical arithmetic are beyond your current scope of understanding maybe you need to find a different book. If you've been truthful about reading through a c++ beginners book then these concepts should have been covered in like the first 2 chapters.  Most books i've seen take you through user input/output, simple data types and manipulating them and control structures like while() for() do() almost always in the first couple chapters.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #48 on: July 20, 2013, 02:15:23 pm »
wow I have been overcomplicating it thanks, the interesting thing is I've seen these kind of methods being used but they're all incredibly complicated for what they're trying to do, I'm going to have read through of those loop sections again, thanks for the explanation, I think I'll have a fairly easy time of all of this now.

As for basic mathematics, it's always been a problem for me because of crappy teaching though I've noticed that seems to be a common trend in schools now to not teach it properly. I'm going to experiment and see what I can come up with for some reason I get it better when I see it all working.

Edit: Yep! I read through the section again and I get it now, I'll post up the code again when I have it all working.
« Last Edit: July 20, 2013, 02:25:01 pm by Lethn »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #49 on: July 20, 2013, 02:37:55 pm »
Oh that's awesome! I've got the incrementing working brilliantly now thanks to the example you gave hatchet, I just need to stop the while loop from interfering with the window event! Been studying for ages for this :D
« Last Edit: July 20, 2013, 02:47:46 pm by Lethn »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Lethn's Programming Questions Thread
« Reply #50 on: July 20, 2013, 02:53:11 pm »
Oh that's awesome! I've got the incrementing working brilliantly now thanks to the example you gave hatchet, I just need to stop the while loop from interfering with the window event! Been studying for ages for this :D

Can you clarify what you mean by this?  The while loop doesn't interfere with getting window events, in fact if you didn't have the event loop inside the main game loop you wouldn't be able to process any events at all.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #51 on: July 20, 2013, 03:04:37 pm »
Hmm, probably overthinking again, what I'm trying to do now is get a basic while loop up and running but for some reason when I enter it in like this the program freezes, I've sometimes got it up and running with the text showing but then it just freezes after that as well. I tried putting in the while loop inside the other while loops etc. but it always gave the same results.

The example you gave me worked fine when it was just the int goldamount ++; by itself

#include <Iostream>
#include <SFML/Graphics.hpp>
#include <sstream>

int main()
{

    sf::RenderWindow window(sf::VideoMode(800, 600), "");

    sf::Font arial;
    if ( !arial.loadFromFile ( "arial.ttf" ) )
    { }

    int goldamount = 0;

    std::ostringstream gld;
    gld << "You have " << goldamount << " Gold";





    sf::Text goldtext;
    goldtext.setFont ( arial );
    goldtext.setCharacterSize ( 12 );
    goldtext.setColor ( sf::Color::Yellow );
    goldtext.setPosition ( 20, 20 );

    goldtext.setString ( gld.str () );

    while (window.isOpen())
    {

        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw ( goldtext );
        window.display();

        while ( goldamount + 5 ); /* the while loop that causes the window to freeze */
        gld.str ( " " );
        gld << "You have " << goldamount << " gold";
        goldamount ++;
        goldtext.setString ( gld.str() );

    }
    return 0;
}


 
« Last Edit: July 20, 2013, 03:13:51 pm by Lethn »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #52 on: July 20, 2013, 03:33:07 pm »
What exactly are you trying to achieve with
while ( goldamount + 5 );
? This is an infinite loop with no body.

Are you sure you know the C++ basics? It really doesn't look so, and you should finally realize that :-\
« Last Edit: July 20, 2013, 03:39:30 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #53 on: July 20, 2013, 03:38:58 pm »
I'm reading through this >_< :D I'll get it eventually, I just need to make sure I put the right stuff in the body don't I?

Quote

The while loop is a for loop stripped of the initialization and update parts; it has just a
test condition and a body:
while (test-condition)
body

First, a program evaluates the parenthesized test-condition expression. If the expression
evaluates to true, the program executes the statement(s) in the body.As with a for
loop, the body consists of a single statement or a block defined by paired braces. After it
finishes with the body, the program returns to the test condition and re-evaluates it. If the
condition is nonzero, the program executes the body again.This cycle of testing and execution
continues until the test condition evaluates to false (see Figure 5.3).

Clearly, if you want the loop to terminate eventually, something within the loop body must do something to affect the test-condition expression. For example, the loop can increment
a variable used in the test condition or read a new value from keyboard input. Like the
for loop, the while loop is an entry-condition loop.Thus, if test-condition evaluates to
false at the beginning, the program never executes the body of the loop.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #54 on: July 20, 2013, 03:42:49 pm »
The problem is, you're wasting a massive amount of time with your approach. You are constantly experimenting with language features and wondering about why something works or fails. And it doesn't help to read up on single language features like the for loop; your next problem is certain to show up soon.

You need an understanding of how the features work together. I really suggest to read the book about C++ thoroughly. If you invested the time you're wasting with try&error into reading (and understanding what's written), you would progress much faster. And you would actually learn things for the future, a lot of topics cannot be learned by doing.

We have already said this multiple times now -- I really hope you realize that you're lacking basic knowledge which you cannot simply learn by asking forum questions and reading internet tutorials. This thread is going nowhere.
« Last Edit: July 20, 2013, 03:47:58 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Lethn's Programming Questions Thread
« Reply #55 on: July 20, 2013, 03:55:14 pm »
^ What nexus said.  If you want some help outside of your reading material PM me and I can try and help.  I have a super slow boring job so I got plenty of time on my hands.

And no, you don't need to just 'put something in the body' of your 'while ( goldamount + 5 );'  You have the test condition completely wrong which is why its an infinite loop.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #56 on: July 21, 2013, 05:55:02 pm »
I think you guys are suffering from the same thing the writer of this book is suffering and that's the problem of writing out vocabulary and words and expecting someone completely new to understand it all immediately so I pretty much have no choice but to resort to trial and error because that's the only way I'll be able to learn.

The examples are fine and I'm sure I'd understand the explanations if they were written in English but it pisses me off when time and time again I come across tutorials/books/people who use vocabulary and don't even explain to beginners what they mean.

Here's a perfect example of what I'm talking about, the book did a great job of showing me the absolute basics, explaining that the ";" was something you used to end the code etc. but as things have gone on it forgets about explaining this kind of thing entirely or just doesn't bother.

I'm surprised you didn't spot it to be honest because I'm still having trouble understanding that quote above but it rants on about test-conditions and refuses to explain to me what it actually means so I can't implement the code properly. This is something that a lot of professionals do and it's incredibly annoying and makes me wonder just whether they actually understand the things they've been taught which is why I keep asking so much about it. It isn't anything to do with not reading the book like you've immediately jumped to, it's the exact opposite, I've been trying to find a proper C++/programming glossary out there and I haven't really found anything yet.

On the bright side, I can get while loops and increments running separately just fine but for some reason ( and I suspect it's going to involve programming vocabulary again ) they're not really doing anything when I put them together.

#include <Iostream>
#include <SFML/Graphics.hpp>
#include <sstream>

int main()
{

    sf::RenderWindow window(sf::VideoMode(800, 600), "");

    sf::Font arial;
    if ( !arial.loadFromFile ( "arial.ttf" ) )
    { }

    int goldamount = 0;



    std::ostringstream gld;
    gld << "You have " << goldamount << " Gold";





    sf::Text goldtext;
    goldtext.setFont ( arial );
    goldtext.setCharacterSize ( 12 );
    goldtext.setColor ( sf::Color::Yellow );
    goldtext.setPosition ( 20, 20 );

    goldtext.setString ( gld.str () );

    while (window.isOpen())
    {

        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw ( goldtext );
        window.display();

        while ( goldamount > 5 )
        {
        gld.str ( " " );
        gld << "You have " << goldamount << " gold";
        goldamount ++;
        goldtext.setString ( gld.str() );
        }


    }
    return 0;
}


 
« Last Edit: July 21, 2013, 06:09:03 pm by Lethn »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Lethn's Programming Questions Thread
« Reply #57 on: July 21, 2013, 07:07:12 pm »
Sent you a PM about vocabulary stuffs.

As far as why your while() loop isn't doing anything lets look at it:
while ( goldamount > 5 )
{
     //do stuff
}
 
Your program thread will never ever enter this loop since the program first goes "Is goldamount greater than 5?"  since goldamount starts at 0 and you never add any to it outside of this loop the answer is "No, goldamount is not greater than 5 (this equates to FALSE)"  And since the test-condition returns as FALSE the thread never enters the loop. 

If you were to set goldamount to 6 or greater before the thread gets to this loop the test condition would evaluate to TRUE and then it would enter the loop and it would stay in the loop forever since goldamount would always be greater than 5.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #58 on: July 21, 2013, 07:40:52 pm »
The examples are fine and I'm sure I'd understand the explanations if they were written in English but it pisses me off when time and time again I come across tutorials/books/people who use vocabulary and don't even explain to beginners what they mean.
But then you have chosen a bad book, good books explain their vocabulary. Unfortunately, there is a mass of bad literature about C++, because as you assume, a lot of authors haven't completely understood the matter themselves. Which one do you read?

Here's a perfect example of what I'm talking about, the book did a great job of showing me the absolute basics, explaining that the ";" was something you used to end the code etc. but as things have gone on it forgets about explaining this kind of thing entirely or just doesn't bother.
What does it forget? Of course books don't explain the same things again and again, but I don't think that's what you mean.

I think you guys are suffering from the same thing the writer of this book is suffering and that's the problem of writing out vocabulary and words and expecting someone completely new to understand it all immediately
This may be true to some extent, but consider that we've all had the same problems as you currently have. We had to find a way to inform ourselves about vocabulary and things we didn't understand; being able to get informations is one of the most important aspects of programming. But I'm also aware that some books teach really badly, I have seen this when learning C++ myself. It would be nice if you could avoid that by directly knowing which books are worth reading.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #59 on: July 21, 2013, 08:55:32 pm »
Thanks for the help guys, I do appreciate it, also hatchet, you're right, before you posted I accidentally got it working and the function now works when I do more than 5. I think I've been misunderstanding how to use incrementing numbers so I'll have to have a look at more examples and explanations of it. If it helps, I've got a screenshot of the kind of integer thing I'm trying to achieve. The increment by itself works brilliantly but I'm going to have to look at how to change the way they work because once I do this I can get a basic game up and running so it looks like I need to go and do more research and find different books.

Take a look at the integer on the top left, that's the kind of thing I'm thinking of.

http://i1-news.softpedia-static.com/images/reviews/large/startopia_006-large.jpg

If you're wondering Nexus, I'm reading through C++ Primer Plus, 6th Edition, I think I remember someone mentioning not to read beyond a certain edition? I think I've just understood why :P
« Last Edit: July 21, 2013, 08:58:06 pm by Lethn »