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

Started by
4 comments, last by Janju 18 years, 9 months ago
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?
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.
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.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
Gamestate is defined/declared static. There shouldn't be a problem.

#ifndef GAMESTATE_H#define GAMESTATE_Hclass 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 playerint Gamestate::score;		int Gamestate::lines;		int Gamestate::level;bool Gamestate::gameover;int Gamestate::currentState = 0;
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++;}
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
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

This topic is closed to new replies.

Advertisement