My teacher asked me to simply create a scenario in C++ that uses an array of objects.
Well, i followed everything by book yet weird problem appears. What is wrong? My take is that the dynamic addressing part has problem. What is the problem? Thanks in advance.
Code:
#include<iostream>
#include<string>
using namespace std;
class kill
{
string name;
public:
void inName(string name);
string outName();
};
void kill::inName(string name)
{
this->name=name;
}
string kill::outName()
{
return name;
}
int main()
{
int size;
kill* peopleyouhate;
cout<<"Insert the amount of people you wanna terminate"<<endl;
cin>>size;
peopleyouhate= new kill[size];
for(int i=0; i<size; i++)
{
string name;
cout<<"Insert the name of the person you wanna terminate"<<endl;
cin>>name;
peopleyouhate[i].inName(name);
}
cout<<"These are the people you wanna terminate, there is no more turning back..."<<endl;
for(int j=0; j<size; j++)
{
cout<<peopleyouhate[j].outName<<endl;
}
return 0;
}
WOW! THAT'S IT? Wow....just wow!!! I am seriously sorry for wasting your time. By the way, is my way of creating a dynamic memory correct because when it comes to pointer and all i am not very confident. Also, i modified the program so that it can take in the name "John Connor" without error by putting in a getline code but there seems to be a looping problem which i do not understand. Mind checking it out? Thank you for your time spent on answering my menial question. I understand some of you guys are programming gurus and have better and more important things to do. I am truly sorry if i wasted any of your time.
Code:
#include<iostream>
#include<string>
using namespace std;
class kill
{
string name;
public:
void inName(string name);
string outName();
};
void kill::inName(string name)
{
this->name=name;
}
string kill::outName()
{
return name;
}
int main()
{
int size;
kill* peopleyouhate;
cout<< "Insert the amount of people you wanna terminate "<<endl;
cin>>size;
peopleyouhate= new kill[size];
for(int i=0; i<size; i++)
{
string name;
cout<<"Insert the name of the person you wanna terminate"<<endl;
getline(cin,name);
peopleyouhate[i].inName(name);
}
cout<<"These are the people you wanna terminate, there is no more turning back..."<<endl;
for(int j=0; j<size; j++)
{
cout<< peopleyouhate[j].outName() <<endl;
}
return 0;
}
Woops sorry, i seemed to have caused some misunderstanding. What i meant was that the getline function causes the first loop not to accept any value. Try compiling it and you will understand.
Wow !That's something new! Ok it works but if that is your explanation then this should not work right?
cin>>a;
cin>>b;
Because the second cin will execute the /n from the first cin?
Bookmarks