SFML Troubles

May 31, 2016 at 2:08pm
Can someone please explain why this wont work? I'm trying to make it so when I type on my keyboard, it displays the text on the window.

I watched a tutorial on this, followed it to the T several times and it refuses to work.

Error I'm getting is "Window" was not declared in this scope... I keep getting errors like this everytime I move one or fix something. The current error is on line 34. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <SFML/Graphics.hpp>
#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

int main()
{
    sf::Event event;
    sf::Font font;

    if (!font.loadFromFile("arialbd.ttf"))
    {
        cout << "Could not load the font";
    }
    sf::RenderWindow window(sf::VideoMode(800, 800), "Getting Text On The Screen");

      sf::String sentence;
        sf::Text text(sentence, font, 40);
        text.setColor(sf::Color(44, 127, 255));

    text.setFont(font);

        text.setString("What is your name?");
        text.setCharacterSize(24);
        text.setColor(sf::Color::Blue);



    while(window.isOpen())
    {
        sf::Event Event;
        while(Window.pollEvent(Event))
        {
            switch(Event.type)
            {
            case sf::Event::Closed:
                Window.close();
                break;
            case sf::Event::KeyPressed:
                if(Event.key.code == sf::Keyboard::Escape)
                    Window.close();
                break;
            case sf::Event::TextEntered:
                if(Event.text.unicode >= 32 &&Event.text.unicode <= 126)
                    sentence += (char)Event.text.unicode;
                else if(Event.text.unicode == 8 && sentence.getSize() > 0)
                    sentence.erase(sentence.getSize() - 1, sentence.getSize());
                text.setString(sentence);
                break;
            }
        }

        window.clear(sf::Color::Black);
        window.draw(text);
        window.draw(text);
        window.display();
    }
}
Last edited on May 31, 2016 at 2:47pm
May 31, 2016 at 2:53pm
On line 17 you create an object named window.
On line 34 you try to use an object named Window.

window
Window

window
Window

window
Window
May 31, 2016 at 3:00pm
Thanks Moschops! This is why I love learning programming.

It works like a charm now. I now know to watch for the tinniest of mistakes.
Topic archived. No new replies allowed.