problems in programming language

Status
Not open for further replies.

Official Techie

In the zone
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
 

rohit66_99

Right off the assembly line
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;
 
OP
Official Techie

Official Techie

In the zone
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
 

Sykora

I see right through you.
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;
 
OP
Official Techie

Official Techie

In the zone
Sykora said:
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
 

Sykora

I see right through you.
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 :) .
 
OP
Official Techie

Official Techie

In the zone
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()
 

siriusb

Cyborg Agent
^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;
}
 

siriusb

Cyborg Agent
Official Techie said:
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.
 

ilugd

Beware of the innocent
Sykora said:
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?

Official Techie said:
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;
}
 
Last edited:
OP
Official Techie

Official Techie

In the zone
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)
 

siriusb

Cyborg Agent
Official Techie said:
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.
 
Status
Not open for further replies.
Top Bottom