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

Author Topic: If Statement Really Aggravating Problem! Grr...  (Read 1732 times)

0 Members and 1 Guest are viewing this topic.

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
If Statement Really Aggravating Problem! Grr...
« on: August 19, 2010, 06:38:29 am »
Okay! I'm going to sound like a total noob. Haha! I wasn't sure if this problem belonged on this forum or the normal C++ forum, so forgive me if this is in the wrong forum but here is my problem.

Code: [Select]
//DECLARATION
bool X = false;
    bool Y = false;
    std::string XCoord;
    std::string YCoord;
    sf::String Coord;
    Coord.SetFont(TypeKnight);
    Coord.Move(175,10);

    sf::String TopLeft("Text Input: ________________");
    TopLeft.SetFont(TypeKnight);
    TopLeft.Move(10,10);
    TopLeft.SetColor(sf::Color::White);

    enum State { State0, State1, State2, State3 };
    State myState = State0;
    int counter = 0;


//EVENT LOOP
            if (myState == State0 && (Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::A))
            {
                std::cout << "ONE" << std::endl;
                myState = State1;
            }
            if (myState == State1 && (Event.Type == sf::Event::TextEntered))
            {
                if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Return))
                {
                    if (X == false)
                        X = true;
                    if (X == true && Y == false)
                        Y = true;
                }
                std::cout << "TWO" << std::endl;

                if (X == false && counter < 5) {
                    if (Event.Text.Unicode > 47 && Event.Text.Unicode < 58)
                    {
                        std::cout << "THREE" << std::endl;
                        XCoord += static_cast<int>(Event.Text.Unicode);
                        Coord.SetText(XCoord);
                        std::cout << XCoord << std::endl;
                    }
                    ++counter;
                }

                if (X == true && Y == false && counter < 5) {
                    if (Event.Text.Unicode > 47 && Event.Text.Unicode < 58)
                    {
                        std::cout << "FOUR" << std::endl;
                        YCoord += static_cast<int>(Event.Text.Unicode);
                        Coord.SetText(YCoord);
                        std::cout << YCoord << std::endl;
                    }
                    ++counter;
                }
            }




//MAIN LOOP
App.Draw(Coord);
                if (X == true)
                    App.Draw(Comma);


What is supposed to happen:
1) The user presses A to activate text input.
2) The user types in the X coordinate and it is displayed
3) The comma is displayed when the user hits enter.
4) The user types in the Y coordinate and it is displayed along side the X coordinate in this fashion: X , Y
5) When the user hits enter, both the X and the Y values are stored on a text file. I haven't put that in yet.
This isn't what happens, obviously...

What does happen though, it displays the X coordinate as I type it. Then if I reach 4 numbers in the coordinate, it doesn't display the comma even if I hit enter. However, if I hit enter when I have 1, 2, or 3 numbers the comma is displayed just fine. Afterwards, it doesn't let me type the Y coordinate and I don't know why.

Please don't say I haven't tried to fix this. I stayed up for 28 hours and never fixed this stupid problem. Then after sleeping I spent another 6 hours trying to fix it again. I know it's probably going to end up being something so stupid.... :D

Thanks
-Wander

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
If Statement Really Aggravating Problem! Grr...
« Reply #1 on: August 19, 2010, 09:37:19 am »
First of all, reading quickly, here is what happens for me :

After Starting
Code: [Select]

X = false
Y = false
myState = State0
counter = 0
cout :
XCoord :


After 'A'
Code: [Select]

X = false
Y = false
myState = State1
counter = 1
cout : ONE TWO
XCoord :

There is a problem I think, counter must not be incremented here, since you are modifying the State and testing the state just after you are entering the second test directly, but it's not a number nor a Return, so you print 'TWO' and increment counter (X==false && counter <5)

After '1'
Code: [Select]

X = false
Y = false
myState = State1
counter = 2
cout : ONE TWO TWO THREE
XCoord : 1


After '2'
Code: [Select]

X = false
Y = false
myState = State1
counter = 3
cout : ONE TWO TWO THREE TWO THREE
XCoord : 12


After '3'
Code: [Select]

X = false
Y = false
myState = State1
counter = 4
cout : ONE TWO TWO THREE TWO THREE TWO THREE
XCoord : 123


After '4'
Code: [Select]

X = false
Y = false
myState = State1
counter = 5
cout : ONE TWO TWO THREE TWO THREE TWO THREE TWO THREE
XCoord : 1234


After 'Return'
Code: [Select]

X = true
Y = true
myState = State1
counter = 6
cout : ONE TWO TWO THREE TWO THREE TWO THREE TWO THREE TWO
XCoord : 1234

There is another problem I think, You are testing X and modifying it (to true), just after you are testing X and Y, passig into this second test directly. Maybe you do something special if X and Y are true ? (like stopping checking keypressed events)

Good luck, it's not easy to understand :)

PS: Do the test in the inverse order of the logic, or return from function after being entered the if statements ;)

EDIT : PPS : Debug step by step too, with your head, a paper and a pencil ;)
Mindiell
----