c++ how can I display the string attribute of an element at a particular position in a vector?

75 views Asked by At

I am trying to print the value of a C++ variable in the console for debugging purposes. The line that I am using to try and do this is:

    printf("\n Value of lTempCoord in CSARCreepingLineItem::SteerStep(...): %s", rm_WPSequence[m_SAR_OrdinalNumber].m_FixName);

But the output that this line generates is unreadable. I am wondering if this is because I am trying to display it using a 'string' character %s but then passing something other than a simple string/ character array to it?

Breaking down the variable that I am passing to the printf():

rm_WPSequence is a Vector, defined with:

vector< CUserWaypointListElement > rm_WPSequence;

m_SAR_OrdinalNumber is an int, and

m_FixName is a string, defined inside the CUserWaypoingListElement class with:

std::string m_FixName;

The output generated by the line is a few 'special' characters (not ones that you can see on the keyboard).

What am I doing wrong here? Is the output unreadable because I'm not accessing the member of the Vector correctly, or because the string is defined as part of the std library? Or is it something else completely?

1

There are 1 answers

0
Guillaume Munsch On

Try this

std::cout << rm_WPSequence[m_SAR_OrdinalNumber].m_FixName  << std::endl;

printf is basically a C function, you should use c++ mechanism in your c++ programs ;). If you really want to use printf(), transform your std::string in char * by using the std::string.c_str() method