condition continuation question

Hi guys,can someone help me with this code?
I don`t understand why,after I`ve died (If I fail to kill a dwarf) program still continues to the next statement!?
I played around with curly brackets and blocks,but for the life of me...I can`t get it!! I know my IDE is not dumb,so it must be me! All help much appreciated and pardon my poor grammar!

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

int main(){


  int steps;
  int hp = 250;
  int dmg;
  char str[] = "You Died!";

   std::cout << "you walked into the forrest,how many Steps are you willing to make?\n";
   std::cout << "Enter Steps: \n";
   std::cin >> steps;

  if(steps >= 10){
   std::cout << str;
}

  else if(steps < 10){
   std::cout << "you encounterd an dwarf that wants to fight you!\n";
   std::cout << "Click some damage: \n";
   std::cin >> dmg;
  if(dmg < hp){
   std::cout << str;}
  else{
   std::cout << "you killed a dwarf\n";
   std::cout << "you can move on now!\n";
   std::cout << "enter steps: \n";
   std::cin >> steps;}
  if(steps > 10){
   std::cout << str;}
  else if(steps <= 10){
   std::cout << "you encauntered a Troll\n";
   std::cout << "Click some damage: \n";
   std::cin >> dmg;
  if(dmg > hp)
   std::cout << "you killed a Troll\n";
  else
   std::cout << str;}
}
  return 0;
}
.
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
#include <iostream>

int main()
{


  int steps;
  int hp = 250;
  int dmg;
  char str[] = "You Died!";

  std::cout << "you walked into the forrest,how many Steps are you willing to make?\n";
  std::cout << "Enter Steps: \n";
  std::cin >> steps;

  if (steps >= 10) {
    std::cout << str;
  }
  else if (steps < 10) {
    std::cout << "you encounterd an dwarf that wants to fight you!\n";
    std::cout << "Click some damage: \n";
    std::cin >> dmg;
    if (dmg < hp) {
      std::cout << str;
    } else {
      std::cout << "you killed a dwarf\n";
      std::cout << "you can move on now!\n";
      std::cout << "enter steps: \n";
      std::cin >> steps;
    }
    if (steps > 10) {
      std::cout << str;
    } else if (steps <= 10) {
      std::cout << "you encauntered a Troll\n";
      std::cout << "Click some damage: \n";
      std::cin >> dmg;
      if (dmg > hp)
        std::cout << "you killed a Troll\n";
      else
        std::cout << str;
    }
  }
  return 0;
}


The problem is the if (steps > 10) on line 31 is not wholly dependent on the std::cin >> steps on line 29.

Use an indentation tool if your editor doesn't have one built-in.
Eg
indent -kr -nut -ts2 -i2 -l120 -cli0 foo.cpp

man, that was quick! This forum is tha bomb ;)
Thank you so much salem c !!!
I still haven`t learned indentation,but will dwell on it this whole day!
Thank you again, salem c for your time...I got some indentation learning to do!
you don't need a day to study indentation.

if you do these 3 rules and nothing else, your code will be readable. there are better ways, but this will get you 90% of it and few complaints.
rule1: if you type {, every statement after it until you type } has a tab or a fixed number of spaces (3 or so) in front of it. If you nest {} twice, you get 2 tabs or n*2 spaces, and so on for each nested pair.
rule2 every loop or conditional statement gets a {} pair even for the 1 and only statement.
rule3 {} characters go on a line by themselves.

2 and 3 can be argued out in favor of more complex rules. But they work and are simple to follow and very readable.
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
#include <iostream>

int main()
{  
   int steps;
   int hp = 250;
   int dmg;
   char str[] = "You Died!";
   
   std::cout << "you walked into the forrest,how many Steps are you willing to make?\n";
   std::cout << "Enter Steps: \n";
   std::cin >> steps;
   
   if(steps >= 10)
   {   
      std::cout << str;      
   }   
   
   else if(steps < 10)
   {   
      std::cout << "you encounterd an dwarf that wants to fight you!\n";
      std::cout << "Click some damage: \n";
      std::cin >> dmg;
      if(dmg < hp)
      {      
         std::cout << str;
      }
      
      else
      {      
         std::cout << "you killed a dwarf\n";
         std::cout << "you can move on now!\n";
         std::cout << "enter steps: \n";
         std::cin >> steps;
      }
      
      if(steps > 10)
      {      
         std::cout << str;
      }
      
      else if(steps <= 10)
      {      
         std::cout << "you encauntered a Troll\n";
         std::cout << "Click some damage: \n";
         std::cin >> dmg;
         if(dmg > hp)
         std::cout << "you killed a Troll\n";
         else
         std::cout << str;
      }           
   }   
   return 0;   
}





see how doing this shows clearly that line 20 to line 52 are a block?
Last edited on
Thank you very much Jonnin!!
I can see it now!
that is my problem...blocks!! i will go thoroughly trough and try to understand it!
Guys you Rule!!!
Thank you again,from the heart!!!
Topic archived. No new replies allowed.