I'm trying to learn c++ and am trying to use classes. I'm doing something wrong but can't figure this out. Here's the code:

#include <iostream>
using namespace std;

class dead
{
public:

	void dieNow()
	{
	cout<<"You just died!"<<endl;
	cin.get();
	}	
};

class alive
{
public:
	
	void youLive()
	{
	cout<<"Whew! You barely made it!"<<endl;
	cin.get();
	}
};


int main()
{
	int a;
	cout<<"Please enter 1 or 2"<<endl;
	cin.get();
	cin>>a;
	cin.get();

	if (a == 1)
	{
	dead nowDead;
	nowDead.dieNow();

	}
	if (a == 2)
	{
		alive youAlive;
		youAlive.youLive();
	}
	return 0;

}

This one really baffles me because when I do enter 1, it does nothing, but when I enter 2, it gives me what I want. I don't know how to get 1 to work when I type it in.

I've also got a question about learning to program in C++. I'm trying to learn by way of gaming so I can program games as a hobby, that's why I'll post the types of code like I have above. Here's my question if anybody knows: Can one use c++ in order to create something like Halo? Or are there other tools involved in the gaming process?

Thanks!

Recommended Answers

All 6 Replies

lines 31 and 32 are asking for keyboard input twice. Delete one of those lines

Use either DirectX or OpenGL for game engines. Post game questions in the game forum.

You definitely need to rethink your usage of classes - that's not how they were meant to be used. They are supposed to represent (conceptual) object classes and thus the instances should represent certain objects.

But: what is a "dead"? And what is an "alive"?

I know what you are saying. I was only playing around with the classes trying to get them to work and trying to get the concept stuck in my head. I was going to be studying actually usage afterwards.

Ah, I see.
Also, you said you wanted to use case statements - I associate that with switch cases:

switch(a)
{
  case 1:
  {
    dead nowDead;
    nowDead.dieNow();
    break;
  }

  case 2:
  {	
    alive youAlive;
    youAlive.youLive();
  }
}

Sorry about that. Wrong terminology.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.