Statement Loops Continuously

I've been working on a simple 'Command' system, where the user can enter set commands to change the variable values, and then repeat the value back to the user.

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
#include "stdafx.h"
#include <iostream>
#include <string>
#define MIN_STACK 1
#define MAX_STACK 2147483647
using namespace std;

class player {
public:
	string name;
	int health;
	int coins;
	bool isDead = false;
};



int main()
{
	player p;
	while (p.isDead == false) {
		cin >> p.health;
		if (p.health > MAX_STACK) {
			cout << "Health Value too high!" << endl;
		}
		else if (p.health < MIN_STACK) {
			cout << "Health value cannot be set below 1." << endl;
			//p.isDead = true;
		}
		else {
			cout << "Health Value set to: " << p.health << endl;
		}
	}
	cout << "You are dead!" << endl;
	

    return 0;
}

With the current code, if a user enters a value over 2,147,483,647 the while loop seems to go into an infinite loop. Where the statement should print out "Health Value too high!"
The else if section works if the user enters a number less than 1, just not the highest figure.
You never change the value of p.isDead, so (p.isDead == false) is always true.

Also, think about this: what is the highest positive value that can be stored in p.health ?
Last edited on
Even though p.isDead == false shouldn't that statement still print out?
If a user enters a value below 1, the statement prints out the "Health value cannot be set below 1." Not really understanding the issue when entering the value over 2147483647. Surely if the value is entered over that amount, the first section of the if statement should execute?
Please correct me if I'm wrong. :)

And I know a standard int can hold -2,147,483,647 to 2,147,483,647.
So... if the highest value that can be stored in p.isDead is 2,147,483,647, then how can (p.health > MAX_STACK) ever be true?
That make's complete sense now. Thank you!

In regards to entering a value over 2147483647, how could I make it so it doesn't enter a infinite loop? If someone tries to be smart, or how would I go about capping the value?
you were just told that the infinite loop is because the condition never changes...

A completely separate problem is too high (or too low) of an entry. In this case you may overrun the cin buffer and so it ends up being in erroneous state for the remainder of the loop. To clear cin errors, cin.clear(), and to ignore the remainder of the line, use cin.ignore(...).

What was actually happening in your case for big numbers was that cin was in erroneous state and the input was set to 2147483647. As an int, it can never be greater than 2147483647 and so the ">" comparison was just pointless. Pick a more sensible max for health.

Other tips:
- Prefer constants instead of defines (which do a lot more replacing than you may think).
- Limit the things you put in global scope.
- Prefer to capitalize classes, especially if you're using namespace std. Those are lowercase on purpose.
- If you're making a class with everything public, you might as well use a struct.
- Prompt the user with info about what's expected. As you had it, the user might think the program froze, not realizing it wants input. (Yes, today you are the user, but think of tomorrow)

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

using namespace std;

struct Player {
    string name;
    int health;
    int coins;
    bool isDead = false;
};

int main()
{
    const int MIN_HEALTH = 1;
    const int MAX_HEALTH = 20000;
    
    Player p;
    int x = 1;
    int times = 5;
    cout << "Enter health ("<<MIN_HEALTH<<"-"<<MAX_HEALTH<<") " << times <<" times\n";
    while (x<=times) 
    {
        cout << "#" << x << " ";
        cin >> p.health;
        cin.clear();
        cin.ignore(numeric_limits<int>::max(), '\n');
        if (p.health > MAX_HEALTH) {
            cout << "Health Value too high!" << endl;
        }
        else if (p.health < MIN_HEALTH) {
            cout << "Health value cannot be set below 1." << endl;
            //p.isDead = true;
        }
        else {
            cout << "Health Value set to: " << p.health << endl;
        }       
        
        x++;
    }
    cout << "You are dead!" << endl;
    

    return 0;
}


can run it at https://repl.it/@icy_1/RunnyWellmadeTranslations
Last edited on
Topic archived. No new replies allowed.