problems in programming language

Discussion in 'Programming' started by Official Techie, Aug 4, 2006.

Thread Status:
Not open for further replies.
  1. Official Techie

    Official Techie New Member

    Joined:
    Jan 25, 2005
    Messages:
    439
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Somewhere in motherboard
    i want to know whats the problem i want to print the address of a initialised variable

    #include<iostream.h>
    int main()
    {
    int i;
    int z;
    z = &i;
    cout<<"the address"<<z;
    cout<<"\n";
    cout<<"the value is"<<i;
    return i;
    }


    the error

    --------------------Configuration: a program of value and address - Win32 Debug--------------------
    Compiling...
    a program of value and address.cpp
    D:\Documents and Settings\Sudipta\school\c++\a program of value and address.cpp(6) : error C2440: '=' : cannot convert from 'int *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    Error executing cl.exe.

    a program of value and address.obj - 1 error(s), 0 warning(s)



    i want to know about stack functions constructors destructors and friend function and how to use inheritance
     
  2. rohit66_99

    rohit66_99 New Member

    Joined:
    Jul 28, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    since adress of any variable is in hexadeciamal form
    so to store these special characters u need special variables.
    to store any adress u need to initialise as follows:
    #include<iostream.h>
    int main()
    {
    int i;
    int *z;//these r known as pointers which store values of adress
    z = &i;
    cout<<"the address"<<z;
    cout<<"\n";
    cout<<"the value is"<<i;
    return i;
     
  3. OP
    OP
    Official Techie

    Official Techie New Member

    Joined:
    Jan 25, 2005
    Messages:
    439
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Somewhere in motherboard
    i know the use of pointers but that would be taught later i want to display the memory location of " i " I know that by using pointers its very easy but then i cannot use it
     
  4. Sykora

    Sykora I see right through you.

    Joined:
    Sep 10, 2005
    Messages:
    597
    Likes Received:
    4
    Trophy Points:
    0
    Location:
    Chennai
    Your problem is that you're trying to find the address of a variable, but if you store that address itself in a variable, it's a pointer, and you can't use it. Isn't that kind of stupid?

    Anyway, there's a fairly simple way to get what you want: Don't store the address in a variable. Send it straight to the output stream using

    Code:
    cout << &i;
     
    Official Techie likes this.
  5. OP
    OP
    Official Techie

    Official Techie New Member

    Joined:
    Jan 25, 2005
    Messages:
    439
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Somewhere in motherboard
    thank u very much but can i store it in any other type of variable
     
  6. Sykora

    Sykora I see right through you.

    Joined:
    Sep 10, 2005
    Messages:
    597
    Likes Received:
    4
    Trophy Points:
    0
    Location:
    Chennai
    Of course not. It is, after all an address, a hexadecimal *integer*. Variables which store addresses of other variables are called pointers, so you'll have to wait till you're taught that to use them :) .
     
  7. OP
    OP
    Official Techie

    Official Techie New Member

    Joined:
    Jan 25, 2005
    Messages:
    439
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Somewhere in motherboard
    i know what pointers do i have already read it in 11th class
     
  8. ilugd

    ilugd Beware of the innocent

    Joined:
    Dec 25, 2005
    Messages:
    1,009
    Likes Received:
    3
    Trophy Points:
    0
    try casting as z=(int)&i;

    Dunno if it will work
     
  9. Sykora

    Sykora I see right through you.

    Joined:
    Sep 10, 2005
    Messages:
    597
    Likes Received:
    4
    Trophy Points:
    0
    Location:
    Chennai
    That's hilarious. Why are you trying to cast it into an integer? It already _is_ an integer.
     
  10. OP
    OP
    Official Techie

    Official Techie New Member

    Joined:
    Jan 25, 2005
    Messages:
    439
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Somewhere in motherboard
    guys i have a new problem

    #include<iostream.h>

    class employee
    {
    int code, telephone;
    char address[50], name[50];
    public :
    void getemployeedata(void);
    void showemployeedata(void);
    };
    class employee :: getemployeedata(void)
    {
    cout<<"\n Enter employee name";
    cin.getline(name, 50);
    cout<<"\n Enter employee code";
    cin>>code;
    cout<<"\n Enter employee address";
    cin.getline(address, 50);
    cout<<"\n Enter employee contact number";
    cin>>telephone;
    }

    class employee :: showemployeedata(void)
    {
    cout<<"\n The entered information of the employee is";
    cout<<"\n Name : - "<<name;
    cout<<"\n Employee code"<<code;
    cout<<"\n Employee address"<<address;
    cout<<"\n Contact number "<<telephone;
    }

    int main()
    {
    employee emp;
    emp.getemployeedata();
    emp.showemployeedata();

    return 0;
    }


    --------------------Configuration: Employee Program - Win32 Debug--------------------
    Compiling...
    Employee Program.cpp
    D:\Documents and Settings\Sudipta\school\c++\Employee Program.cpp(40) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

    Employee Program.obj - 1 error(s), 0 warning(s)


    what the error is i want to know since i cannot see any what the hell it is

    and can we declare objects just before terminating the class or we can only declare it in the main prog after int main()
     
  11. siriusb

    siriusb New Member

    Joined:
    May 12, 2005
    Messages:
    1,171
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    Chennai, India, Asia, the Earth, the Solar system,
    ^Replace the appropriate sections with this:
    Code:
    void employee::getemployeedata(void)
    {
    cout<<"\n Enter employee name";
    cin.getline(name, 50);
    cout<<"\n Enter employee code";
    cin>>code;
    cin.get();
    cout<<"\n Enter employee address";
    cin.getline(address, 50);
    cout<<"\n Enter employee contact number";
    cin>>telephone;
    }
    
    void employee::showemployeedata(void)
    {
    cout<<"\n The entered information of the employee is";
    cout<<"\n Name : - "<<name;
    cout<<"\n Employee code"<<code;
    cout<<"\n Employee address"<<address;
    cout<<"\n Contact number "<<telephone;
    }
     
  12. OP
    OP
    Official Techie

    Official Techie New Member

    Joined:
    Jan 25, 2005
    Messages:
    439
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Somewhere in motherboard
    no dude its one as the same thing to give or not to give spaces in scope resolution operator
     
  13. siriusb

    siriusb New Member

    Joined:
    May 12, 2005
    Messages:
    1,171
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    Chennai, India, Asia, the Earth, the Solar system,
    I thought you'd spot more differences than that. Ok, let me tell you:
    -You don't use the 'class' keyword to define class functions. U use the function's return type (here it was 'void')
    -After accepting the emp code, you have to flush the newline char so that address can be got in the variable.
     
  14. ilugd

    ilugd Beware of the innocent

    Joined:
    Dec 25, 2005
    Messages:
    1,009
    Likes Received:
    3
    Trophy Points:
    0
    Did you see this @ sykora in the first post?

    Anyway it was just a suggestion.

    @syriusb's suggestion looks good to go.
    these will be in the function definitions

    void employee :: getemployeedata(void)
    void employee :: showemployeedata(void)

    instead of

    class employee :: getemployeedata(void)
    class employee :: showemployeedata(void)

    It compiled and ran on devcpp after this change. But you need to look in syriusb's second suggestion as the program only asks for name and code.

    Hey, just checked out the first program now. It compiled and ran perfectly fine. So I was right after all (though it was just a guess.) Here is the code

    #include<iostream.h>
    int main()
    {
    int i;
    int z;
    z = (int)&i;
    cout<<"the address"<<z;
    cout<<"\n";
    cout<<"the value is"<<i;
    system("pause");
    return i;
    }
     
    Last edited: Aug 10, 2006
  15. OP
    OP
    Official Techie

    Official Techie New Member

    Joined:
    Jan 25, 2005
    Messages:
    439
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Somewhere in motherboard
    there is no problem withn the first problem i have with the class and we use class keyword to connect public variables to a function through scope resolution so its right


    class employee :: getemployeedata(void)
    class employee :: showemployeedata(void)
     
  16. siriusb

    siriusb New Member

    Joined:
    May 12, 2005
    Messages:
    1,171
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    Chennai, India, Asia, the Earth, the Solar system,
    I didn't understand what you wrote, but defining class functions outside the class using the 'class' keyword is not correct. Why do you say it's right (if that's what you said)?
    Even for referring to a class variable (which is done inside a function body), you don't need a 'class' keyword.
     
  17. OP
    OP
    Official Techie

    Official Techie New Member

    Joined:
    Jan 25, 2005
    Messages:
    439
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Somewhere in motherboard
    thnx the problem is solved :)) :)) :)) :))
     
Thread Status:
Not open for further replies.

Share This Page