a very simple question, pls help a new programmer

M

Marco

I used to write program in Matlab. I have no experience in C++ at all.
But my boss order me to convert a matlab function to C++ function, so
he can use it with his program. For the concept of the program, it
receive a new input and keep some variable in memory to give a output.
Could you please tell me what mistake I have make in the program?

Thanks


#include <stdio.h>
#include <math.h>


class CGripperHysteresis {

private:

public:
CGripperHysteresis( );
void SetThreshold( double threshold);
double CalculateOutput( CGripperHysteresis, double input_data);
};

CGripperHysteresis::CGripperHysteresis( )
{
double m_last_update_side = 0;
double m_last_on_curve_value = 0;
double m_threshold = 0;
double output_data = 0;
double input_data = 0;


}

void CGripperHysteresis::SetThreshold( double threshold)
{
m_threshold = threshold;
}




double CGripperHysteresis::CalculateOutput(CGripperHysteresis, double
input_data)
{
double output_data_temp;

if (m_last_update_side == 0 && m_last_on_curve_value == 0)
{
if (input_data <= threshold){
output_data = 0;
m_last_update_side = 0;
m_last_on_curve_value = 0;}
else if (input_data > threshold){
output_data = (input_data-threshold)/(1-threshold) ;
m_last_update_side = 0;
m_last_on_curve_value = output_data;
}
}
else if (m_last_update_side == 0 && m_last_on_curve_value > 0 &&
m_last_on_curve_value <1)
{
output_data_temp = (input_data-threshold)/(1-threshold);
if (output_data_temp >= m_last_on_curve_value)
{
output_data = output_data_temp;
m_last_update_side = 0;
m_last_on_curve_value = output_data_temp;
}
else if (output_data_temp < m_last_on_curve_value && input_data >
m_last_on_curve_value*(1-threshold))
{
output_data = m_last_on_curve_value;
m_last_update_side = 0;
m_last_on_curve_value = m_last_on_curve_value;
}
else if (output_data_temp < m_last_on_curve_value && input_data <=
m_last_on_curve_value*(1-threshold))
{
output_data = m_last_on_curve_value;
m_last_update_side = 1;
m_last_on_curve_value = output_data;
}
}
else if (m_last_update_side == 0 && m_last_on_curve_value >= 1)
{
if (input_data >= (1 - threshold))
{
output_data = 1;
m_last_update_side = 0;
m_last_on_curve_value = 1;
}
else if (input_data < (1 - threshold))
{
output_data = (input_data)/(1-threshold) ;
m_last_update_side = 1;
m_last_on_curve_value = output_data;
}
}
else if (m_last_update_side == 1 && m_last_on_curve_value <= 1 &&
m_last_on_curve_value > 0)
{
output_data_temp = (input_data)/(1-threshold);
if (output_data_temp <= m_last_on_curve_value)
{
output_data = output_data_temp;
m_last_update_side = 1;
m_last_on_curve_value = output_data_temp;
}
else if (output_data_temp > m_last_on_curve_value && input_data <
m_last_on_curve_value*(1-threshold) + threshold)
{
output_data = m_last_on_curve_value;
m_last_update_side = 1;
m_last_on_curve_value = m_last_on_curve_value;
}
else if (output_data_temp > m_last_on_curve_value && input_data >=
m_last_on_curve_value*(1-threshold) + threshold)
{
output_data = m_last_on_curve_value;
m_last_update_side = 0;
m_last_on_curve_value = m_last_on_curve_value;
}
}
else if (m_last_update_side == 1 && m_last_on_curve_value == 0)
{
if (input_data < threshold)
{
output_data = m_last_on_curve_value;
m_last_update_side = 0;
m_last_on_curve_value = m_last_on_curve_value;
}
else if (input_data >= threshold)
{
output_data = (input_data-threshold)/(1-threshold) ;
m_last_update_side = 0;
m_last_on_curve_value = output_data;
}
}
return output_data;
}
 
N

Noah Roberts

Marco said:
I used to write program in Matlab. I have no experience in C++ at all.
But my boss order me to convert a matlab function to C++ function, so
he can use it with his program. For the concept of the program, it
receive a new input and keep some variable in memory to give a output.
Could you please tell me what mistake I have make in the program?

Thanks


#include <stdio.h>
#include <math.h>


class CGripperHysteresis {

private:

public:
CGripperHysteresis( );
void SetThreshold( double threshold);
double CalculateOutput( CGripperHysteresis, double input_data);
};

CGripperHysteresis::CGripperHysteresis( )
{
double m_last_update_side = 0;
double m_last_on_curve_value = 0;
double m_threshold = 0;
double output_data = 0;
double input_data = 0;


}

Here is one mistake. Those members should be declared in the class and
initialized in the constructor. You should also consider using
constructor initialization instead of setting values in the constructor
body.
 
H

Howard

Marco said:
I used to write program in Matlab. I have no experience in C++ at all.
But my boss order me to convert a matlab function to C++ function, so
he can use it with his program. For the concept of the program, it
receive a new input and keep some variable in memory to give a output.
Could you please tell me what mistake I have make in the program?

It might help if you told us what the problem is. Is it failing to compile?
If so, what error messages do you get, and for what lines of code? Is it
compiling, but not running properly? If so, have you tried debugging it?
Does it produce wrong output? Or crash?

You need to tell us exactly what help you want, not just ask us what's wrong
with some random code.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top