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

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

## 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 on other sites
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 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 on other sites
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;

##### 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 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