Highlighted
New Member.
1597 views

Floating point numbers multiplication in LoadRunner

Hi,

I have written this code to multiply 2 floating point numbers and then saving it into a parameter:

Action()
{

double i;
double q;
double result,output;
double finalOutput;
int decimals=0;
i=atof(lr_eval_string("{UnitPrice}"));
q=atof(lr_eval_string("{Quantity}"));

result=i*q;

sprintf(result,"%2f",decimals);
sprintf(finalOutput,"Value=%2f",result);

//lr_get_attrib_double(result);
lr_output_message("%s",lr_eval_string("{OutPut}"));
    
    return 0;
}

I'm getting this error message: Action.c(14): Error: C interpreter run time error: Action.c (14):  Error -- memory violation : Exception ACCESS_VIOLATION received.

Line 14: sprintf(result,"%2f",decimals);

 

Any inputs/suggestions are highly welcome.

Thanks in advance,

sroy1

 

0 Likes
2 Replies
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: Floating point numbers multiplication in LoadRunner

Hi

There are a couple of issues here:

1. If you want to use a function that do not return an Integer, it needs to be declared first. This means that you need to put the following line as first in vuser_init, as it is now, it multiplies the adresses of the strings. Also atof() returns only the integer part, even if you assign it to a double.

double atof( const char *string);

Next, sprintf() takes as first parameter a character buffer where the result to be written. now you printing somewhere random in memmory and gets an error.

So what is it that you want to accomplish? Multiply two LoadRunner parameters (as floating point) and save the result to new LoadRunner parameter, with 2 decimals?

br /ola


Please mark post as solved if your problems or questions is/are resolved.
If this post was valuable to you, please consider kudo it.

 

.
0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: Floating point number multiplication in LoadRunner

>Action()

 

You need to program to C99 Standards, the return type must be declared as int.

 

> i=atof(lr_eval_string("{UnitPrice}"));

> sprintf(result,"%2f",decimals);

>sprintf(finalOutput,"Value=%2f",result);


Where do you include <stdio.h> and <stdlib.h>?  result and finalOutput aren't char arrays.

 

@OlaBerggren:

>If you want to use a function that does not return an Int, it needs to be declared first.

 

That's not correct.  If you want to use ANY function you must declare it first.  😉

You also shouldn't be making up declarations but should be including the standard C headers:

<stdio.h> & <stdlib.h>

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.