error: expected primary-expression before '.' token

I keep getting an error trying to call my struct, my assignment for this is to create an organized table of information about the given pets. We were given:
Pet pets[5] = { {“Fido”, 7, true}, {“Puppy”, 1, false}, {“Kitty”, 1, false}, {“Brutus”, 11, true}, {“Bilbo”, 15, true} };
Here is what I have so far.
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
#include <iostream>

using namespace std;
struct pet{
    string name;
    int age;
    bool isNeutered;
};
int i;
int printPets() {
    cout << pet.name[i] << "     " << endl;
    cout << pet.age[i] << "     " << endl;
    switch (isNeutered) {
        case true:
            cout << "Yes";
            break;
        case false:
            cout << "No";
            break;
    return 0;
    }
}
int main(){
    pet pets[5] = {{"Fido", 7, true}, {"Puppy", 1, false},{"Kitty", 1, false}, {"Brutus", 11, true}, {"Bilbo", 15, true} };
    printPets();
    cout << "Name       Age     Neutered" << endl << "----------------------------" << endl;
    for (int i=0; i < 0; i++){
        printPets();
    }
}
Last edited on
Read this carefully and you'll be able to see wher the problems were:

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
#include <iostream>

using namespace std;

struct pet{
    string name;
    int age;
    bool isNeutered;
};

void printPets(pet* Pets, int i)
{
    cout << Pets[i].name << "     ";
    cout << Pets[i].age << "     ";
    switch (Pets[i].isNeutered)
    {
        case true:
            cout << "Yes\n";
            break;
        case false:
            cout << "No\n";
            break;
    }
}

int main(){
    pet Pets[5] = {{"Fido", 7, true}, {"Puppy", 1, false},{"Kitty", 1, false}, {"Brutus", 11, true}, {"Bilbo", 15, true} };
    printPets(Pets, 2);
    cout << "Name       Age     Neutered" << endl << "----------------------------" << endl;
    for (int i=0; i < 5; i++){
        printPets(Pets, i);
    }
}
BTW, this is the output. You need to tidy it up a bit by formatting it:


Kitty     1     No
Name       Age     Neutered
----------------------------
Fido     7     Yes
Puppy     1     No
Kitty     1     No
Brutus     11     Yes
Bilbo     15     Yes
Program ended with exit code: 0
Thank you so much. That makes a lot more sense now.
Also

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
#include <iostream>
#include <iomanip>

using namespace std;

struct Pet {
	string name;
	int age {};
	bool isNeutered {};
};

ostream& operator<< (ostream& os, const Pet& p)
{
	static const char* const neut[] {"No", "Yes"};

	return os << p.name << '\t' << p.age << '\t' << neut[p.isNeutered];
}

int main() {
	const Pet pets[] {{"Fido", 7, true}, {"Puppy", 1, false}, {"Kitty", 1, false}, {"Brutus", 11, true}, {"Bilbo", 15, true}};

	cout << pets[2] << "\n\n";

	cout << "Name\tAge\tNeutered\n" << setw(25) << setfill('-') << '\n';

	for (const auto& p : pets)
		cout << p << '\n';
}



Kitty   1       No

Name    Age     Neutered
------------------------
Fido    7       Yes
Puppy   1       No
Kitty   1       No
Brutus  11      Yes
Bilbo   15      Yes

Topic archived. No new replies allowed.