Visual Studio 2010 C++ code formatter - c++

I'm new to coding C++ programs in Visual Studio. When I am coding C# programs in VS, code formatter changes code that looked like this
for(int i= 0; i<(n+ m) ; i++){
}
into
for(int i = 0; i < (n + m); i++)
{
}
which is way easier to read. When I write the same thing in C++, nothing happens. I tried to select the text and press Ctrl+E, F, but that didn't work. Is there a way to improve the C++ code formatter in visual studio?

Visual Studio can't format C++-Code. Maybe there is a VS extension. I searched a long time, but never found a suitable one for free.
A very good, free, but not easy to use code formatter is GreatCode. Its a command line tool.
You can configure it as "External Tool":
After unpacking GreatCode on your HD just go Tools->External Tools->Add and insert the following settings...
Whenever you call that Tool, the actual opened file is being formatted.
You can configure GreatCode as you like in the gc.cfg. I tried many options, some are easy, some are complex.
If you want a Microsoft-like looking, just use my settings as a template and fine tune yourself:
-code_constructor_style-1
-code_split_fctdef_style-5
-code_split_decl_style-2
-overwrite_read_only-
-verbose-
-tab_out-
-space_if-
-space_return-
-space_fctcall_inparam-
-no-space_fctcall_firstparam-
-no-space_cast_after-
-space_affect_style-0
-space_autoaffect_style-0
-code_len-180
-code_keep_more_empty_lines-
-code_decl_access_to_type-
-code_decl_break_template-
-code_remove_return_paren-
-code_align_max_blanks-80
-code_class_access_eol_after-1
-code_class_access_eol_before-1
-code_split_fctcall_style-1
-code_constructor_style-1
-no-code_split_bool_before-
-no-stmt_concat_else_if-
-no-stmt_decl_remove_empty-
-no-stmt_concat_if_remove_empty-
-no-stmt_concat_else_if-
-stmt_force_brace-1
-stmt_break_dowhile-
-stmt_switch_style-1
-stmt_switch_eol-0
-stmt_class_indent-0
-stmt_static_init_style-2
-stmt_concat_inline_class-
-pp_align_to_code-
-pp_style-1
-pp_align_breakline-
-no-cmt_first_space_cpp-
-cmt_dont_modify-
-no-cmt_add_class_access-
-no-cmt_add_gc_tag-
-no-cmt_add_fct_def_class-
-no-cmt_decl_before-
-no-cmt_decl-
-no-cmt_first_line_break_first-
-no-cmt_first_line_break_last-
-no-code_split_bool_before-
-catch_eol_before-1
-no-stmt_decl_remove_empty-
-no-cmt_add_fct_def_class-
-no-cmt_add_class_access-
-no-stmt_break_alone-
-stmt_concat_inline_class-
-cmt_keep_cpp-
Good luck!

I use exactly the same approach as DirkMausF except the formatting tool itself. I would suggest you to use Artistic Style formatter:
http://astyle.sourceforge.net/
It is well documented and comes with a lot of predefined formatting styles so it is very easy to use.

If you have cash to spend, you might want to look in to Visual Assist. See also, this question.

For a long time, I was writing all the C++ code in Netbeans and I was compiling it in Visual Studio. Netbeans is formatting code perfectly (with ALT+SHIFT+F) and there are many formatting options.

Related

Migrating flex 2.5.4a to 2.6 (lexical analyser generator)

