Jump to content
  • Advertisement
Sign in to follow this  
Jonus

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

This topic is 4715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!