Sign in to follow this  
Jonus

Simple C++ problem i can't seem to figure out.

Recommended Posts

if(testTimer.CheckFreq(2000))
{
	if (Gamestate::currentState > 4)
	{
		Gamestate::currentState = 0;
	}
	else
	{
		Gamestate::currentState++;
	}
}		

switch(Gamestate::currentState) {
	case Gamestate::States::title:
		DrawTitle();
		break;

	case Gamestate::States::options:
		DrawOptions();
		break;

	case Gamestate::States::start:
		DrawStart();
		break;		

	case Gamestate::States::game:
		DrawGame();
		break;

	case Gamestate::States::gameover:
		DrawGameOver();
		break;

	default:
		SimpleFont::print(20,50,"CRAP! %i", Gamestate::currentState);
		getchar();
}
The problem is that the default: case of the switch function gets called, but afaik this shouldn't happen. Look at the if case Gamestate::currentState > 4. What do i fail to see? The absurd is that the printfunction prints a "CRAP! 5" on my screen. How can that happen?

Share this post


Link to post
Share on other sites
Instead of having
Gamestate::CurrentState
, why don't you create an instance of Gamestate called Game or something like that, and use
Game.CurrentState
?

That might work - it did for me once.

Hope I've helped,

ukdeveloper.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jonus
*** Source Snippet Removed ***The problem is that the default: case of the switch function gets called, but afaik this shouldn't happen. Look at the if case Gamestate::currentState > 4. What do i fail to see?

The absurd is that the printfunction prints a "CRAP! 5" on my screen. How can that happen?


Hypothetical: Before the if, currentState == 4.

(Gamestate::currentState > 4) == false. It therefore goes in the else, where Gamestate::currentState is incremented to 5.

Share this post


Link to post
Share on other sites
Gamestate is defined/declared static. There shouldn't be a problem.

#ifndef GAMESTATE_H
#define GAMESTATE_H

class Gamestate
{
public:
class States
{
public:
static const int title = 0;
static const int options = 1;
static const int start = 2;
static const int game = 3;
static const int gameover = 4;
};

// basic stats of the player
static int score;
static int lines;
static int level;
static bool gameover;

static int currentState;
};

#endif;


#include "gamestate.h"

// basic stats of the player
int Gamestate::score;
int Gamestate::lines;
int Gamestate::level;
bool Gamestate::gameover;

int Gamestate::currentState = 0;

Share this post


Link to post
Share on other sites
Following my previous post, This:


//Borked condition if the state is currently 4, since it will get incremented to 5 in the else.
if (Gamestate::currentState > 4) {
Gamestate::currentState = 0;
}
else {
Gamestate::currentState++;
}


Should be:


//Fixored, depending on how you want it to be treated for non-standard value, == could be used.
if (Gamestate::currentState >= 4) {
Gamestate::currentState = 0;
}
else {
Gamestate::currentState++;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by xMcBaiNx
Quote:
Original post by Jonus
*** Source Snippet Removed ***The problem is that the default: case of the switch function gets called, but afaik this shouldn't happen. Look at the if case Gamestate::currentState > 4. What do i fail to see?

The absurd is that the printfunction prints a "CRAP! 5" on my screen. How can that happen?


Hypothetical: Before the if, currentState == 4.

(Gamestate::currentState > 4) == false. It therefore goes in the else, where Gamestate::currentState is incremented to 5.
Holy "Crap! 5" batman, that's it! Thanks man. I was looking at it forever.

This is my solution:
if(testTimer.CheckFreq(2000))
{
Gamestate::currentState++;

if (Gamestate::currentState > 4)
{
Gamestate::currentState = 0;
}
}




Another thread that proves my foolishness ... *sigh*

Btw. your solution xMcBaiNx would also work. Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this