Need help with (if,else) statements and Hello peeps!!!

Hi guys,first I would like to say "Hello Cpp Forum!";
I am a 44 year old boxing coach,that his friend enlisted in c++ course...hoping that it would regenerate my beat up brains!
So here I am,newly emersed in study after 20 years of not openning a book of any kind (except LOTR series)!
I must say that I am enjoing my journey so far and I hope I will learn this wonderful and complex language before I die!
Pardon my poor grammar,I am not a native speaker!

this is my question!
I wrote some silly adventure game,but I am already stuck...
when you enter steps > 10;
you should fall into the pit and the function should end,but instead the "If"
stement follows?!
Thanx for the replies and sorry for the long post!
this is the code;



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
  #include <iostream>
int main()
{

    int steps;
    int dmg;
    int hp1 = 250;
   
    std::cout << "Welcome to Horror House!\n";
    std::cout << "Step into the dark...how many steps will you dare take?\n";
    std::cin >> steps;
    
    if(steps <= 10){
	std::cout << "You encoutered a Troll!!!\n" << "click some damage: \n";
	std::cin >> dmg;
		}
    else{
	std::cout << "You fell into the pit\n" << "GAME OVER!!!\n";
		}
	if(dmg >= hp1){
	std::cout << "You killed the Troll!!!\n";	
		}
	else{
	std::cout << "You have been killed\n" << "GAME OVER!!!\n";	
		}	
	
	return 0;
}
Last edited on
Your program works fine the only problem is that you wanted the game to end so therefore you needed to return 0 so that the program knows to end. Without that return the program will continue to run the other statements until it reaches return 0 at the end of the program.

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
#include <iostream>
int main()
{

    int steps;
    int dmg;
    int hp1 = 250;
   
    std::cout << "Welcome to Horror House!\n";
    std::cout << "Step into the dark...how many steps will you dare take?\n";
    std::cin >> steps;
    
    if(steps <= 10)
    {
	std::cout << "You encoutered a Troll!!!\n" << "click some damage: \n";
	std::cin >> dmg;
		}
    else
    {
	std::cout << "You fell into the pit\n" << "GAME OVER!!!\n";
	return 0; //ends program if in the pit
		}
	if(dmg >= hp1){
	std::cout << "You killed the Troll!!!\n";	
		}
	else{
	std::cout << "You have been killed\n" << "GAME OVER!!!\n";	
		}	
	
	return 0;
}
Last edited on
This is what you originally had:
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
#include <iostream>
int main()
{

  int steps;
  int dmg;
  int hp1 = 250;
 
  std::cout << "Welcome to Horror House!\n";
  std::cout << "Step into the dark...how many steps will you dare take?\n";
  std::cin >> steps;
  
  if(steps <= 10) {
    std::cout << "You encoutered a Troll!!!\n" << "click some damage: \n";
    std::cin >> dmg;
  }
  else {
    std::cout << "You fell into the pit\n" << "GAME OVER!!!\n";
  }


  /* This below should be nested inside the if block above, since it would only
   * make sense to mention a troll if we encounter one */
  if(dmg >= hp1) {
    std::cout << "You killed the Troll!!!\n"; 
  }
  else{
    std::cout << "You have been killed\n" << "GAME OVER!!!\n";  
  } 
  
  return 0;
}



I think this is what you're trying to aim for
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
#include <iostream>

using namespace std;

int main()
{
    int steps;
    int dmg;
    int hp1 = 250;
   
    std::cout << "Welcome to Horror House!\n";
    std::cout << "Step into the dark...how many steps will you dare take?\n";
    std::cin >> steps;
    
    /* We've taken 10 steps and have encountered a troll! */
    if(steps <= 10) {
      std::cout << "You encoutered a Troll!!!\n" << "click some damage: \n";
      std::cin >> dmg;
      if(dmg >= hp1) {
          std::cout << "You killed the Troll!!!\n"; 
      }
      else {
          std::cout << "You have been killed\n" << "GAME OVER!!!\n";  
      } 
    }
    /* We've taken too many steps and fell into the pit! */
    else {
      std::cout << "You fell into the pit\n" << "GAME OVER!!!\n";
    }
    return 0;
}


If main isn't specified with a return 0, it will automatically return 0 when it has reached the end of its block.
You guys Rock!!!
Thank you so much nicholasjb1996 and fiji885!!
Now back to the "drawing board" I need to analyse everything!
this forum rulz ,so glad I have registered...thanx again you guys and thank you cpp forum for having me!!

edit;

@fiji885
I`v got a compiler error saying that "else" statement must start with the "if" statement!

@nicholasjb1996
it ends fine if I fall into the pit, but it doesn`t continue when I encounter a Troll! It all ends after "return 0"!

edit2;

found a solution,with your help guys,thank you so much!!! (please, feel free to comment anything)

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

using namespace std;

int main()
{
    int steps;
    int dmg;
    int hp1 = 250;
   
    std::cout << "Welcome to Horror House!\n";
    std::cout << "Step into the dark...how many steps will you dare take?\n";
    std::cin >> steps;
    
    if(steps <= 10) {
      std::cout << "You encoutered a Troll!!!\n" << "click some damage: \n";
      std::cin >> dmg;
      if(dmg >= hp1) {
          std::cout << "You killed the Troll!!!\n"; 
      }
      else {
          std::cout << "You have been killed\n" << "GAME OVER!!!\n";  
      } 
    }
     if(steps > 10) {
      std::cout << "You fell into the pit\n" << "GAME OVER!!!\n";
    }
    return 0;
}


now I will try to add some new things,will get back to you! You have been a great help!!
any kind of advice or help in the future will be appreciated...Thanx a bunch!!!!

tried to play some more and man,now it reall gets messed up!
how the he** can I end statements properly in different scenarios like this one,
I tried to follow c++ statement rules,but I for sure doing something wrong!?
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
#include <iostream>

using namespace std;

int main()
{
    int steps;
    int dmg;
    int hp1 = 250;
    int hp2 = 150;
   
    std::cout << "Welcome to Horror House!\n";
    std::cout << "Step into the dark...how many steps will you dare take?\n";
    std::cin >> steps;
    
    if(steps <= 10) {
      std::cout << "You encoutered a Troll!!!\n" << "click some damage: \n";
      std::cin >> dmg;
      if(dmg >= hp1) {
          std::cout << "You busted Troll`s head open!!!\n"; 
          std::cout << "continue in the dark,click some Steps: \n";
          std::cin >> steps;
      }
      else {
          std::cout << "You have been killed\n" << "GAME OVER!!!\n";  
      } 
      if(steps >= 5){
		  std::cout << "You fell deeeeeeep.....\n" << "GAME OVER!!!\n";
    }
      else{
		  std::cout << "Giant SPIDER!!!\n" << "click some damage: \n";
		  std::cin >> dmg;
	  }
	  if(dmg >= hp2){
		  std::cout << "SPIDER KILLED!!\n";
	  }
	  else{
		  std::cout << "you have been killed by a SPIDER!!!\n";
	  }
     if(steps > 10) {
      std::cout << "You fell into the pit\n" << "GAME OVER!!!\n";
    }
    return 0;
}}
Last edited on
Topic archived. No new replies allowed.