0

Hey so I'm trying to read from a file and basically create a cool star map. I am totally a newb, and this is my first time playing with SFML. I am working this on CLion.

I have used the Debugger and can see that everything is being properly put into it's place, but when I run the program the Window just ends up freezing not drawing anything. I was unsure if it was my loop that is the problem or there is something else at play. I didn't think I needed to use a vector since Vector2f seems to suffice ...Any help is greatly appreciated!!!

    CircleShape star;
    star.setRadius(2);
    while(!fileIn.eof()){

        fileIn >> xPixel >> yPixel >> brightStar;
        star.setPosition( Vector2f(xPixel, yPixel) );
        window.draw(star);

    }
    fileIn.close();

Draw Window just has the wheel of death and is "not responding"

  • 1
    Make the program a [mcve]. – Ted Lyngmo Nov 06 '19 at 05:34
  • 1
    Recommendation: replace `while(!fileIn.eof()) { fileIn >> xPixel >> yPixel >> brightStar;` with `while(fileIn >> xPixel >> yPixel >> brightStar) { `. [Explanation here](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). No clue if that will help you at all, but it's the only bug I see in the code provided. Please edit your way into a [mcve]. DO the [mcve] right and you'll probably solve the problem without any assistance. – user4581301 Nov 06 '19 at 05:35

1 Answers1

0

Oh now I put this in...

  CircleShape star;
    star.setRadius(2);
    while(!fileIn.eof()){

        fileIn >> xPixel >> yPixel >> brightStar;
        star.setPosition( Vector2f(xPixel, yPixel) );
        window.draw(star);
        CircleShape star;
        star.setRadius(2);
    }
    fileIn.close();

Star Map showed for a second then went away

Got it Yay:

    CircleShape star;
    star.setRadius(2);
    while(!fileIn.eof()){
        {
        fileIn >> xPixel >> yPixel >> brightStar;
        star.setPosition( Vector2f(xPixel, yPixel) );
        window.draw(star);
        CircleShape star;
        star.setRadius(2);}
        window.display();
    }
    fileIn.close();
  • Make edits to your original question to clarify it if you need to. Don't add proposed answers to the question - unless you have an actual answer. – Ted Lyngmo Nov 06 '19 at 05:33
  • I recommend editing this extra info into the question. It doesn't make for a particularly good answer – user4581301 Nov 06 '19 at 05:33
  • 1
    It looks like you missed the recommendation made by @user4581301 regarding using `while(!fileIn.eof())`. – Ted Lyngmo Nov 06 '19 at 05:41
  • Now that this does answer the question, I recommend making the need for `window.display();` brutally obvious. Mark it as an addition with a comment and briefly explain why it's needed (it'll be a really, really brief explanation). Definitely fix the `while(!eof)` bug. It's such a common and simple-to-fix mistake that it should never show up in an answer. Please notify me with an @ when you make the fix so I can upvote. – user4581301 Nov 06 '19 at 05:46