confused on bracket placment and if/if else statments

this is what i have so far can someone please correct me where im wrong
#include <iostream>
using namespace std;

int main()
{
char HorseType;
int weight;

cout << "What type of horse do you have ?" << endl;
cout << "Enter 1 (Light Riding Horse), 2 (Large Riding Horse), 3 (Draft Horse)"
<< endl;
cin >> HorseType;

cout << "Now enter the horse's weight" << endl;
cin >> weight;

if (HorseType = 1 && weight >= 840 <= 1200)
{

cout << "WeightCategory: Optium" << endl;
cout << "Feed 3.0 pounds" << endl;
}
else if
{
(HorseType = 1 && weight > 1200)

cout << "Weight Category: Overweight" << endl;
cout << "Feed 2.5 pounds" << endl;
}
else
{
if (HorseType = 1 && weight < 840)

cout << "Weight Category: Underweight" << endl;
cout << "Feed 3.3 pounds" << endl;
}
if (HorseType = 2 && weight >= 1100 <= 1300)
{
cout << "Weight Category: Optium" << endl;
cout << "Feed 3.0 pounds" << endl;
}
else if
{
(HorseType = 2 && weight < 1100)
cout << "Weight Category: Underweight" << endl;
cout << "Feed 3.3 pounds" << endl;
}
else
{
if (HorseType = 2 && weight > 1300)
cout << "Weight Category: Overweight" << endl;
cout << "Feed 2.5 pounds" << endl;
}

if (HorseType = 3 && weight >= 1500 <= 2200)
{
cout << "Weight Category: Optium" << endl;
cout << "Feed 3.0 pounds" << endl;
}
else if
{
(HorseType = 3 && weight < 1500)
cout << "Weight Category: Underweight" << endl;
cout << "Feed 3.3 pounds" << endl;
}
else
{
if (HorseType = 3 && weight > 2200)
cout << "Weight Category: Overweight" << endl;
cout << "Feed 2.5 pounds" << endl;

}
system("pause");
return (0);
}
Last edited on
The confusion seems to be over the = and == operators.

= is used to assign a value
== is used to test for equality

example here the = should be ==
if (HorseType = 1 && weight >= 840 <= 1200)

There should also be another &&
if (HorseType == 1 && weight >= 840 && weight <= 1200)
Last edited on
Hi,

Please use code tags:

http://www.cplusplus.com/articles/z13hAqkS/


If you want more than 1 statement to be associated with an if else if else or a loop, then you need braces.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (HorseType == 1 && weight >= 840 && weight <= 1200)
{   // indent inside braces
    cout << "WeightCategory: Optium" << endl;
    cout << "Feed 3.0 pounds" << endl;
}
else if (HorseType = 1 && weight > 1200)
{
    cout << "Weight Category: Overweight" << endl;
    cout << "Feed 2.5 pounds" << endl;
}
else if (HorseType = 1 && weight < 840)
{
    cout << "Weight Category: Underweight" << endl;
    cout << "Feed 3.3 pounds" << endl;
}



I changed the else part, I couldn't see the point of having it with an if inside it.

An else statement is good for catching errors.

In this example :
1
2
3
if (HorseType = 2 && weight > 1300)
cout << "Weight Category: Overweight" << endl;
cout << "Feed 2.5 pounds" << endl;


If line 1 is false, then line 3 will be executed. If line 1 is true, both lines 2 and 3 will be executed. In other words line 3 is always executed. The way to solve this is to use the braces:

1
2
3
4
5
6
7
8
9
if (HorseType = 2 && weight > 1300)
{
cout << "Weight Category: Overweight" << endl;
cout << "Feed 2.5 pounds" << endl;
}
//condition is false this code executed
.
.
.
Topic archived. No new replies allowed.