SolvedSomething wrong in my code , help me !

Posts 1–6 of 6 · Page 1 of 1
Something wrong in my code , help me !
Hello , I just started to learn c++ yesterday , and something is wrong in my code and I can't figure out what !!!


Code:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
    bool isAlive(true);
    int hp(100);
    cin >> hp;
    if (hp ==0)
    {
        isAlive == false;
    }
    else
    {
        isAlive == true;
    }
    if (isAlive)
    {
        cout << "cool you are alive" << endl;
    }
    else
    {
        cout << "rip" << endl;
    }




    return 0;
}


When I enter 0 , it keep saying : Cool you are alive !

But I want it to say : rip when hp ==0
Hello! Your problem is pretty easy to figure out if you look at your if statement, it's not the statement itself but what it does if (true) else (false)
isAlive == false;
else
isAlive == true;

Do you see it? You've got the conditional == not the assignment =. So it's not really changing isAlive to true or false, it's just keeping it value initialized (true).

On another note, I don't know if you're doing this just to play with bools but you can do the same thing with a lot less code. This will do the same thing:

Code:
#include <iostream>

int main()
{
	int hp;	//	default initialize hp, don't give it any value
	std::cin >> hp;

	if (hp)		//	Tests to see if hp is true (0 is false, any number other than 0 is true)
		std::cout << "cool you are alive" << std::endl;	//	hp is any number other than 0
	else
		std::cout << "rip" << std::endl;	//	hp is 0

	return 0;
}
Quote Originally Posted by InunoTaishou View Post
Hello! Your problem is pretty easy to figure out if you look at your if statement, it's not the statement itself but what it does if (true) else (false)
isAlive == false;
else
isAlive == true;

Do you see it? You've got the conditional == not the assignment =. So it's not really changing isAlive to true or false, it's just keeping it value initialized (true).

On another note, I don't know if you're doing this just to play with bools but you can do the same thing with a lot less code. This will do the same thing:

Code:
#include <iostream>

int main()
{
	int hp;	//	default initialize hp, don't give it any value
	std::cin >> hp;

	if (hp)		//	Tests to see if hp is true (0 is false, any number other than 0 is true)
		std::cout << "cool you are alive" << std::endl;	//	hp is any number other than 0
	else
		std::cout << "rip" << std::endl;	//	hp is 0

	return 0;
}
Oh its a way better and simple that way !! I thought it was necessary to use bool :/ Thank you very much !!
Here's another fun one for you to learn, the conditional operator (aka ternary operator, the ? operator)

Code:
#include <iostream>

int main()
{
	int hp;	//	default initialize hp, don't give it any value
	std::cin >> hp;

	//	Using the conditional operator (aka the ternary, ?, operator) to test help and count the selected message
	std::cout << ((hp == 0) ? "You are dead" : (hp > 0 && hp <= 30) ? "You are just barely alive!" : 
				 (hp > 30 && hp <= 60) ? "You're alive!" : "You're alive and well!") << std::endl;
	return 0;
}
cout specific messages based on health
Quote Originally Posted by mic44 View Post
Oh its a way better and simple that way !! I thought it was necessary to use bool :/ Thank you very much !!
It's not necessary but you can use bool if you wanted to.

Code:
#include <iostream>

using namespace std;

int main()
{
    bool isAlive = true;
    int health = 100;

    cout << "Enter the amount of health.\n";
    cin >> health;

    // if health is less than or equal to 0 then set isAlive to false
    if(health <= 0)
        isAlive = false;

    // if isAlive is false than display R.I.P.
    if(!isAlive)
        cout << "R.I.P.\n";
    // if isAlive is true than display....
    else
        cout << "Cool, you're alive.\n";

    cin.get(); // so program doesn't automatically close
        return 0;
}
Posts 1–6 of 6 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us