Thread: help ;p

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    72

    help ;p

    Code:
    #include<iostream.h>
    #include<conio.h>
    
    class myclass
    {
    private:
    int a,b;
    public:
    add()
    {
    cin>>a;
    cin>>b;
    cout<<a+b;
    }
    
     };
    
     int main()
     {
     myclass obj;
     obj.add();
     return 0;
     }
    whats wrong with the above code;\

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    1. Using non-standard headers iostream.h and conio.h -- just #include <iostream>
    2. No return type declared for the add() method

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    72
    baahahha thanks
    Code:
    #include<iostream.h>
    #include<conio.h>
    
    class myclass
    {
    private:
    int a,b;
    public:
    void add()
    {
    cin>>a;
    cin>>b;
    cout<<a+b;
    }
    
     };
    
     int main()
     {
     myclass obj;
     obj.add();
     return 0;
     }
    forgot the type for ADD, that solved it thanks man ;x

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    You don't even need conio.h anyway.

    Get rid of the .h after iostream ( As Brewbuck suggested ) and use std:: infront of cout and cin.
    Double Helix STL

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Your function Add() does not do its thing: it does not only add two integer values, it also sets the private attributes with their values.

    Why don't you split this in three functions? Two which set 'a' and 'b', and one who adds them.

    This leads to a more understandable main program: we see what we do.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    and indent your code

Popular pages Recent additions subscribe to a feed