Hello,
I have a function which calculates time for a specific operation and prints the time...
However i want to call the time in another function and display its value...
The code is as follows:-

int encrypt()
{
double encryptTime,Add,sub,sft,mix;
// add + sub + sft + mix calculations
encryptTime = Add + sub + sft + mix;
printf("Encrytion Time : ");
printf("%lf ", encryptTime);
printf("seconds.");
printf("\n\n");
return 0;
}

I am getting the correct time...that is its working..The output is as follows:-

0.000142 seconds

Now i have to call this encryptTime from another function:-

double dTime()
{
	double encryptTime;
	return (encryptTime);
}

I am getting the time as:-

-9.25596e+061 seconds

In the main function

int main()
{
encrypt();
dTime();
return 0;
}

Can anyone help how we can solve this problem...

From what i have understood u want to get the time again from the dTime() function.

firstly, if u want the same time that was calculated in the encrypt() function to be displayed u have to remove the declaration of encryptTime from dTime().
the wrong value u are getting is becuase ur declaring it again and returning it. you shld declare the encryptTime variable outside the functions and then pass it as an argument to dTime() : dTime(double encryptTime){ return (encryptTime);}

secondly, if u want the time to be caculated again call the encrypt() inside the dTime(). but for this u'll have to pass all the values required for caculation to dTime()

Two things wrong with dTime
1. You're returning the value of an uninitialised variable.
2. You're ignoring that result (such as it is) in main.

Hello,
I have changed the codes..
I have declared the times outside the functions..

double encryptTime,Add,sub,sft,mix;
int encrypt()
{
// add + sub + sft + mix calculations
encryptTime = Add + sub + sft + mix;
printf("Encrytion Time : ");
printf("%lf ", encryptTime);
printf("seconds.");
printf("\n\n");
return 0;
}
double dTime()
{
return (encryptTime);
}

Now in the main function i am calling both functions..

int main()
{
encrypt();
double d;
d=dTime();
cout<<d;
return 0;
}

The code is working now and i am getting the correct time that is 0.0000145869 seconds instead of 0.000146 seconds...and i want it to return 0.000146 seconds..

Hello,
The code is working correctly...and i am getting 0.000146 seconds..
Thanks for the help scaraffe and Salem...

Hello,
I have to return the text from a file...That is open a file read all the contents in the file and return the all the contents/data...
I am using the code...

const char* DisplayCipherText()
{	char* plaintext;
	ifstream openPlaintext ("Ciphertext.txt");
	openPlaintext.read(plaintext, 100);
	return (plaintext);
}

int main()
{
const char* rt = DisplayCipherText();
cout<<rt;
return 0;
}

I am unable to get all the data from the file....Can anyone help how we can solve it...

> char* plaintext;
Use a std::string to store the information, rather than a bare char pointer which isn't allocated any memory.

Hello,
In fact i am calling the c++ code as a dll in VB, so therefore i am catering for both c++ and VB variables when using the c++ codes..

char sk[4][4];
double _stdcall Decrypt_Text(int key_decrypt)
{for ( int j=0; j<4; j++)
	for (int i=0; i < 4; i++)
	sk[i][j] = '0';
return 0.0;
}
const char * _stdcall DisplayKey()
{
return ((const char *)sk);
}

Using the main to test the code:=

int main()
{	const char * yy = DisplayKey();
	cout<<yy;
	return 0;
}

The output that i am getting is:= 0000
The expected output that i need to get is: 0000000000000000
Can anyone help how we can solve the problem

Change

char sk[4][4];

to

char sk[16];

Hello,
sk is a 2d array and its size is 16 and i have to maintain it as:-

sk[4][4];

Is there any other way to solve it...

Hello,
I have another problem...
I am using a function to open a file and read all the contents in that file and i have to return all the contents...
The code is:=

const char * _stdcall DisplayCipherText()
{
	ifstream openPlaintext ("Ciphertext.txt", ios::binary);
	char plaintext[16];	
	openPlaintext.read((char *)plaintext,16);
	return (plaintext);
}

I am testing the code in the main()

int main()
{
const char * rt;
rt = DisplayCipherText();
cout<<rt;
return 0;
}

I am not getting the results...
Can anyone help how we can solve these 2 problems...

>>I am not getting the results
Not supprised because you can't return the array like that because it will disappear as soon as the function returns. There are a couple of ways to solve that problem:

1) add a parameter and have the calling program pass the input buffer

