Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 04-08-2006, 08:10 PM   #1 (permalink)
In The Zone
 
Official Techie's Avatar
 
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
Default problems in programming language


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
__________________
Game Khelwanu Magazine Padhwanu Majjani Life

http://sudiptatech.blogspot.com/
http://sudiptamondal.blogspot.com/
Official Techie is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 04-08-2006, 08:28 PM   #2 (permalink)
Right Off the Assembly Line
 
Join Date: Jul 2006
Posts: 2
Default Re: problems in programming language

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;
rohit66_99 is offline  
Old 04-08-2006, 08:34 PM   #3 (permalink)
In The Zone
 
Official Techie's Avatar
 
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
Default Re: problems in programming language

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
__________________
Game Khelwanu Magazine Padhwanu Majjani Life

http://sudiptatech.blogspot.com/
http://sudiptamondal.blogspot.com/
Official Techie is offline  
Old 04-08-2006, 09:00 PM   #4 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: problems in programming language

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;
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 04-08-2006, 09:09 PM   #5 (permalink)
In The Zone
 
Official Techie's Avatar
 
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
Default Re: problems in programming language

Quote:
Originally Posted by Sykora
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;
thank u very much but can i store it in any other type of variable
__________________
Game Khelwanu Magazine Padhwanu Majjani Life

http://sudiptatech.blogspot.com/
http://sudiptamondal.blogspot.com/
Official Techie is offline  
Old 04-08-2006, 09:18 PM   #6 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: problems in programming language

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 .
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 04-08-2006, 09:23 PM   #7 (permalink)
In The Zone
 
Official Techie's Avatar
 
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
Default Re: problems in programming language

i know what pointers do i have already read it in 11th class
__________________
Game Khelwanu Magazine Padhwanu Majjani Life

http://sudiptatech.blogspot.com/
http://sudiptamondal.blogspot.com/
Official Techie is offline  
Old 05-08-2006, 10:40 AM   #8 (permalink)
Beware of the innocent
 
ilugd's Avatar
 
Join Date: Dec 2005
Posts: 1,024
Default Re: problems in programming language

try casting as z=(int)&i;

Dunno if it will work
__________________
Life is too short. Have fun.
ilugd is offline  
Old 05-08-2006, 11:01 AM   #9 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: problems in programming language

Quote:
try casting as z=(int)&i;
That's hilarious. Why are you trying to cast it into an integer? It already _is_ an integer.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 09-08-2006, 08:08 PM   #10 (permalink)
In The Zone
 
Official Techie's Avatar
 
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
Question Re: problems in programming language

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()
__________________
Game Khelwanu Magazine Padhwanu Majjani Life

http://sudiptatech.blogspot.com/
http://sudiptamondal.blogspot.com/
Official Techie is offline  
Old 09-08-2006, 08:27 PM   #11 (permalink)
Wise Old Owl
 
siriusb's Avatar
 
Join Date: May 2005
Location: Chennai, India, Asia, the Earth, the Solar system, the Milky Way, the Local group, this Universe.
Posts: 1,171
Default Re: problems in programming language

^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;
}
__________________
http://myxp.blogspot.com
-----------------------
Winchester 3200+ @2,500MHz
LeadTek 7900GT VOLT MODDED @ 680 core, 1800 mem
2x1GB Transcend DDR400 @ DDR454 2.5,3,3,5,1T
siriusb is offline  
Old 09-08-2006, 08:36 PM   #12 (permalink)
In The Zone
 
Official Techie's Avatar
 
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
Default Re: problems in programming language

no dude its one as the same thing to give or not to give spaces in scope resolution operator
__________________
Game Khelwanu Magazine Padhwanu Majjani Life

http://sudiptatech.blogspot.com/
http://sudiptamondal.blogspot.com/
Official Techie is offline  
Old 09-08-2006, 09:27 PM   #13 (permalink)
Wise Old Owl
 
siriusb's Avatar
 
Join Date: May 2005
Location: Chennai, India, Asia, the Earth, the Solar system, the Milky Way, the Local group, this Universe.
Posts: 1,171
Default Re: problems in programming language

Quote:
Originally Posted by Official Techie
no dude its one as the same thing to give or not to give spaces in scope resolution operator
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.
__________________
http://myxp.blogspot.com
-----------------------
Winchester 3200+ @2,500MHz
LeadTek 7900GT VOLT MODDED @ 680 core, 1800 mem
2x1GB Transcend DDR400 @ DDR454 2.5,3,3,5,1T
siriusb is offline  
Old 10-08-2006, 11:14 AM   #14 (permalink)
Beware of the innocent
 
ilugd's Avatar
 
Join Date: Dec 2005
Posts: 1,024
Default Re: problems in programming language

Quote:
Originally Posted by Sykora
That's hilarious. Why are you trying to cast it into an integer? It already _is_ an integer.
Did you see this @ sykora in the first post?

Quote:
Originally Posted by Official Techie
cannot convert from 'int *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
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;
}
__________________
Life is too short. Have fun.

Last edited by ilugd; 10-08-2006 at 11:31 AM. Reason: Automerged Doublepost
ilugd is offline  
Old 12-08-2006, 02:54 PM   #15 (permalink)
In The Zone
 
Official Techie's Avatar
 
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
Default Re: problems in programming language

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)
__________________
Game Khelwanu Magazine Padhwanu Majjani Life

http://sudiptatech.blogspot.com/
http://sudiptamondal.blogspot.com/
Official Techie is offline  
Old 12-08-2006, 10:36 PM   #16 (permalink)
Wise Old Owl
 
siriusb's Avatar
 
Join Date: May 2005
Location: Chennai, India, Asia, the Earth, the Solar system, the Milky Way, the Local group, this Universe.
Posts: 1,171
Default Re: problems in programming language

Quote:
Originally Posted by Official Techie
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)
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.
__________________
http://myxp.blogspot.com
-----------------------
Winchester 3200+ @2,500MHz
LeadTek 7900GT VOLT MODDED @ 680 core, 1800 mem
2x1GB Transcend DDR400 @ DDR454 2.5,3,3,5,1T
siriusb is offline  
Old 13-08-2006, 07:58 AM   #17 (permalink)
In The Zone
 
Official Techie's Avatar
 
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
Default Re: problems in programming language

thnx the problem is solved
__________________
Game Khelwanu Magazine Padhwanu Majjani Life

http://sudiptatech.blogspot.com/
http://sudiptamondal.blogspot.com/
Official Techie is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:41 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2