Page 1 of 3 123 LastLast
Results 1 to 10 of 22
  1. #1
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    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...!

  2. #2
    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;
    }

  3. #3
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    aw... please read and anlayse review your pointers topic and how to point memory location properly.
    horrible ---> cDriver* d;

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

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

    well, back to java now.

  5. #5
    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;
    }

  6. #6
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    @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...

  7. #7
    ...its only accidental...i'm still wondering why it won't run if using pointers (e.g cDriver* pD)...

  8. #8
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    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.

  9. #9
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    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.

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

    With a big help from Stack Overflow.

  11.    Advertisement

Page 1 of 3 123 LastLast

Similar Threads

 
  1. Looking For: P.regalis the smaller and cheaper the better
    By kingnat_revivingcenter in forum Pets
    Replies: 4
    Last Post: 04-22-2010, 09:28 PM
  2. ESCAPE THE FATE and BLESS THE FALL
    By achilles127 in forum Music & Radio
    Replies: 5
    Last Post: 12-20-2009, 08:39 PM
  3. What Is The Problem With The Philippines and Your Solution?
    By slimer in forum General Discussions
    Replies: 58
    Last Post: 06-24-2008, 08:09 AM
  4. Laws are imposed upon the poor and not the rich
    By iloveyou4ever in forum Politics & Current Events
    Replies: 9
    Last Post: 06-05-2006, 12:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top