PDA

View Full Version : [PLEASE IGNORE, ANSWER FOUND] Very easy C++ question



geo909
June 14th, 2009, 12:39 AM
EDIT:

Please ignore this thread, I found the solution.
Sorry for the spamming!

MadCow108
June 14th, 2009, 12:45 AM
you declare entry and info as local variables of the entry function.
they are deleted when the function ends (variables go out of scope).
So in print they are undefined.

set them as member variables of the entry class.

simeon87
June 14th, 2009, 12:46 AM
In these cases, it's better to leave the question in the thread and post the answer. That way, it can also be found by people who search for the answer on the internet.

Actually, this is better in all cases, regardless of whether you've found the answer yourself or not.

ibuclaw
June 14th, 2009, 01:03 AM
In these cases, it's better to leave the question in the thread and post the answer. That way, it can also be found by people who search for the answer on the internet.

Actually, this is better in all cases, regardless of whether you've found the answer yourself or not.

+1

For historic purposes.

Original:


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

class entry {

public:
int name, info;
entry (){};
entry (char * , char * );
void print();
};

entry::entry(char * a, char * b){
string name(a);
string info(b);
}

void entry::print(void){
cout << name << endl;
cout << info << endl;
}

int main(){
entry a, b;

a = entry("George", "phone: 232962");
a.print();

return 0;
}


My Solution:


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

class entry {

public:
string name, info;
entry (){};
entry (char * , char * );
void print();
};

entry::entry(char * a, char * b){
name = a;
info = b;
}

void entry::print(void){
cout << name << endl;
cout << info << endl;
}

int main(){
entry a, b;

a = entry("George", "phone: 232962");
a.print();

return 0;
}


Regards
Iain

markux^Hs
June 14th, 2009, 03:09 AM
Why do people do that? Delete, or ask to delete their question once it has been answered. Selfishness!

Sinkingships7
June 14th, 2009, 03:55 AM
Why do people do that? Delete, or ask to delete their question once it has been answered. Selfishness!

Indeed. The original question should be left, so that others may benefit from it as well.

JordyD
June 14th, 2009, 03:58 AM
Why do people do that? Delete, or ask to delete their question once it has been answered. Selfishness!

I don't think it's selfishness. They just don't realize how helpful it can be to other people and don't want it to clutter the forums.

markux^Hs
June 14th, 2009, 12:42 PM
I don't think it's selfishness. They just don't realize how helpful it can be to other people and don't want it to clutter the forums.

And that isn't selfish?

simeon87
June 14th, 2009, 12:49 PM
And that isn't selfish?

The first step to solving a problem is to be aware of it. Just because you don't realize a problem, that doesn't mean you're selfish. One can act without knowing that it is a problem for others. If you don't change your behaviour when you've become aware of it, then you're selfish :P

markux^Hs
June 14th, 2009, 12:51 PM
The first step to solving a problem is to be aware of it. Just because you don't realize a problem, that doesn't mean you're selfish. One can act without knowing that it is a problem for others. If you don't change your behaviour when you've become aware of it, then you're selfish :P

Touché :popcorn:

geo909
June 17th, 2009, 02:58 AM
Hello everybody,

I just gave a look at my threads and was surprised to find 9 replies to this one!!!

I made a super stupid mistake (declared as int some variables which were supposed to be strings) and spent one hour trying to find the solution to my problem! I thought nobody would really care about such a thing so I deleted the post without realizing that people had replied.

I apologize if I wasted your time, I didn't really realize that until I read your replies today. I will not do it again

Mirge
June 17th, 2009, 03:24 AM
Hello everybody,

I just gave a look at my threads and was surprised to find 9 replies to this one!!!

I made a super stupid mistake (declared as int some variables which were supposed to be strings) and spent one hour trying to find the solution to my problem! I thought nobody would really care about such a thing so I deleted the post without realizing that people had replied.

I apologize if I wasted your time, I didn't really realize that until I read your replies today. I will not do it again

You can go to "Thread Tools" -> "Subscribe to Thread" if you want an email notification when you get replies. Quite useful, I use it all the time!