C/C++ Questions

Kn1ghT

Active Member
Joined
May 13, 2009
Messages
79
Reaction score
0
Hey guys,

I'm just starting out with C++. I've not done any object-orientated stuff so far, so I guess it's just like C. I would like to know if it is possible to put 3 conditions in an if statement? If not, how would you do it?

Situation:

I need something to be printed to screen when these conditions are true:

a + b > c
b + c > a
c + a > b

Can I put all that in one if statement or how would you suggest I do it?


Also, I'm having an issue with getting my last else statement to work right in a small program I wrote. I don't want to post the whole thing here. Is there anyone who I can contact about this via PM? I would really appreciate it and I'm sure if you are an intermediate c or c++ programmer you would be able to easily point out where I made a mistake.

Thank you
 
I need something to be printed to screen when these conditions are true:

a + b > c
b + c > a
c + a > b
Should something be printed to the screen only when ALL 3 of those conditions are met (&&), or when at least one is met (||)?

In case you need ALL 3 conditions to be met, you can do something like:
Code:
if ((a + b) > c && (b + c) > a && (c + a) > b)
    cout << "All 3 conditions were met..." << endl;

In case only ONE condition needs to be met, you can do something like:
Code:
if ((a + b) > c || (b + c) > a || (c + a) > b)
    cout << "Only one condition was met..." << endl;
 
Last edited:
Thank you for that!

I did this:

if (((a + b) > c) && ((b + c) > a) && ((a + c) > b))

And well, it didn't work as it was supposed to. :)

Without all the braces, like in your code, everything runs fine. Thanks so much.
 
Last edited:
Notation:
---------
+ OR
. AND
! NOT

Formula's we will use:
---------------------
x.TRUE = x
(x + !x) = TRUE

Code:
All 3 conditions were met:

----------------------------------
|   A   |   B   |   C   | Result |
----------------------------------
| FALSE | FALSE | FALSE | FALSE  |
| FALSE | FALSE | TRUE  | FALSE  |
| FALSE | TRUE  | FALSE | FALSE  |
| FALSE | TRUE  | TRUE  | FALSE  |
| TRUE  | FALSE | FALSE | FALSE  |
| TRUE  | FALSE | TRUE  | FALSE  |
| TRUE  | TRUE  | FALSE | FALSE  |
| TRUE  | TRUE  | TRUE  | TRUE   |
----------------------------------

A.B.C
----->

Code:
At least 1 conditions is met:

----------------------------------
|   A   |   B   |   C   | Result |
----------------------------------
| FALSE | FALSE | FALSE | FALSE  |
| FALSE | FALSE | TRUE  | TRUE   |
| FALSE | TRUE  | FALSE | TRUE   |
| FALSE | TRUE  | TRUE  | TRUE   |
| TRUE  | FALSE | FALSE | TRUE   |
| TRUE  | FALSE | TRUE  | TRUE   |
| TRUE  | TRUE  | FALSE | TRUE   |
| TRUE  | TRUE  | TRUE  | TRUE   |
----------------------------------

!A.!B.C + !A.B.!c + !A.B.C + A.!B.!C + A.!B.C + A.B.!C + A.B.C

!B.C.(A + !A) + B.C.(A + !A) + B.!C.(A + A)
!B.C + B.C + B.!C
C.(B + !B) + B.!C
C + B.!C
------->

/Yes I'm bored :whistle:
 
@Kn1ghT: Pleasure.
@deq: Dude, you really ARE bored? Aren't there any interesting engineering projects for you to work on? :p
 
I hope Dequadin is still bored cause here is my last problem. :D
The part that isn't working is where I want it to say the option is invalid when option entered isn't 1 or 2. Let's say the user enters 3, the program should give an error. Instead it's just going on.

---> if (numSystem < 1 || numSystem > 2)​

Code:
// Program that calculates Body Mass Index of either the metric and imperial systems

#include <iostream> // makes input and output possible
using namespace std;

// variable declarations
   int numSystem = 0;
   char progRestart = 'a';
   double aweight = 0;
   double aheight = 0;
   double aBMI = 0;
   
int main()
{
  
  do 
  {
// lets user choose system for BMI calculation

   cout << "Please choose a value system: " << endl;
   cout << "1) Metric System" << endl;
   cout << "2) Imperial System" << endl;
   cin >> numSystem;
   
   char progRestart = 'a'; // resets restart function   
   
   // asks user for weight
   cout << "\nPlease enter weight: \n" << endl;
   cin >> aweight;
   
   // asks user for height
   cout << "Please enter height: \n" << endl;
   cin >> aheight;
   
   // metric system
   if (numSystem = 1)
      {
      aBMI = (aweight) / (aheight * aheight);
      }
   // imperial system
   else 
   if (numSystem = 2)
      {
      aBMI = (aweight * 703) / (aheight * aheight);
      }

   // no value system  
      if (numSystem < 1 || numSystem > 2)   
      { 
      cout << "\nInvalid option. Please try again." << endl;
      //cout << "To restart press 'r'" << endl;
      //cin >> progRestart;
      }
   
   cout << "\n\nBody Mass Index is: " << aBMI << endl;
   cout << "_____________________________________________________________________________" << endl;
   
   // BMI guide print out
   cout << "\nBMI VALUES\n\n" << "Underweight: less than 18.5\n" << "Normal: between 18.5 and 24.9\n" 
   << "Overweight: between 25 and 29.9\n" << "Obese: 30 or greater" << endl;
   
   // restart and quit program options
   cout << "To restart press 'r'. To quit press 'q'." << endl;
   cin >> progRestart;
    
   if (progRestart != 'r')
   break;
	 
   else 
   continue;   
  }
  
  while (progRestart = 'r');
  
}

