else if

Here is my code thus far, the problem I am now having, is that when I get to the else if function, it will cout only the if function, however if my input = one of the else if variables, the program will not cout my statement, and will exit. However, if I run the code in an online compiler, it runs properly and returns everything properly. Appreciate the help!
PS... I have tried to put in the code brackets but it is not letting me for some reason.

#include <iostream>
#include <iomanip>
using namespace std;

int main()

{

int type, weight;



cout << setw(50) << "Noahs Horse Yard\n";
cout << "_______________________________________________________________________________\n";

cout << "Horse type";
cout << setw(40) << " Minimum Optimum weight";
cout << setw(30) << "Maximum Optimum Weight\n";


// key
cout << "_______________________________________________________________________________\n";
cout << "\n";
cout << "1." << " Light Riding Horse" << setw(20) << "840" << setw(25) << "1200\n";
cout << "\n";
cout << "2." << " Large Riding Horse" << setw(20) << "1110" << setw(25) << "1300\n";
cout << "\n";
cout << "3." << " Draft Horse" << setw(27) << "1500" << setw(25) << "2200\n";
cout << "\n";

cout << "Enter type of horse <1-3>";
cin >> type;

if (type == 1)
{
cout << "Light riding horse\n";
}
else if (type == 2)
{
cout << " Large riding horse\n";
}
else if (type == 3)
{
cout << "Draft riding horse\n";
}



cout << " Input the weight of horse in pounds";
cin >> weight;
cout << " The Horses weight is " << weight << " pounds\n";


// underweight, optimum, and overweight variables.
float uw, o, ow;

uw = 3.3;
o = 3.0;
ow = 2.5;

// output of food in pounds to feed horse.
// lighthorse
if ( type == 1 && weight < 840)
{
cout << " Your horse is underweight, you should feed " << uw << " pounds of food";
}
else if (type ==1 && weight > 840 && weight < 1200)
{
cout << " Your horse is optimum weight, you should feed " << o << " pounds of food.";
}
else if ( type == 1 && weight > 1200)
{
cout << "Your horse is overweight, you should feed " << ow << " pounds of food.";
}

//largehorse
else if (type == 2 && weight < 1100)
{
cout << "Your horse is underweight, and you should feed " << uw < " pounds of food.";
}
else if (type == 2 && weight > 1100 && weight < 1300)
{
cout << "Your horse is optimum weight, you should feed " << o << " pounds of food";
}
else if ( type == 2 && weight > 1300 )
{
cout << "Your horse is overweight, and you should feed " << ow << " pounds of food.";
}
//drafthorse
else if (type == 3 && weight < 1500)
{
cout << "Your horse is underweight, you should feed " << uw << " pounds of food.";
}
else if (type == 3 && weight > 1500 && weight < 2200)
{
cout << "Your horse is optimum weight, and you should feed " << o << " pounds of food";
}
else if ( type == 3 && weight > 2200)
{
cout << " Your horse is overweight, and you should feed " << ow << " pounds of food.";
}
system("PAUSE");
return 0;
}
Last edited on
that code doesn't compile because you've got a typo, ¿how the hell you've got a typo?


> when I get to the else if function, it will cout only the if function,
> however if my input = one of the else if variables, the program will not cout
> my statement, and will exit.
¿may provide an input/output example?

> However, if I run the code in an online compiler, it runs properly and
> returns everything properly
perhaps your eyes are deceiving you.
try running from withing a terminal.
It contains a small error:

cout << "Your horse is underweight, and you should feed " << uw < " pounds of food.";

This should be:

cout << "Your horse is underweight, and you should feed " << uw << " pounds of food.";

Also you should consider using double instead of float, else your uw, o, ow will be demoted. I tried your program with VS 2015 and works flawlessly, no matter what condition is tested. :)
awesome thank you!
Topic archived. No new replies allowed.