If then statement not working as intended, involving logical operators.

I'm am working on an assignment that involves if and then statements as well as logical operators.

My objective is to write a code for the price of pet tags. They way it works is you must state whether you have a dog or cat. If you have either then you must state whether it has been spayed. Depending on the choice you will get a price. IF you don't have a dog or cat, then it will skip to "Only cats and dogs need pet tags"

My question is that it works seamlessly when I don't use "||" in the "if (spayed == )" argument just as I wrote it when the user writes dog. So if you just type a lower case y, it will work as intended. however one of the objectives of the assignment is to make it work with both lowercase and uppercase Y. I tried to do this with the cat choice, but no matter the input i keep getting the first choice rather than the "else if" statement.

What am I doing wrong? Thank you.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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;

	if (pet == "cat" || "dog")
	{
		// Determine the pet tag fee 
		if (pet == "cat")
		{
			cout << "Has the pet been spayed or neutered (y/n)? ";
			cin >> spayed;

				if (spayed == 'y' || 'Y')
					cout << "Fee is $4.00 \n";
				else if (spayed == 'n' || 'N')
					cout << "Fee is $8.00 \n";
		}
		else if (pet == "dog")
		{
			cout << "Has the pet been spayed or neutered (y/n)? ";
			cin >> spayed;

			if (spayed == 'y')
				cout << "Fee is $6.00 \n";
			else if (spayed == 'n')
				cout << "Fee is $12.00 \n";
		}
		else
			cout << "Only cats and dogs need pet tags";
	}

	return 0;
}
Last edited on
if (pet == "cat" || "dog")

Common mistake.
Every condition between || or && must be an independent statement.

You have to say:
if (pet == "cat" || pet == "dog")

Also applies to your line 22 and 24
Last edited on
Thank You!! I know this probably means nothing here, but-

+rep
Topic archived. No new replies allowed.