I have a file that generates cc code using flex. When I use the version 2.5.4a-10 the codes works as expected.
If I use bit more recent version 2.5.37 or even newer like 2.6 the generated code seems not to allocate anything. It uses some pointers defined with nullptr and crashes.
I think the syntax has changed in between these versions. I find it also strange that Debian/Ubuntu have a package called flex-old saying:
flex is a tool for generating scanners: programs which recognize lexical
patterns in text. This is the old 2.5.4a version, which is no longer
being developed. You should normally choose flex, unless you have
legacy lexer files that do not work with a modern flex.
This product includes software developed by the University of California,
Berkeley and its contributors. The upstream source code can be found at
http://flex.sourceforge.net/
(Editor's note: Flex has moved to Github but v2.5.4a is not there.)
That version seems to be a big deal for others I suspect. Getting to my question:
Is there any manual or guide of what I have to do in order to port that code to generate some c++ code that works in more recent versions of flex?
EDIT: Here is my simple example taken from something larger:
int num_lines = 0, num_chars = 0;
%%
\n ++num_lines; ++num_chars;
. ++num_chars;
%%
int main()
{
yy_init=1;
yylex();
printf( "# of lines = %d, # of chars = %d\n",
num_lines, num_chars );
return 0;
}
flex it with flex file.l and build it with gcc lex.yy.c -lfl. Now, if you used version 2.5.4 it will work. With later versions it translates and compiles just fine, but when you run the program you will get segmentation fault.
I found the problem myself. The variable yy_init can be explicitly set in that old version. In newer versions it is not allowed. I'm not sure if that is intended, maybe someone can explain why this behavior is observed. I find it a bit strange.
If someone has a similar problem, you might want to take a look at the yy_init variable. Other than that I had no issues.

How to embed R in a c++ cli program

I'd like to embed R into a c++ winforms app. It will output the graphs to a pictureBox, and output the numbers to a richTextBox. It takes commands from a textbox. I tried the header file RInside.h, but there is no such file.
Pseudo code:
#include <someDirToImbedR>
ExecuteButton_Click(args) {
RExecute(commandTextBox->Text);
outputGraphPictureBox->Load(output_graph);
richTextBox1->Text = OutputString;
}
I found this post on SO concerning (may be) the same problem.
Here is the Link to a repository by Dirk Eddelbuettel. It has even sample code in c++ to try for yourself.
Also this Link might be helpful.
Hope this can bring you on track. Unfortunately I personally used R only in C#.

Code::Blocks fortran source formatting issues

I just started using the Code::Blocks editor for Fortran because a friend suggested it. I can't get the source formatter to work properly. For this simple code segment:
print *,'hello moon'
!left = (mybankact-tax) * number1
!iowe = stuff-1;
I get this, when I use "Format using AStyle"
print *,'hello moon'
!left = (mybankact-tax) * number1
!iowe = stuff-1;
I get these strange indents all the way down my code. Anyone know how to get this to work? This is driving me nuts. (Or other good editors for Fortran - I don't need to compile code, just edit and jump between functions, which code::blocks is great for.)
By the way, the file extension is ".90" and all the syntax highlighting works fine, so code::blocks seems to recognize this is Fortran okay.

Win32 C++ getting text from elements

While this question has probably been asked a thousand times before (pretty sure of it I have read a thousand answers). I still don't get it.
Lets say I have a function that creates a ComboBox like this:
scopeComboSelector=CreateCombobox(hwnd,
GetModuleHandle(0),
CBS_DROPDOWNLIST,
re,
IDCC_DROPDOWNLIST_SCOPE_SELECTOR,
_T("Scopes"));
Where "re" is a positioning rectangle. And IDCC_DROPDOWNLIST_SCOPE_SELECTOR (pretty long name) is the id of the combobox. Now the point is, I can actually fill this "drop down select list" but I have no clue as how I can simply get the currently selected value as a string.
I have seen about 10 ways to do it, which all give errors straight away (need to Convert to LPWSTR -> fixing results in more terror).
Maybe I'm just to used to Java where one can simply say:
textfield.getText();
How would one achieve this in Win32 C++ (microsoft visual studio)?
Edit
Code I've used:
char userName[_MAX_PATH+1];
GetDlgItemTextW(scopeComboSelector,
IDCC_DROPDOWNLIST_SCOPE_SELECTOR,
(LPWSTR)userName,
200);
Returns: userName == empty
Update
Now using: GetDlgItemText(). Debugger tells me the value of userName = ""
The documentation has a C style Windows 9x code example.
You need simply to replace C with C++ and Windows 9x silly T macros with wchar_t and friends.
It's always a good idea to read the documentation.

Looping through the samples of CAF file

Could anyone give me a suggestion or an example of how I would loop through the samples of CAF (Core Audio Format) file? Like taking the first 1000 samples and changing them to 0?
Something like:
for(int i=0; i < numberOfSamples; i++)
{
cafFile.sample[i] = methodThatUsesSampleValue(cafFile.sample[i]);
}
Keep in mind:
I don't need to do this live. No buffers, etc. needed.
I would like to do this in C, C++ or Objective-C.
Would like to be able to use the code on the iOS platform.
Any help appreciated!
The libsndfile C++ library is well-known and provides -among lot of other functions - a function to read AIFF-files: http://www.mega-nerd.com/libsndfile/, the lib is distributed under the "Gnu Lesser General Public License". I don't know if that is an issue.
Recently I stumbled over the amazing OpenFrameWorks library: http://www.openframeworks.cc/about And I know it compiles on MacOS and iPhone. Amongst many(!) other libs it come with an interface to rtAudio: http://www.music.mcgill.ca/~gary/rtaudio/index.html. You might want to consider also using that directly. Hope that helps.