Jump to content

How the fuck do you use Boolean variable

multitasker451

I'm trying to write a program (C++) for an assignment and I'm really strugglin with Boolean variables.  Basically, the user is asked if there are fish present, and then they are supposed to input a 1 (yes/true) or a 0 (no/false).  The result then leads to a water volume calculation.  Here's what I have (with unrelated stuff removed):

 

float water_volume;

float tank_volume;

bool areFishPresent;

 

cin >> areFishPresent;

 

//make water volume calculations

if (areFishPresent == true){

  water_volume = tank_volume - (areFishPresent * 2);

}

else (areFishPresent == false);{

  water_volume = tank_volume;

}

 

please help.  When I try to run it I get a message that says "floating point exception (core dumped)."

Processor Intel Core i5-4690K CPU Cooler Cooler Master Hyper 212 EVO Motherboard MSI Z97-G45 RAM 8GB G.Skill Ripjaws X Storage Samsung 850 EVO 250GB SSD WD Caviar Blue 1TB HDD Graphics Card EVGA GeForce GTX 970 Case NZXT Phantom (Black) PSU Corsair CX600M Operating System Windows 10 Monitor BenQ GL2760H Headset Skullcandy PLYR 1 & Sennheiser Momentum

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Windows 10 said:

Calm down, no need for foul langauge

sorry I've been working on this for so long

Processor Intel Core i5-4690K CPU Cooler Cooler Master Hyper 212 EVO Motherboard MSI Z97-G45 RAM 8GB G.Skill Ripjaws X Storage Samsung 850 EVO 250GB SSD WD Caviar Blue 1TB HDD Graphics Card EVGA GeForce GTX 970 Case NZXT Phantom (Black) PSU Corsair CX600M Operating System Windows 10 Monitor BenQ GL2760H Headset Skullcandy PLYR 1 & Sennheiser Momentum

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, multitasker451 said:

-snip-

You just need else for the last statement. No need for "(areFishPresent == false);"

 

I'm also guessing you have some other code before this where you actually set areFishPresent to true or false based on user input?

Link to comment
Share on other sites

Link to post
Share on other sites

 

Just now, scottyseng said:

You just need else for the last statement. No need for "(areFishPresent == false)"

I got the same message after running it

Processor Intel Core i5-4690K CPU Cooler Cooler Master Hyper 212 EVO Motherboard MSI Z97-G45 RAM 8GB G.Skill Ripjaws X Storage Samsung 850 EVO 250GB SSD WD Caviar Blue 1TB HDD Graphics Card EVGA GeForce GTX 970 Case NZXT Phantom (Black) PSU Corsair CX600M Operating System Windows 10 Monitor BenQ GL2760H Headset Skullcandy PLYR 1 & Sennheiser Momentum

 

Link to comment
Share on other sites

Link to post
Share on other sites

if(areFishPresent){

     do stuff

} else { 

     do stuff

}

 

