Hi all...
I am a new c++ learner.
I'm facing a little problem. according the code below
I try to display the output from the loop...
but no matter how I trying it still can not work....
here is my codes...
anyone can tell me which part am I wrong and how can I fix it back? thx...

#include <iostream>
using namespace std;

class Animal { 
string name;
public:
Animal (string name="") : name(name) {}
void setName(string name) { this->name = name; }
void print() const { cout << name << endl; }
};

int main() {
int size;
string name;
cout << "Animal number ? = ";
cin >> size;

Animal *a;
a = new Animal[size];

for(int i=0; i<size; i++) {
cout << "Animal #" << (i+1) << " = ";
cin >> name;

}

cout << "Animal List" << endl
<< "===========" << endl;
for(int i=0; i<size; i++){

cout << name[i];
}
delete [] a;
return 0;
}

I haven't run it but at first glance I think you want a.name both for when you input and output names. So for input use setName and for output don't refer to name but use a.print();

Thx AkashL... but how with if the situation now is change to
we only can modify these 2 parts for the whole program? hehe ~

Animal *a;
  a = new Animal[];

  
  for(int i=0; i<size; i++) {
    cout << "Animal #" << (i+1) << " = ";
    cin >> name;

  }

I haven't run it but at first glance I think you want a.name both for when you input and output names. So for input use setName and for output don't refer to name but use a.print();

Thx AkashL... but how with if the situation now is change to
we only can modify these 2 parts for the whole program? hehe ~

Animal *a;
a = new Animal[];


for(int i=0; i<size; i++) {
cout << "Animal #" << (i+1) << " = ";
cin >> name;

}

I don't understand the question but hope this helps.

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

class Animal { 
string name;
public:
Animal (string name="") : name(name) {}
void setName(string name) { this->name = name; }
void print() const { cout << this->name << endl; }
};

int main() {
int size;
string name;
cout << "Animal number ? = ";
cin >> size;

Animal *a;
a = new Animal[size];

for(int i=0; i<size; i++) {
cout << "Animal #" << (i+1) << " = ";
cin >> name;
a[i].setName(name);

}

cout << "Animal List" << endl
<< "===========" << endl;
for(int i=0; i<size; i++){

	cout << "Animal# " << (i+1);
	a[i].print();
}
delete [] a;
return 0;
}
commented: Don't just fix errant code and paste it back. Make the OP think about it a little. You should know this by now. +0

You need to pay closer attention to your variable names and what you do or don't have in your code.

There are multiple issues here.
First, in order to do pretty much anything with C++'s std::string type, you need the <string> header. This won't even compile without it because you need operator<<() which is defined in the header. Note, there are also <cstring> and <string.h> headers, neither of these is the correct one, so don't take any liberties with the header name.

Once I #included <string>, this is the output I got from your code.

Animal number ? = 2
Animal #1 = hunt
Animal #2 = peck
Animal List
===========
pe

As you can see, this is not what you want.

The invalid output results from a second issue.
Your usages of name and a within main() are not correct. You input to name, then output from name, but how does that information get in to an Animal? Answer, it doesn't ever get in to an Animal. Your input is fine, but you never send it to an Animal for "permanent" storage, you simply take another input and overwrite your previous input. To correct this, you need to access Animal::setName(). To access it, you need an instance of the class (an Animal object). Where do you get your Animal object? From your array. You would then use the "dot" operator to access the member function. This means your first for loop should be something like this:

for(int i = 0; i < size; ++ i) {
  /* input prompt, etc. */
  a[i].setName(name);  //<---notice the array access and "dot" operator...
}

You have the same issue with your output loop. Modify your output loop similarly to use Animal::print() instead of Animal::setName().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.