iSTORYA.NET

Go Back   iSTORYA.NET > Science & Technology > Software & Games > Programming
: :

Register FAQ Members List Calendar Mark Forums Read

Programming :: Programming related discussions ::

Reply
LinkBack Thread Tools
  #1  
Old 10-13-2009, 03:40 PM
Senior Member
MarkCuering is online now
Join Date: Aug 2008
Posts: 600
Blog Entries: 1
Default C++ (Find the errors and Solve the problem)

Check the code below... please provide explaination

Code:
#include <iostream>
using namespace std;

// Leave this class unchanged.
class cDriver {
    public:
        int age;
        int license;
    };

// you can edit this function.
cDriver* myFunc() {
    cDriver D;
    cDriver* pD = &D;
    pD->age = 27;
    return pD;
}
// you can edit below main, with the desired output.
int main(int argc, char *argv[])
{    
    cDriver* d;    
    d = myFunc(); 
    
    cout << d->age << endl;
    cout << "Display Age: " << d->age << endl;
    d->age = 27;  
    d->license = 4413;  

    cout << "age: " << d->age << endl;
    cout << "gpa: " << d->license << endl;
      
    system("PAUSE");
    return 0;
}
sample output from above code:
Code:
27
Display Age: 0
age: 0
gpa: 104724280
Press any key to continue . . .
Desired output:
Code:
27
Display Age: 27
age: 27
gpa: 4413
Press any key to continue . . .

I don't know what's the difficulty level of this problem, just try to solve it.

GOODLUCK...!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 10-13-2009, 06:07 PM
Junior Member
Badekdek is offline
Badekdek's Avatar
Join Date: Jan 2009
Gender: Male
Posts: 377
Default

int main(int argc, char *argv[])
{
cDriver* d;
d->age = 27;
d->license = 4413;

d = myFunc();

cout << d->age << endl;
cout << "Display Age: " << d->age << endl;


cout << "age: " << d->age << endl;
cout << "gpa: " << d->license << endl;

system("PAUSE");
return 0;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 10-13-2009, 06:34 PM
Senior Member
MarkCuering is online now
Join Date: Aug 2008
Posts: 600
Blog Entries: 1
Default

aw... please read and anlayse review your pointers topic and how to point memory location properly.
horrible ---> cDriver* d;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 10-14-2009, 01:50 PM
Junior Member
Badekdek is offline
Badekdek's Avatar
Join Date: Jan 2009
Gender: Male
Posts: 377
Default

nice try for me. i dont do c++ actually.

maybe this one:
d = malloc(sizeof(cDriver));

well, back to java now.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 10-14-2009, 03:28 PM
Junior Member
nullpointer is offline
nullpointer's Avatar
Join Date: Sep 2008
Posts: 83
Default

Code:
int main(int argc, char *argv[])
{    
    cDriver d;
    cDriver* pd = myFunc();
    d = *pd;

    cout << d.age << endl;
    cout << "Display Age: " << d.age << endl;
    d.age = 27;  
    d.license = 4413;
    cout << "age: " << d.age << endl;	 
    cout << "gpa: " << d.license << endl;
    system("PAUSE");
    return 0;
}

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 10-14-2009, 04:53 PM
Senior Member
MarkCuering is online now
Join Date: Aug 2008
Posts: 600
Blog Entries: 1
Default

@nullpointer,

you did a great job hope you can explain a bit., :P is it accidentally discover? hoping your honest share 2-3 of my collegues code it that why, BUT, there's still an issue right there, still had some bugs and overhead.

I wonder why you didn't use the "->" operator. can you resolve using both (dot) and "->" operator?

eg.

cout << d.age;
cout << pd->age;

again, nice try...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 10-14-2009, 05:03 PM
Junior Member
nullpointer is offline
nullpointer's Avatar
Join Date: Sep 2008
Posts: 83
Default

...its only accidental...i'm still wondering why it won't run if using pointers (e.g cDriver* pD)...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 10-14-2009, 05:08 PM
Senior Member
MarkCuering is online now
Join Date: Aug 2008
Posts: 600
Blog Entries: 1
Default

Quote:
Originally Posted by Badekdek View Post
nice try for me. i dont do c++ actually.

maybe this one:
d = malloc(sizeof(cDriver));

well, back to java now.
can you share your complete code? this might open a new topic, maybe you got some idea of using malloc, you're working with JAVA? I thought you will gonna use the one that specify the number of objects to allocate instead of bytesize to allocate. AFAIK, malloc returns a NULL in case there's no room for data to be allocated.

you almost got it anyway on this kind of solution.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old 10-14-2009, 05:15 PM
Senior Member
MarkCuering is online now
Join Date: Aug 2008
Posts: 600
Blog Entries: 1
Default

Quote:
Originally Posted by nullpointer View Post
...its only accidental...i'm still wondering why it won't run if using pointers (e.g cDriver* pD)...
hahahaha... yes it can't be helped..., its kinda tricky, but its really nice thing to workout. surely you will hit this over and over again if ever you will forget this rule.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10  
Old 10-14-2009, 08:22 PM
Elite Member
doomsweek is offline
doomsweek's Avatar
Join Date: Aug 2003
Posts: 1,009
Default

I don't know C++ but know a little C and Java. Here's my solution.

With a big help from Stack Overflow.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11  
Old 10-14-2009, 10:10 PM
Junior Member
Badekdek is offline
Badekdek's Avatar
Join Date: Jan 2009
Gender: Male
Posts: 377
Default

Quote:
Originally Posted by MarkCuering View Post
can you share your complete code? this might open a new topic, maybe you got some idea of using malloc, you're working with JAVA? I thought you will gonna use the one that specify the number of objects to allocate instead of bytesize to allocate. AFAIK, malloc returns a NULL in case there's no room for data to be allocated.

you almost got it anyway on this kind of solution.
i dont have one. in fact, i dont have a c++ compiler
i thought this is a guessing game! lewlz. shame on me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12  
Old 10-15-2009, 07:18 AM
Senior Member
MarkCuering is online now
Join Date: Aug 2008
Posts: 600
Blog Entries: 1
Default

Quote:
Originally Posted by doomsweek View Post
I don't know C++ but know a little C and Java. Here's my solution.

With a big help from Stack Overflow.
There you go... that explains everything anyone could summarize the lesson learned? or anyone can give using malloc?
Last edited by MarkCuering; 10-15-2009 at 07:21 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13  
Old 10-17-2009, 02:53 PM
Senior Member
MarkCuering is online now
Join Date: Aug 2008
Posts: 600
Blog Entries: 1
Default

hmm... it seems nobody is doing C++ project ^^
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14  
Old 10-20-2009, 03:47 PM
Junior Member
Burn Out is offline
Join Date: Mar 2007
Gender: Female
Posts: 34
Default

hirap talaga ng C++
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15  
Old 10-20-2009, 03:55 PM
Senior Member
MarkCuering is online now
Join Date: Aug 2008
Posts: 600
Blog Entries: 1
Default

some people don't want to eat apples, but some make it a living
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
A hard headed boy, know how to solve his problem? mr_kyme Family Matters 21 09-18-2009 09:27 AM
solve weight problem...i can help you through tabsy Fitness & Health 3 06-12-2006 07:18 PM
VB gurus... I need your expertise... problem solve afortaliza Programming 14 05-23-2006 09:38 PM
PROBLEM SOLVE ****mods pls close**** koto Computer Hardware 5 12-27-2005 08:42 PM


All times are GMT +8. The time now is 02:31 PM.


Powered by vBulletin® Version 3.7.6
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
(c) 2002-2009 iSTORYA.NET | Design by DrE | Modifications by BeoR