execute program when information provided is not valid

I have a program that identifies either a cat or a dog. If anything other than that is entered the computer should reply something like - Sorry only cats and dogs. I tried using the != but am having trouble with the string - I may be going about this all wrong for all I know

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

int main()
{
string pet; // "cat" or "dog"
char spayed; // 'y' or 'n'

// Get pet type and spaying information
cout << "Enter the pet type (cat or dog): ";
cin >> pet;
cout << "Has the pet been spayed or neutered (y/n)? ";
cin >> spayed;

// Determine the pet tag fee
if (pet == "cat")
{ if (spayed == 'y')
cout << "Fee is $4.00 \n";
else
cout << "Fee is $8.00 \n";
}
else if (pet == "dog")
{ if (spayed == 'y')
cout << "Fee is $6.00 \n";
else
cout << "Fee is $12.00 \n";
}
else if (pet !=cat || !=dog)

cout<< pet << "Only cats and dogs need pet tags. \n";

return 0;
}
else if (pet !=cat || !=dog)
A few of problems with that statement.
1) cat and dog need to be quoted.
2) The condition will always be true. if pet is a dog, pet != "cat" will be true. if pet is a cat, pet != "dog" will be true. You want && (and), not or operator.
3) C++ does not support an implied left hand side of a condition. You must fully specify both conditions.
else if (pet !="cat" && pet !="dog")

PLEASE USE CODE TAGS (the <> formatting button) when posting code. It makes it easier to read you code and it makes it easier to respond to your post.


Last edited on
Thanks for your help - I'll try this
Topic archived. No new replies allowed.