*HELP* C++ Console Game

Can you tell me what is wrong with this, making myself a game...sorry for lack of detail, i just had to get this up quick. It will run but none of the content will show and will shutdown after a pressed key like nothing is there

#include <iostream>

using namespace std;

int main()
{
string input;
int hp=30;
int currentHp;
int currentHp2;
int finalHp;
int health=30;
int finalHealth;

while (health<1 || finalHealth<1){
cout << "Sorry, you have died." <<endl;

while (hp<1 || currentHp<1 || currentHp2<1 || finalHp<1){
cout << "Sorry, you have run out of energy." <<endl;

cout << "Your Health:" << health << endl;
cout << "Your HP:" << hp << endl;
cout << "you see a man, would you like to kill him?\n1. Yes\n2. No" <<endl;
cin >> input;
if (input=="yes" || input == "Yes") {
currentHp=hp-5;
cout << "You killed him! It used -5 HP, You now have:" << currentHp << health << endl;
cout << "A witness has called 911, what would you like to do?\n1. Kill witness while he is on the phone\n2. Run Away\n3. Wait and shoot it out with the cops" <<endl;
cin >> input;
if (input== "Kill witness while he is on the phone" || input== "Kill witness while he is on the phone"){
currentHp2=currentHp-5;
cout << "You are insane! you murderer! You now have:" << currentHp2 << health <<endl;
if (input== "Run Away" || input== "Run Away"){
finalHp=currentHp2-15;
cout << "You have successfully run away! Good Job, You Win!You finished with this:" << finalHp << health <<endl;

if (input== "Wait and shoot it out with the cops" || input== "Wait and shoot it out with the cops"){
health= health-30;

}
}
}
}
}

}
if (input == "no" || input == "no") {
cout << "You decided otherwise, you are a good person...";
}
}



Last edited on
Where to begin. First you didn't use code tags so I had to copy and format the code to see what you did.

Now as for the code, I am surprised it compiled because your code has a string data type but no string header so it shouldn't have compiled. Your while loops are never executing because you are saying While the health is less than 1 or the energy is less than 1 but you have it set higher than that so you never even go into the while loop.

Using your code you have health set to 30 and finalhealth is uninitialized so it is some random data in memory. So your first while loop is actually the equivalent of while (30 < 1 || +/-<random data> < 1){} which will come back as false on both.

I would have done it this way.... not pretty, but....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>

using namespace std;

int main()
{
	char answer;
	int selection;
	int ep = 30;
	int finalEp = ep;
	int currentEP = ep;
	int health = 30;
	int finalHealth = health;

	cout << "Your Health: " << health << endl;
	cout << "Your EP: " << ep << endl; // energy points since hp is hit points or health points
	cout << "You see a man, would you like to kill him?\n\t1. [Y]es\n\t2. [N]o\n\t3. [Q]uit" << endl;
	cin >> answer;
	
	while (answer != 'q' || answer != 'q'){
		if(answer == 'Y' || answer == 'y'){
			currentEP -= 5;
			cout << "You killed him! It use -5 EP, You now have : " << currentEP << " EP " << health << endl;
			cout << "A witness has called 911, what would you like to do?\n[1]. Kill witness while he is on the phone\n[2]. Run away\n[3].Wait and shoot it out with the cops" << endl;
			cin >> selection;
			if (selection == 1){
				currentEP -= 5;
				cout << "You are insane! You Murderer! You now have: " << currentEP << " EP " << health << endl;
			}
			if (selection == 2){
				finalEp = currentEP - 15;
				cout << "You have successfully run away! Good job, you win! You finished with this: " << finalEp << " EP " << health << endl;
			}
			if (selection == 3){
				finalHealth = 0;  // you loose all your health
				cout << "You died...";
				answer = 'q'; // might as well quit since we have no health
			}
			else if (finalEp == 0){ 
				answer = 'q';
				cout << "You ran out of energy..."; 
			} // migh as well quit since we have no energy  	
			else{
				cout << "Your Health: " << health << endl;
				cout << "Your EP: " << ep << endl; // energy points since hp is hit points or health points
				cout << "You see a man, would you like to kill him?\n\t1. [Y]es\n\t2. [N]o\n\t3. [Q]uit" << endl;
				cin >> answer;
			}
		}
	}
	
	if(answer == 'n' || answer == 'N'){
		cout << "You decide otherwise, you are a good person...";
	}
	else if (answer == 'q' || answer == 'Q'){
		cout << "That's how it goes...";
	}
	
	return 0;
}
	
BHXSpecter wrote:
Now as for the code, I am surprised it compiled because your code has a string data type but no string header so it shouldn't have compiled.
Actually his code will compile just fine. You can use string objects if the iostream header is included.

If your loop ever hit true then you would enter an infinite loop. Be careful.
Avillius wrote:
Actually his code will compile just fine. You can use string objects if the iostream header is included.

Yes, but as a programmer you should never assume it is included. You should implicitly explicitly include it.
Avillius wrote:

If your loop ever hit true then you would enter an infinite loop. Be careful.

The OPs code, it did do an infinite loop after I got the code formatted so I could see what was going on. My code will only go into an infinite loop if I remove the lines setting answer to 'q' or 'Q' and the prompt to ask the question again.
Last edited on
BHX wrote:
You should implicitly include it.