Now, if you wanted to add a third option (obviously this wouldn't apply for booleans), then something like this: 


int x = ____; //get user input

if(x == 0){

         do this stuff;

} else if (x == 1) {

       do this other stuff;

} else {

       do this

}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, multitasker451 said:

 

I got the same message after running it

I also saw that you had a semi colon after (areFishPresent == false)

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, scottyseng said:

You just need else for the last statement. No need for "(areFishPresent == false)"

 

I'm also guessing you have some other code before this where you actually set areFishPresent to true or false based on user input?

I don't think I can set it to true or false for this scenario since the user of the program is supposed to input whether it is true or false

Processor Intel Core i5-4690K CPU Cooler Cooler Master Hyper 212 EVO Motherboard MSI Z97-G45 RAM 8GB G.Skill Ripjaws X Storage Samsung 850 EVO 250GB SSD WD Caviar Blue 1TB HDD Graphics Card EVGA GeForce GTX 970 Case NZXT Phantom (Black) PSU Corsair CX600M Operating System Windows 10 Monitor BenQ GL2760H Headset Skullcandy PLYR 1 & Sennheiser Momentum

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, scottyseng said:

I also saw that you had a semi colon after (areFishPresent == false)

I deleted that after deleting (areFishPresent == false)

Processor Intel Core i5-4690K CPU Cooler Cooler Master Hyper 212 EVO Motherboard MSI Z97-G45 RAM 8GB G.Skill Ripjaws X Storage Samsung 850 EVO 250GB SSD WD Caviar Blue 1TB HDD Graphics Card EVGA GeForce GTX 970 Case NZXT Phantom (Black) PSU Corsair CX600M Operating System Windows 10 Monitor BenQ GL2760H Headset Skullcandy PLYR 1 & Sennheiser Momentum

 

Link to comment
Share on other sites

Link to post
Share on other sites

The whole code:

 

#include<iostream>

using namespace std;

int main()
{
  int length;
  int width;
  int height_ft;
  int height_in;
  float tank_volume;
  float water_volume;
  bool areFishPresent;
  float waste_mass;
  int fish1;

 

//display welcome message
  cout <<"Welcome to the Fish Tank Computer" << endl;
  cout <<"Enter the following information:" << endl;

 

//user inputs length
  cout <<"Please enter the length of the tank in feet" << endl;
  cin >> length;

 

//user inputs whether fish are present
  cout <<"Are fish present (1 - yes, 0 - no)?" << endl;
  cin >> areFishPresent;

 

//make width and height calculations
  width = length * (1.0/4.0);
  height_ft = length * (2.0/3.0);

 

//make water volume calculations
  if (areFishPresent == true){
    water_volume = tank_volume - 2;
  }
  else{
    water_volume = tank_volume;
  }

 

//determine number of fish the tank can hold
  fish1 = water_volume / 25;

 

//determine amount of nuclear waste required
  height_in = height_ft / 12;
  waste_mass = 11 / height_in;

 

//output measurements
  cout <<"Your tank dimensions are:" << endl;
  cout << "length" << length << "ft" << endl;
  cout << "width" <<  width << "ft" << endl;
  cout << "height" << height_ft << "ft" << endl;
  cout << "tank cap" << tank_volume << "gals" << endl;
  cout << "water" << water_volume << "gals" << endl;
  cout << "waste" << waste_mass << "rads" << endl;
  cout << "Your tank will hold:" << fish1 << "fish." << endl;
  cout << "Thank you for using the Fish Tank Computer." << endl;

  return 0;
}

 

Processor Intel Core i5-4690K CPU Cooler Cooler Master Hyper 212 EVO Motherboard MSI Z97-G45 RAM 8GB G.Skill Ripjaws X Storage Samsung 850 EVO 250GB SSD WD Caviar Blue 1TB HDD Graphics Card EVGA GeForce GTX 970 Case NZXT Phantom (Black) PSU Corsair CX600M Operating System Windows 10 Monitor BenQ GL2760H Headset Skullcandy PLYR 1 & Sennheiser Momentum

 

Link to comment
Share on other sites

Link to post
Share on other sites

 


float water_volume;

float tank_volume;

bool areFishPresent;

 

cin >> areFishPresent;

 

//make water volume calculations

if (areFishPresent == true){

  water_volume = tank_volume - (areFishPresent * 2); // WHAT IS THIS LINE DOING?

}

else  {

  water_volume = tank_volume;

}

 

 

I'm not positive if you can cin a boolean. Try using an int instead. 

 

What is 2*true? 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, multitasker451 said:

I deleted that after deleting (areFishPresent == false)

Ah, the other thing I'm seeing is that you're trying to multiply areFishPresent by 2...

water_volume = tank_volume - (areFishPresent * 2); 

You can't multiply a Boolean variable.

Link to comment
Share on other sites

Link to post
Share on other sites

Make areFishPresent an int. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, multitasker451 said:

-snip-

You don't really need a Boolean variable at all, you can just have the if statement check if areFishPresent is equal to 1 (after turning it into a int).

Link to comment
Share on other sites

Link to post
Share on other sites

OK, the person potentially using this program is supposed to enter a 1 (for yes/true) or a 0 (for no/false) when asked if fish are present.  Depending on their answer, the water volume is calculated.

Processor Intel Core i5-4690K CPU Cooler Cooler Master Hyper 212 EVO Motherboard MSI Z97-G45 RAM 8GB G.Skill Ripjaws X Storage Samsung 850 EVO 250GB SSD WD Caviar Blue 1TB HDD Graphics Card EVGA GeForce GTX 970 Case NZXT Phantom (Black) PSU Corsair CX600M Operating System Windows 10 Monitor BenQ GL2760H Headset Skullcandy PLYR 1 & Sennheiser Momentum

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, scottyseng said:

Ah, the other thing I'm seeing is that you're trying to multiply areFishPresent by 2...


water_volume = tank_volume - (areFishPresent * 2); 

You can't multiply a Boolean variable.

OK, I see that now, that was wrong, but I'm still getting the message.  I'm going to update the code above

 

Processor Intel Core i5-4690K CPU Cooler Cooler Master Hyper 212 EVO Motherboard MSI Z97-G45 RAM 8GB G.Skill Ripjaws X Storage Samsung 850 EVO 250GB SSD WD Caviar Blue 1TB HDD Graphics Card EVGA GeForce GTX 970 Case NZXT Phantom (Black) PSU Corsair CX600M Operating System Windows 10 Monitor BenQ GL2760H Headset Skullcandy PLYR 1 & Sennheiser Momentum

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, multitasker451 said:

OK, I see that now, that was wrong, but I'm still getting the message.  I'm going to update the code above

 

Are you still using a boolean or did you change it to an int? 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, multitasker451 said:

OK, the person potentially using this program is supposed to enter a 1 (for yes/true) or a 0 (for no/false) when asked if fish are present.  Depending on their answer, the water volume is calculated.

Do this after turning areFishPresent to a int instead of a boolean:

if (areFishPresent == 1){
	water_volume = tank_volume - 2;
} else {
	water_volume = tank_volume;
} 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, djdwosk97 said:

Are you still using a boolean or did you change it to an int? 

changing it to int doesn't do anything

Processor Intel Core i5-4690K CPU Cooler Cooler Master Hyper 212 EVO Motherboard MSI Z97-G45 RAM 8GB G.Skill Ripjaws X Storage Samsung 850 EVO 250GB SSD WD Caviar Blue 1TB HDD Graphics Card EVGA GeForce GTX 970 Case NZXT Phantom (Black) PSU Corsair CX600M Operating System Windows 10 Monitor BenQ GL2760H Headset Skullcandy PLYR 1 & Sennheiser Momentum

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, multitasker451 said:

changing it to int doesn't do anything

Paste a new copy of the code, and use code brackets.

 

[code.]

 

[/code.]

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, djdwosk97 said:

Paste a new copy of the code, and use code brackets.

 

[code.]

 

[/code.]


using namespace std;

int main()
{
  int length;
  int width;
  int height_ft;
  int height_in;
  float tank_volume;
  float water_volume;
  int areFishPresent;
  float waste_mass;
  int fish1;

 

//display welcome message
  cout <<"Welcome to the Fish Tank Computer" << endl;
  cout <<"Enter the following information:" << endl;

 

//user inputs length
  cout <<"Please enter the length of the tank in feet" << endl;
  cin >> length;

 

//user inputs whether fish are present
  cout <<"Are fish present (1 - yes, 0 - no)?" << endl;
  cin >> areFishPresent;

 

//make width and height calculations
  width = length * (1.0/4.0);
  height_ft = length * (2.0/3.0);

 

//make water volume calculations
  if (areFishPresent == 1){
    water_volume = tank_volume - 2;
  }
  else{
    water_volume = tank_volume;
  }

 

//determine number of fish the tank can hold
  fish1 = water_volume / 25;

 

//determine amount of nuclear waste required
  height_in = height_ft / 12;
  waste_mass = 11 / height_in;

 

//output measurements
  cout <<"Your tank dimensions are:" << endl;
  cout << "length" << length << "ft" << endl;
  cout << "width" <<  width << "ft" << endl;
  cout << "height" << height_ft << "ft" << endl;
  cout << "tank cap" << tank_volume << "gals" << endl;
  cout << "water" << water_volume << "gals" << endl;
  cout << "waste" << waste_mass << "rads" << endl;
  cout << "Your tank will hold:" << fish1 << "fish." << endl;
  cout << "Thank you for using the Fish Tank Computer." << endl;

  return 0;
}

 

Processor Intel Core i5-4690K CPU Cooler Cooler Master Hyper 212 EVO Motherboard MSI Z97-G45 RAM 8GB G.Skill Ripjaws X Storage Samsung 850 EVO 250GB SSD WD Caviar Blue 1TB HDD Graphics Card EVGA GeForce GTX 970 Case NZXT Phantom (Black) PSU Corsair CX600M Operating System Windows 10 Monitor BenQ GL2760H Headset Skullcandy PLYR 1 & Sennheiser Momentum

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, multitasker451 said:


using namespace std;

int main()
{
  int length, width, height_ft, height_in, areFishPresent, fish1;
  float tank_volume, water_volume, waste_mass;
 

  //display welcome message
  cout <<"Welcome to the Fish Tank Computer" << endl;
  cout <<"Enter the following information:" << endl;

  //user inputs length
  cout <<"Please enter the length of the tank in feet" << endl;
  cin >> length;

  //user inputs whether fish are present
  cout <<"Are fish present (1 - yes, 0 - no)?" << endl;
  cin >> areFishPresent;

  //make width and height calculations
  width = length * (1.0/4.0);
  height_ft = length * (2.0/3.0);

  //make water volume calculations
  if (areFishPresent == 1){
    water_volume = tank_volume - 2;
  }
  else{
    water_volume = tank_volume;
  }

  //determine number of fish the tank can hold
  fish1 = water_volume / 25;

  //determine amount of nuclear waste required
  height_in = height_ft / 12;
  waste_mass = 11 / height_in;

  //output measurements
  cout <<"Your tank dimensions are:" << endl;
  cout << "length" << length << "ft" << endl;
  cout << "width" <<  width << "ft" << endl;
  cout << "height" << height_ft << "ft" << endl;
  cout << "tank cap" << tank_volume << "gals" << endl;
  cout << "water" << water_volume << "gals" << endl;
  cout << "waste" << waste_mass << "rads" << endl;
  cout << "Your tank will hold:" << fish1 << "fish." << endl;
  cout << "Thank you for using the Fish Tank Computer." << endl;

  return 0;
}

 

What happens if you run it? (p.s. magic numbers are bad style). 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, multitasker451 said:

  int length;
  int width;
  int height_ft;
  int height_in;


  width = length * (1.0/4.0);
  height_ft = length * (2.0/3.0);

width and height_ft should be float

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, scottyseng said:

width and height_ft should be float

They'll just get truncated, that won't break anything.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, djdwosk97 said:

They'll just get truncated, that won't break anything.

Ah, forgot about that. Yeah, I'm still trying to see what else would break in this code.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×