-
July 3rd, 2010, 08:50 AM
#1
Halp with my program?
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;
}
-
July 3rd, 2010, 08:59 AM
#2
Re: Halp with my program?
cout << peopleyouhate[j].outName() << endl;
Get in the habit of using white space. It'll make your program more readable.
-
July 3rd, 2010, 09:25 AM
#3
Re: Halp with my program?
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;
}
-
July 3rd, 2010, 09:36 AM
#4
Re: Halp with my program?
Wasn't as waste of time. You put the effort in and just made a trivial mistake. Believe it or not, we all do something similar from time to time.
Your allocation is fine, just don't forget for every new, there should be a delete.
-
July 3rd, 2010, 10:03 AM
#5
Re: Halp with my program?
Thanks but is still have problem with the getline function. The getline function did not seem to prompt me to insert a value....
-
July 3rd, 2010, 10:05 AM
#6
Re: Halp with my program?
Originally Posted by hayloiuy
Thanks but is still have problem with the getline function. The getline function did not seem to prompt me to insert a value....
All getline does is read input from the console. If you want to give the user any kind of prompt you have to do it with cout.
-
July 3rd, 2010, 10:40 AM
#7
Re: Halp with my program?
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.
-
July 3rd, 2010, 12:16 PM
#8
Re: Halp with my program?
This is a very commom error for beginners.
The reason is that you're mixing input methods.
The stream extractors stop reading when they find a whitespace -> leaving the final '\n' in the buffer
Code:
string name;
cout<<"Insert the name of the person you wanna terminate"<<endl;
getline(cin,name);
getline stops reading when it finds the terminating character ( by default '\n' ) that's why an empty string is returned.
solution: put
after the use of the stream extractor.
Kurt
-
July 3rd, 2010, 08:16 PM
#9
Re: Halp with my program?
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?
-
July 3rd, 2010, 08:21 PM
#10
Re: Halp with my program?
Oh yeah, thanks by the way.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|