 |
04-08-2006, 08:10 PM
|
#1 (permalink)
|
|
In The Zone
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
|
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/
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
04-08-2006, 08:28 PM
|
#2 (permalink)
|
|
Right Off the Assembly Line
Join Date: Jul 2006
Posts: 2
|
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;
|
|
|
04-08-2006, 08:34 PM
|
#3 (permalink)
|
|
In The Zone
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
|
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/
|
|
|
04-08-2006, 09:00 PM
|
#4 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
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
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
|
|
|
04-08-2006, 09:09 PM
|
#5 (permalink)
|
|
In The Zone
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
|
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
|
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/
|
|
|
04-08-2006, 09:18 PM
|
#6 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
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 --
|
|
|
04-08-2006, 09:23 PM
|
#7 (permalink)
|
|
In The Zone
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
|
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/
|
|
|
05-08-2006, 10:40 AM
|
#8 (permalink)
|
|
Beware of the innocent
Join Date: Dec 2005
Posts: 1,024
|
Re: problems in programming language
try casting as z=(int)&i;
Dunno if it will work
__________________
Life is too short. Have fun.
|
|
|
05-08-2006, 11:01 AM
|
#9 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
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 --
|
|
|
09-08-2006, 08:08 PM
|
#10 (permalink)
|
|
In The Zone
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
|
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/
|
|
|
09-08-2006, 08:27 PM
|
#11 (permalink)
|
|
Wise Old Owl
Join Date: May 2005
Location: Chennai, India, Asia, the Earth, the Solar system, the Milky Way, the Local group, this Universe.
Posts: 1,171
|
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
|
|
|
09-08-2006, 08:36 PM
|
#12 (permalink)
|
|
In The Zone
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
|
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/
|
|
|
09-08-2006, 09:27 PM
|
#13 (permalink)
|
|
Wise Old Owl
Join Date: May 2005
Location: Chennai, India, Asia, the Earth, the Solar system, the Milky Way, the Local group, this Universe.
Posts: 1,171
|
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
|
|
|
10-08-2006, 11:14 AM
|
#14 (permalink)
|
|
Beware of the innocent
Join Date: Dec 2005
Posts: 1,024
|
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
|
|
|
12-08-2006, 02:54 PM
|
#15 (permalink)
|
|
In The Zone
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
|
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/
|
|
|
12-08-2006, 10:36 PM
|
#16 (permalink)
|
|
Wise Old Owl
Join Date: May 2005
Location: Chennai, India, Asia, the Earth, the Solar system, the Milky Way, the Local group, this Universe.
Posts: 1,171
|
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
|
|
|
13-08-2006, 07:58 AM
|
#17 (permalink)
|
|
In The Zone
Join Date: Jan 2005
Location: Somewhere in motherboard
Posts: 439
|
Re: problems in programming language
__________________
Game Khelwanu Magazine Padhwanu Majjani Life
http://sudiptatech.blogspot.com/
http://sudiptamondal.blogspot.com/
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|