Explicitly?
Oops, didn't even catch I typed implicitly. Yes, explicitly include string. Sorry, I shouldn't have replied while running my son's medicines because I was focused on that rather than what I was typing.
Avilius wrote:
Actually his code will compile just fine. You can use string objects if the iostream header is included.

Portable code cannot make this assumption. If you wish to use std::string you must include the appropriate header as the standard doesn't require <iostream> to include <string>, although it may on some implementations.

It does not, for instance, in VC++.
Last edited on
Portable code cannot make this assumption. If you wish to use std::string you must include the appropriate header as the standard doesn't require <iostream> to include <string>, although it may on some implementations.

It does not, for instance, in VC++.
I can see what you mean. But since this is beginner-ish level code, this really shouldn't matter much. If it was larger, I'd say the same.
Avilius wrote:
I can see what you mean. But since this is beginner-ish level code, this really shouldn't matter much. If it was larger, I'd say the same.

I'm sure every programmer here just hit their monitors. That is a terrible argument for not including string header file. That is the kind of attitude that leads to bad coding habits later.
I'm sure every programmer here just hit their monitors. That is a terrible argument for not including string header file. That is the kind of attitude that leads to bad coding habits later.
It isn't anything serious; just a simple beginner program. Of course you wouldn't do this with a real product, but this fine for just a simple H.W. assignment or test.

Similar to using system();
Avilius wrote:
It isn't anything serious; just a simple beginner program. Of course you wouldn't do this with a real product, but this fine for just a simple H.W. assignment or test.

Similar to using system();

It is serious. Beginner program or not, you have to stop the bad habits and bad practices fast or they will continue to use them thinking they are fine to do so. It concerns me when any programmer says it is fine for a beginner to use a bad habit or practice. Not to mention, as cire pointed out, it breaks the portability of your code.
Last edited on
Are you seriously going to start an argument based on this? This is most likely some sort of prototype. I'm almost 100 percent certain that this code isn't going to be ported to another platform. It is beginner code. It's on the same level of importance of a homework assignment. Can you chill out just a bit?

Starting an argument over something as silly as this is just childish.
Avilius wrote:
Are you seriously going to start an argument based on this? This is most likely some sort of prototype. I'm almost 100 percent certain that this code isn't going to be ported to another platform. It is beginner code. It's on the same level of importance of a homework assignment. Can you chill out just a bit?

Starting an argument over something as silly as this is just childish.

I wouldn't call it an argument. I'm just stating what I have learned in the time I've been programming and helping others. That reply just justified my concerns with your outlook though.

[EDIT] Now you make utterly no sense. Telling someone it is no big deal to not explicitly include <string> but in the ATM Machine thread you are making note of not requiring a semicolon at the end of a function definition (which is even more minor than not including headers).
Last edited on
I would've preferred to make this post a PM, but since you apparently don't care for them (despite having enabled them) when they contain something that isn't complimentary to you, I shall just post it in this thread. No response is required.

Yes. He made a note of something in another thread, just as I made a note of the portability concerns here. That was enough said.

If you wish to continue to draw out trivial arguments (and you are definitely being argumentative and confrontational,) please limit yourself to posting in the lounge where you and the other bunnies can have your 24 pages of bullshit threads. If you wish for the community to discuss the merits of your posts here, please make a thread in the lounge. If you wish to respond to me personally, use a PM.
Fair enough. You can let members continue to choose and pick which bad habits they promote. Worse that can happen is it continues to give the site bad views for promoting bad habits. Google search revealed reviews as late as February of 2013 saying this site promotes bad habits.
[sarcasm]Yes, because if it's on the internet it MUST be true.[/sarcasm]

Since arguing with you wont help the OP in any way, I'm going to simply ignore you for the rest of the thread. This is my last reply to you for the duration of this thread.

By the way. Your source :
BHXSpecter wrote:
late as February of 2013 saying this site promotes bad habits.

is irrelevant (close to a year old). Try giving better sources before you start to state your 'facts'.

BHXSpecter wrote:

I wouldn't call it an argument. I'm just stating what I have learned in the time I've been programming and helping others. That reply just justified my concerns with your outlook though.
BHXSpecter wrote:
That is a terrible argument


-----------------------------------

@OP:

while (hp<1 || currentHp<1 || currentHp2<1 || finalHp<1)

Take a look at this. the variable 'finalHP' isn't initialized, so it's of undefined value. It can literally be anything between the limits of it's datatype. Try setting the variable to a set value.
Last edited on
The problem with relying on <iostream> to provide the definition of std::string (it is actually required to do so by the standard, although in a rather obscure way) is that it is not required, and usually does not provide the definitions of anything non-member from <string>, including std::getline or operator== for strings.
I've observed beginners at other forums who were convinced that strings cannot be compared with operator== (and must use string::compare) because it never compiled for them because they never included <string>
@Avilius

There's a difference between "argument" and "argument". In the first one it's meant to say a discussion while in the 2nd it's there to say opinion, reason, etc.

I for one am on BXH's side since it's terrible to give incentive on bad coding habits no matter on how simple and beginer-ish the program is. The easiest way to learn is starting to do it right at the begining.
Topic archived. No new replies allowed.