PLEASE HELP ME!!!

THIS IS MY ASSIGNMENT:

"You must create a "struct" named pets. It must have data members

bool m_tail, bool m_fur, bool m_wings, and string m_name

Your struct must not contain any other data.

Your main program must ask user for the name of three of their pets, and ask if it has a tail, fur and wings. The pet names can have more than one word (i.e. Sir George). After asking for the information, the program must output all the data alphabetically according to the pet's first name."

HERES 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 #include <iostream>
#include <string>
#include <sstream>

using namespace std;

struct pets
{
bool m_tail;
bool m_fur;
bool m_wings;
string m_name;

} bool animals[3];

void print_pet(pets animals);

int main()
{

    string mystr;
    bool char m_tail, m_fur, m_wings;
    int n;


    for (n=0; n<3; n++)
    {

    cout<<"What is the name of your pet?  ";
    getline (cin,animals[n].m_name );

    cout<<"Does your pet have a tail?   ";
    getline(cin,animals[n].m_tail );

    cout<<"Does your pet have fur?  ";
    getline (cin,animals[n].m_fur );

    cout<<"Does your pet have wings? ";
    getline(cin, animals[n].m_wings)

    stringstream(mystr)>> animals[n].m_name;

    }
    cout << "\nYou have entered these these pets:  \n";
    for (n=0; n<3; n++)
    print_pet(animals[n]);
    return 0;
    }

     void print_pet (pets animals)
{
     cout <<"Pets Name "<< animals.m_name<<"\n"<<"Tail: "<<animals.m_tail<<"Fur "<<animals.m_fur<<
     " Wings: "<<animals.m_wings<<endl;
  //cout << " (" << movie.year << ")\n";
}



Hello jpoindexter,

Welcome to the forum.

Without putting the code through a compiler, which you should have already done, I can see some problems.

For the struct it starts out OK, but finishes poorly. This may be a personal preference of mine, but I like to use "s_" for a struct and "m_" for a class member.

Line 14. "animals[3]" is a variable name for the struct, but yo do not want to make this variable a "bool".

Line 21. "name" would be a better name for the variable than "mystr". In the end I do not see where it is used yet if at all. You can loose this line since your input is directly into the struct.

Line 22. What is the type a "bool" or a "char"? Choose one because you can not have both.

Lines 26 - 43 The input looks OK for now.

Line 46 is wrong. In the parameter you do not need the "[n]" just the array name.

The for loop for printing is one I will have to test. I think I understand what you are doing. I would have put the for loop in the function not use it to call the function.

By this point you have yet to sort your array to put it in the proper order.

I will have to load up your program and see if there are any other problems.

Hope that helps,

Andy

Thanks for the help!

so should it look like this :

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

struct pets
{
bool m_tail;
bool m_fur;
bool m_wings;
string m_name;

} animals[3];

void print_pet(pets animals);

int main()
{
pets animals[3];

char m_tail, m_fur, m_wings,m_name;
int n;


for (n=0; n<3; n++)
{

cout<<"What is the name of your pet? \n";
cin>> animals[n].m_name;

cout<<"Does "<<animals[n].m_name<<" have a tail? \n";
cin>>animals[n].m_tail;

cout<<"Does "<<animals[n].m_name<<" have fur? \n";
cin>>animals[n].m_fur;

cout<<"Does "<<animals[n].m_name<<" have wings? \n";
cin>> animals[n].m_wings;

}



return 0;

}

void print_pet (pets animals)
{
int n;
for ( n=0; n<3; n++)
{


cout << "\nYou have entered these pets: \n"<<n+1<<endl;

pets print_pet(animals[n]);

}

cout <<"Pets Name: \n"<< animals.m_name<<"\n"<<"Tail: \n"<<animals.m_tail<<"Fur: \n"<<animals.m_fur<<
" Wings: \n"<<animals.m_wings<<endl;
//cout << " (" << movie.year << ")\n";
}



also how would i make the final ouput in alphabetical order? Im really lost with this.
For the alphabetical order part, sorting using the standard library may be an option.
the function getline takes a string as it's second argument,so you cannot write to a bool with getline,you would need an alternative way such as cin >>(be careful of the \n cin leaves in the buffer when mixing with getline() )

Topic archived. No new replies allowed.