Reputation: 13
I am trying to use different if statements based on user input. However it only seems to use the final set. any help would be great thanks.
char type[20];
double weight;
double feed;
cout<< "Enter horse type: ";
cin>>type;
cout << "Enter the horse weight in whole pounds: ";
cin>>weight;
cout<<"Horse type: "<<type<<endl;
cout<<"Horse weight: "<<weight<<endl;
This is my if statements.
{
if (type=="Light");
if (weight >= 840 && weight <=1200)
feed = (3.0);
else if (weight< 840)
feed = (3.3);
else if (weight > 1200)
feed = (2.5);
}
{
if (type=="Large");
if (weight >= 1100 && weight <=1300)
feed=(3.0);
else if (weight < 1100)
feed=(3.3);
else if (weight > 1300)
feed= (2.5);
}
{
if (type=="Draft");
if (weight >= 1500&& weight <=2200)
feed = (3.0);
else if (weight< 1500)
feed = (3.3);
else if (weight >2200)
feed= (2.5);
}
cout<<"Feed Amount "<<feed<<" pounds"<<endl;
Thanks again for any help
Upvotes: 0
Views: 903
Reputation: 141554
Where you have:
{
if (type=="Light");
should be:
if ( type == "Light" )
{
and the same for Draft and Large. What you are actually doing is taking no action regardless of the if
, and always executing the following code.
Also ( as noted by Mike Seymour ) change char type[20];
to std::string type;
. If you really must stick to char
, then you will also need to change your comparison.
If your compiler supports C++14 then:
if ( type == "Light"s )
Otherwise:
if ( type == std::string("Light") )
For any of these cases you need #include <string>
at the top of your file.
Upvotes: 0
Reputation: 254431
You can't compare C-style strings (character arrays) using ==
. That compares the addresses of the arrays, not their contents.
Use std::string
instead. Replace the first line with
std::string type;
You also need to fix the if
statements:
if (type == "Whatever") // no ;
{
// do stuff
}
Upvotes: 3