help ;p

This is a discussion on help ;p within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream.h> #include<conio.h> class myclass { private: int a,b; public: add() { cin>>a; cin>>b; cout<<a+b; } }; int main() { ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    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,479
    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.
    I'm just trying to be a better person - My Name Is Earl

  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,183
    and indent your code

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21