0
#include <bits/stdc++.h>
using namespace std;

class animal { // class

public:
void get_data(string breed, string color, char gender, int age, int cost) {
    cout << "Breed:" << breed << "\n" << "Color:" << color << "\n" << "Gender:" << gender << "\n" << "Age:" << age << "\n" << "Cost:" << cost << "\n";
    }
} dog, cat; // object

int main()
{   
dog.get_data('Chihuahua', 'Brown', 'F', 5, 10000);
cat.get_data('British Shorthair', 'Gray', 'F', 3, 15000);
return 0;
}

Output should be Breed: Chihuahua Color: Brown .... etc.

This code is executing perfectly if I remove the string variables and values.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • 1
    `'Chihuahua'` strings need double quotes – perivesta Feb 01 '22 at 10:07
  • 7
    Your compiler should be [*screaming* at you](https://godbolt.org/z/e6M9Moefd) about things like `'Chihuahua'`, which is clearly not a single character, yet using single-quotes. – WhozCraig Feb 01 '22 at 10:08
  • Unrelated, but as your `get_data` does not modify the strings you should accept them by `const` reference to avoid unnecessary copies. – Aconcagua Feb 01 '22 at 10:11
  • 1
    Also you should make `breed`, `color` etc. member variables of your class and `get_data` not taking any arguments. Your way `get_data` just prints its arguments and no object related values – Odysseus Feb 01 '22 at 10:14
  • 2
    Side note: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – and much worse: including [`bits/stdc++.h`](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Aconcagua Feb 01 '22 at 10:16
  • Which gender would be `'S'`??? – That's the problem coming with unspecified `char`, better would be defining an enum for: `enum class Gender { Female, Male, Diverse /*???*/ };` – well, we're speaking about animals, they don't know such human problems, so I'd rather go with biological `enum class Sex { Female, Male };`... – Aconcagua Feb 01 '22 at 10:21
  • Finally a rather minor one: What about a cat of age -3? Meaningful in any way? If not, then `unsigned int` expresses this fact (self-documenting code), similarly costs, though it might possibly not unreasonable that one *receives* some money for taking a really bad dog... – Aconcagua Feb 01 '22 at 10:25
  • @WhozCraig `'Chihuahua'` is a multicharacter literal. Refer to description of the fifth form at https://en.cppreference.com/w/cpp/language/character_literal Such a literal has a type `int`, and its value is implementation-defined. Support of multicharacter literals is conditional (which means not all implementations are required to support them) but a number of implementations *do* support them (and therefore do not treat them as a diagnosable error). – Peter Feb 01 '22 at 10:26
  • @Aconcagua I am just starting with OOPS and "using namespace std" shortens the code that I have to write. I have seen many tutorials where it is said that "using name....." is a bad practice but I never faced any issue with that. Also why is including "bits/stdc++" is bad? It just includes all library and if I decide to use one the compiler just automatically assigns the relevant one. Can u pls clarify more? – Iamhere Feb 01 '22 at 10:28
  • @Aconcagua A cat with age `-3` might be born 3 days (or years or decades ...) in the future. Unlike biological cats, modelled cats might have negative ages ;-) – Peter Feb 01 '22 at 10:29
  • warnings and error messages should already tell a lot about what is wrong. Please always include the compiler messages in the question – 463035818_is_not_an_ai Feb 01 '22 at 10:46
  • @Iamhere particularly the combination of the two means that *thousands* of names are now taken. `bits/stdc++` is also an undocumented feature of a particular implementation. Someone trying to help you, who uses a different implementation, without that header, is going to have to add all the includes in. – Caleth Feb 01 '22 at 10:46
  • @Iamhere I'm aware about the convenience you gain – but if you search for long enough you will find enough questions to proving how many trouble you *can* gain because of name clashes and in consequence e.g. overload resolution selecting another function than the one intended to be called. I dare to predict that some day you *will* run into similar trouble (or cause to others if you do that in headers)... – Aconcagua Feb 01 '22 at 11:20

2 Answers2

2

You are mixing single quotes '' and double quotes "".

Unlike other languages, C and C++ treat them differently.

Single quotes are single characters only; double quotes define character arrays (aka C-strings).

To be honest, I'm surprised your code compiles. Usually the compiler tells you, that you cannot define string literals with single quotes.

1

C++ uses " as a string delimiter and ' as a char delimiter. Your compiler should warn you that 'Chihuahua' is surely not correct string. Fix it to "Chihuahua" and the code should work!

whiskeyo
  • 873
  • 1
  • 9
  • 19