const char * _stdcall DisplayCipherText(char plaintext[16])
{
	ifstream openPlaintext ("Ciphertext.txt", ios::binary);
	openPlaintext.read(plaintext,16);
	return (plaintext);
}

int main()
{
   char plaintext[16];
const char * rt;
rt = DisplayCipherText(plaintext);
cout<<rt;
return 0;
}

Or allocate the buffer

const char * _stdcall DisplayCipherText()
{
    ifstream openPlaintext ("Ciphertext.txt", ios::binary);
    char* plaintext = new char[16];
    openPlaintext.read(plaintext,16);
    return (plaintext);
}

int main()
{
const char * rt;
rt = DisplayCipherText();
cout<<rt;
delete[] rt;
return 0;
}

If you want to allocate space for any size file then call seekg() to move the file pointer to end of file and tellg() to get current position -- that will give you the file size.

commented: Just brilliant and very helpful +1

Hello,
I have modified the code to cater for file size:-

const char * _stdcall DisplayCipherText()
{
	ifstream openPlaintext ("Ciphertext.txt", ios::binary);
	int size;
	openPlaintext.seekg (0, ios::end);
	size = openPlaintext.tellg();
	openPlaintext.seekg( 0, ios::beg );

	char* plaintext = new char[size];
    openPlaintext.read(plaintext,size);
    return (plaintext);
}

The text in Ciphertext.txt = ¯õ#;‘ƒ9s„‡Ñ'Ý¢@ (16 bytes)
The output i am getting on the VB interface = ¯õ#;‘ƒ9s„‡Ñ'Ý¢@ýýýýÝ (21 bytes)
What can cause the problem...

check to see if openPlaintext was actually opened

if( !openPlaintext.is_open() )
{
   cout << "Error";
}

Otherwise, post the file contents. You are opening the file in binary mode so I would expect the file to contain unreadable stuff. Open it in Notepad and see if you get the same sort of thing.

The output i am getting on the VB interface = ¯õ#;‘ƒ9s„‡Ñ'Ý¢@ýýýýÝ (21 bytes)
What can cause the problem...

There is no terminating null byte in the string, allocate 1 byte more and terminate the string yourself, e.g.

char* plaintext = new char[size + 1];
plaintext[size] = 0;
commented: Very helpful +1

Hello mitrmkar,
Yes it works....
The correct code is as follows:=

const char * _stdcall DisplayCipherText()
{
	ifstream openPlaintext ("Ciphertext.txt", ios::binary);
	if( !openPlaintext.is_open() )
	{   cout << "Error";
	}
	int size;
	openPlaintext.seekg (0, ios::end);
	size = openPlaintext.tellg();
	openPlaintext.seekg( 0, ios::beg );
	//char* plaintext = new char[size];
	char* plaintext = new char[size + 1];
	plaintext[size] = 0;
                openPlaintext.read(plaintext,size);
                return (plaintext);	
}

Hello,
I have 1 problem left with...
I have to return the key defined: sk[4][4]

char sk[4][4];
double _stdcall Decrypt_Text(int key_decrypt)
{
     for ( int j=0; j<4; j++)
     for (int i=0; i < 4; i++)	
       sk[i][j] = '0';
     return 0.0;
}

const char * _stdcall DisplayKey()
{      return ((const char *)sk);
}

I am testing the code in main:=

int main()
{   const char * yy = DisplayKey();
     cout<<yy;	
     return 0;
}

The output that i am getting is:= 0000
The expected output that i need to get is: 0000000000000000
Can anyone help how we can solve the problem

you can not treat that array like a normal null-terminated string, because it isn't. If you do get the output you want then it will be only by pure accident.

But I ran your program with VC++ 2008 Express and got the output you wanted, luckly. I had to add a line to main(). Since the string is not null-terminated it could just as easily have printed a lot of garbage after thse 16 '0's.

char sk[4][4];
double _stdcall Decrypt_Text(int key_decrypt)
{
     for ( int j=0; j<4; j++)
     for (int i=0; i < 4; i++)	
       sk[i][j] = '0';
     return 0.0;
}

const char * _stdcall DisplayKey()
{      return ((const char *)sk);
}

int main()
{
    Decrypt_Text(0);
    const char * yy = DisplayKey();
     cout<<yy << "\n";	
     return 0;
}

Hello,
I am able to solve the problem....I have saved the sk in a file and then retrieved all the contents from the file and then display them in the VB interface...
Thnks to all the persons who have helped me in one way or another to solve the problems in this thread and many other threads in the forum...
A special thanks goes to Ancient Dragon and mitrmkar
Very helpful keep the threads going on...Good luck...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.