If you have any tips on where I should improve any of my code, I would greatly appreciate it!
 
An equality comparison operator is ==, not =. Change that, and you will probably find its not working because numSystem isnt being set correctly. Get it to print out numSystem as soon as its value is set.

EDIT: Also, read up on functions. Your code would be a lot simpler if you put the logic into a separate function. In fact, while you are at it, I would create a class. Creating a class is VERY easy, dont be scared. Create a class called something like BMICalculator, and give it a function that takes in two numbers and returns a BMI. You can even make it static if you want (read up on what static is too). Then you call that method from your main program.
 
Last edited:
@Ancalagon Thank you, but even after replacing = with ==, my invalid option is still not working.....

Anyway, thanks a LOT for the tips!

Anyone else know why my invalid option is not working?
 
I suggest breaking the problem down. First, print out the value of numSystem at that time. Then, get it to print out the value of numSystem < 1, and then, the value of numSystem > 2. Then the value of the whole thing - ie, numSystem < 1 || numSystem > 2

What that will do is tell you what your logical expressions are returning, which is probably not what you think. It might indicate the problem lies elsewhere. Normally, in visual studio I would do this with the debugger. Depending on what environment you are using to code this, you might also have a debugger. Otherwise, you have to rely on the oldfashioned method - outlined above.

EDIT: A sidenote, remember that statements like numSystem < 1 themselves return an int. You want to know what that int is - it should either be 0 or 1. Thats why you want to print it out, because it will tell you what the result of the comparison was. Also remember that C++, by default, lacks a boolean type if I remember correctly, so I think a tinyint is usually used instead.

The last thing to consider is that there are many, many ways to achieve things with code, and many that are a lot better than your solution. Look up the switch and case statements - those should help.

Why are you doing this, by the way? Trying to teach yourself code, or is this varsity related? I recommend starting with a basic coding tutorial. Theres a lot of aspects to programming that you need to cover, specifically flow of control, including loops, as well as things like classes and functions.
 
First, I agree with Ancalagon.

I have modified your code slightly to make it work as it should. It is definitely not the best solution, but it is A solution without having to modify much. :)

Code:
// Program that calculates Body Mass Index of either the metric and imperial systems

#include <iostream> // makes input and output possible
using namespace std;

// variable declarations
   int numSystem = 0;
   char progRestart = 'a';
   double aweight = 0;
   double aheight = 0;
   double aBMI = 0;
   
int main()
{
  
  do 
  {
	  char progRestart = 'a'; // resets restart function

// lets user choose system for BMI calculation
   cout << "Please choose a value system: " << endl;
   cout << "1) Metric System (height in meter and weight in kilogram)" << endl;
   cout << "2) Imperial System (height in inches and weight in pounds)" << endl;
   cin >> numSystem;
   
   if (numSystem < 1 || numSystem > 2)   
      { 
      cout << "\nInvalid option. Please try again." << endl;
      //cout << "To restart press 'r'" << endl;
      //cin >> progRestart;
	  char progRestart = 'q';
	  break;
	  cin.get();
      }  
   
   // asks user for weight
   cout << "\nPlease enter weight: \n" << endl;
   cin >> aweight;
   
   // asks user for height
   cout << "Please enter height: \n" << endl;
   cin >> aheight;
   
   // metric system
   if (numSystem == 1)
      {
      aBMI = (aweight) / (aheight * aheight);
      }
   // imperial system
   else 
   //if (numSystem == 2)
      {
      aBMI = (aweight * 703) / (aheight * aheight);
      }
   // no value system  
   
   cout << "\n\nBody Mass Index is: " << aBMI << endl;
   cout << "_____________________________________________________________________________" << endl;
   
   // BMI guide print out
   cout << "\nBMI VALUES\n\n" << "Underweight: less than 18.5\n" << "Normal: between 18.5 and 24.9\n" 
   << "Overweight: between 25 and 29.9\n" << "Obese: 30 or greater\n" << endl;
   
   // restart and quit program options
   cout << "To restart press 'r'. To quit press 'q'.\n" << endl;
   cin >> progRestart;
    
   if (progRestart != 'r')
   break;
	 
   else 
   continue;   
  }
  
  while (progRestart = 'r');
  
}

RBD
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X