Can someone tell me why this code wont work ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnkelly11
    New Member
    • Oct 2011
    • 8

    Can someone tell me why this code wont work ?

    hi

    i almost have this code complete but i am getting 2 errors.
    I am supposed to use function declared inside/outsie class

    Code:
    #include<iostream>
    using namespace std;
    #include<conio.h>
    
    
        class person
             {
             char name[30];
             int age;
             public:
                     void getdata(void); //inside declaration
                     void display(void);
               };
            
              main()
               {
                person p; //object of type person
                p.getdata();
                p.display();
                getche();
                  }
            
              void person::getdata(void)  //outside declaration
                  {
                   cout << "Enter name: ";
                   cin >> name;
                   cout << "Enter age: ";
                   cin >> age;
                     }
              
              void person::display(void)
                   {
                    cout << "\nName: " << name;
                    cout << "\nAge: " << age;
                    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You cannot write a function inside another function.

    There are functions inside main(). Move those outside of main().

    Comment

    • Muhammad Adnan
      New Member
      • Nov 2011
      • 2

      #3
      Your program is mostly correct. The error is in header file. Write the header file iostream like this. #include <iostream.h> and remove the using namespace std. Insha Allah your program will execute with out any error.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        iostream.h is a pre-ANSI C++ header from 1998. It is obsolete.

        The program is correctly includung iostream.

        Also. the std namespace is vital to avoid name clashes between your code and the library. iostream.h did not support namespaces beacuse it is too old.

        Please do not use this old header. Remember, C++ header files do not have extensions. Headers wth .h extensions are for C and not C++.

        Comment

        Working...