I am using VScode with intelliSense for programming in c++. When I type a line ending with c; for example
int a=b+c;
Intellisense still offers me option continue;, so when I hit Enter to go to the next line, it changes c; to continue;, what I obviously don't like and don't want.
In general I would expect it wouldn't offer any options after semicolon. Can I somehow change this setting?
Related
Using new Eclipse and CDT versions built into STM32CubeIDE. I have the built in formatter options adjusted and use clang-format as my main beautifier. Everywhere I can see in the Window >> Preferences menus I have indent turned to 2 spaces-only.
Next line indent works correctly with everything but structs which the indentation seems to be doubled.
It doesn't seem to be indenting 2 units twice, because pressing tab moves me 4 spaces while inside a struct block. Clang-Format with CTRL + SHIFT + F does fix the incorrect formatting, but I'd rather a proper solution.
Either it's a bug, or somewhere this is yet another setting for "4 space indent but only while inside a struct block"?
See examples:
typedef struct
{
//New lines created inside the block start here, indented at 4 and not 2
//I get here if I press tab from the start column
//This is where it should intent to, manually pressed spaced twice
} some_new_t;
void foo()
{
//Correct
}
if (something)
{
//Correct
}
while(1)
{
//Correct
}
#ifdef TEST
//Doesn't indent, that's fine
#endif
EDIT: Applies to unions as well
Figured it out. This was not a 2 spaces vs 4 spaces issue, rather double indent when you only wanted one.
For some reason CDT has a lot more references to C++ and still Java than C and this was labeled under something misleading.
Under Window, Preferences, C/C++, Code Style, Formatter >> Edit >> Indentation >> Indent there are two options that can be checked.
'public', 'protected', 'private' within class body
and
Declarations relative to 'public' 'private'
You can check ONLY ONE of these for C structures and unions to indent at your chosen width. For whatever non-C reason, each one counts as including one indent width.
There are some weird auto indentation going on in CLion that I don't understand why. Here is an example
int i1(5); // some comments, then I hit Enter
// auto indentation goes here. I hit Enter again
// It goes here
float f = 5; // some comments, then I hit Enter
// then auto indentation goes here. I hit Enter again
// It then goes here.
float f2 = 6;
// If I don't comment on the previous line, there is no weird indentation
It seems like whenever I use = to initialize an variable, and then add comment at the end of the line, it will give me extra indentation on the next line. But in all other instance, it will not give me extra indentation.
I recently installed CLion and it was using default code style. It seems like the the extra indentation is controlled by the continuation indent setting, but I only want this continuation indent setting be application to codes, not comments. Where can I change me setting to disable the continuation indentation for comment when using = for assignment?
This bug has been fixed in CLion 2018.2 EAP (https://www.jetbrains.com/clion/nextversion/ ).
I personally uninstalled CLion 2018.1 because of this bug, since the EAP was stable enough.
I'm using the scite text editor (I cannot make use of any IDE or compiler since I'm required to also utilize Makefiles which is only possible if I use some sort of text editor) for all of my coding in c++. However, I'm consistently facing the same challenge; the text editor (I've attempted this on multiple ones including codepad and sublime text) isn't reading any input from the keyboard. Here is the source code:
#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE_OF_ARRAY = 5;
int main(){
int x, y;
int counter = 0;
int elements[SIZE_OF_ARRAY];
cout << "Please enter a number ";
cin >> x;
cin.ignore();
cout << "Please enter a choice ";
cin >> y;
if(y == 1){
for(int i = 0; i < SIZE_OF_ARRAY; i++)
elements[i] = -1*SIZE_OF_ARRAY + x;
for(int j = 0; j < SIZE_OF_ARRAY; j++)
cout << elements[j] << " ";
}
else if(y == 2){
for(int i = 0; i < SIZE_OF_ARRAY; i++){
if(i == 0)
elements[i] = -1*x;
else{
elements[i] = elements[i-1] + 1;
}
}
for(int j = 0; j < SIZE_OF_ARRAY; j++)
cout << elements[j] << " ";
}
else if(y == 3){
for(int i = 0; i < SIZE_OF_ARRAY; i++){
counter++;
elements[i] = 7*x*counter;
}
for(int j = 0; j < SIZE_OF_ARRAY; j++)
cout << elements[j] << " ";
}
}
The program is supposed to take as an input any number from the user and, depending on a numeric choice (between one and three) entered by the user, manipulate the value first entered somehow.
Choice one (User picks first choice)
The program negates the size of the array and adds the number which the user first entered and fills the array with the resulting value.
Choice Two (User picks second choice)
The program negates the number entered by the user, places this in the first array location then each successive element is added one unit more than the previous one.
Choice Three (User picks third choice)
Fills the array with the first five multiples of seven. Then shifts each number by a factor equivalent to the number the user had first entered.
I've ran it on an IDE (Codeblocks) and it works perfectly well. However, on any text editor, the 'cout' statements are printed with the variables x and y taken to each be equal to zero rather than being set to the value entered from the keyboard. It doesn't even allow any keyboard input. Any answer regarding how I can fix this would be immensely appreciated.
Hoosain, continuing from the comments above, when you use an IDE, you must configure the path to your compiler as well as all compiler options you wish to use, and the location for the resulting executable and object files, etc. When you used CodeBlocks on windows, you essentially got lucky that CodeBlocks will automatically detect whether MinGW is installed and set its compiler configuration to allow you to build and run your code without you having to configure the compiler details. Geany is another editor that does a good job auto-detecting and using MinGW.
For the remaining IDE's it is up to you to configure them to find and use the compiler you have installed (MinGW), as well as configuring all desired compiler options (at minimum enable compiler warning with -Wall -Wextra).
That is where new programmers who have only used an IDE configured for them run into problems... Before you can tell an IDE where your compiler is located and which compiler options you want to use, you have to know where you compiler is located and understand what minimum set of compiler options you should use.
The way you learn to use a compiler is with the good old command line. (yep, that's cmd.exe on windows, often labeled as the "DOS Prompt" in earlier versions) An IDE is just a front-end to your compiler that executes the same commands you can simply enter on the command line to compile your program.
Learning how to use your compiler will save you an incredible amount of time when learning to code. You can simply open a command prompt and compile any file you wish, without setting up a project, etc.. When learning to code, trying to shoehorn small examples into an IDE is much more time consuming and often more trouble than it is worth. Rather than worry how to use an IDE, focus on "how to use your compiler" first.
Since you have MinGW installed on windows, all you have to do to be able to compile from the command prompt is add the path to the MinGW bin directory to your User Environment. You do that by adding the PATH as an Environment Variable here:
Start Menu-> (rt-click on Computer)-> Properties->
Advanced System Settings-> (Advanced tab)-> Environment Variables
In the Top window (your user variables), click to add (or edit) the PATH "Variable name". Generally, if you installed MinGW in the default location, you simply add the path as the "Variable value":
c:\MinGW\bin;c:\MinGW\mingw32\bin
(verify the path on your computer)
(note: windows separates path components by the semi-colon, so if there is already a PATH variable set, just add a semi-colon between what is there and what you add. Also if you already have a command prompt open, you must close it and open it again for the new path to take effect) Just open Start Menu-> Accessories-> Command Prompt (you can rt-click on the icon (top-left) and choose Properties to set the font (recommend Lucida Console 12) and height/width)
Now you have configured your command prompt to allow you to compile any file at any location within your filesystem. For example, I tested with the code you posted (I modified it to add prompts for the information). Compiling it is a piece of cake. I keep my executables in a bin directory to keep sources and binaries separate.
I named your file array_get.cpp.
Compile
Then just enter the normal g++ compiler command, and at minimum use -Wall -Wextra options to enable compiler warnings (you can add -pedantic and whatever additional warnings you want, I would recommend at least adding -Wshadow so your compiler will warn on any variables you declare in multiple scopes that could conflict). The -o option allows you to specify the location of the executable (I just use a separate bin directory as explained above). So to compile and link your code into bin\array_get.exe all I have to enter is:
C:\Users\david\Documents>g++ -Wall -Wextra -o bin\array_get array_get.cpp
(do not accept code until it compiles without warning -- read any warning (it gives line of problem), understand what it is telling you, and go fix it)
Example Use/Output
C:\Users\david\Documents>bin\array_get.exe
Please enter a number: 21
Please enter a choice (1-3): 3
147 294 441 588 735
That's it. Since MinGW uses gcc, the compiler commands you use on windows are the exact same you would use in Linux, so learning to compile from the command line pays double-benefit.
Now you have the luxury of using any text-editor to edit your code while you have the command prompt simply and easily re-compile as you wish until your code is correct. No project dialogs, no mess of a different folder for every file, just the freedom to compile any file you wish -- right from the command line. (I actually separate my sources in directory by type, e.g. c, cpp), you find what works best for your. I also use a simple bat file that takes the exename and source.cpp names as arguments and then compiles with the options I set -- it just cuts down on typing :)
Further, since you now where your compiler is located, and which options to use, you can open the Settings window on any IDE and set the appropriate compiler command and compiler options to allow the IDE compile your code for you. Give it a try, and let me know if you have further questions.
I am using C++ on Netbeans 7.1 on Ubuntu 11.04. For some reason, the following code results in the error message "Unable to resolve identifier cout".
#include <iostream>
using namespace std;
int main()
{
std::cout << "Hello,world!\n";
return 0;
}
Any help resolving this problem would be greatly appreciated.
The solution for your problem is at least strange ;)
Once iostream header is added, one has to reparse code. Click right on a project, go to code assistance and click to reparse project. Worked for me.
I was using netbeans for mac.
check whether iostream is really getting included;
i have tried your code on my machine using eclipse cdt it worked fine.so, please check the
includes.
What sort of file is this in? Is it a .h file, or .hpp file? I had this same issue. Netbeans can be ridiculous sometimes with C++. For me, I changed #include <iostream> to #include<iostream.h>
This may seem too simple, but...
In my NetBeans installation, when I go to create a new project, specify C/C++, it brings up a dialog box prompting for "Project Name:", location, folder, makefile name, and then...
a check box for "Create Main File", an edit box with "main" filled in, and to the right of that is a drop down list that reads "C". If you hit Finish, this will create "main.c" (C, but NOT a C++ file). Instead, in the drop down list, select "C++". Then the IDE creates main.cpp, which will be compiled with g++ and will find those includes and functions.
There is a difference between std::cout and cout. You don't currently have std::cout defined in your file. std::cout is a c standard out. In C++ we only need cout to work with iostream.
If you must use a standard c out then do the following:
Add this to the top under iostream
#include <iostream> //Input output stream in C++
#include <cstdlib> //Stands for c standard library
using namespace std;
Your code will now work because:
This change defines std::cout and std::cin among other things. (standard in, standard out respectively.)
However, I'd recommend this alternative if you don't need standard in outs:
Replace std::cout with cout, because cout is defined in iostream in C++. Your program would have worked without the std:: portion of your cin cout commands because you originally included iostream.
Try taking out the using namespace std; - it's generally considered bad form anyway :-)
I'm not sure that will fix the problem but most people either use the namespace or fully qualify things like std::cout. I've never seen code that does both.
The other thing to check is that the iostream header actually is being bought in. In other words, are there any errors on that line. A lot of problems (at least in the Windows world, so it may not necessarily apply to you) seem to be due to faulty path setup in NetBeans.
Hey look at your Output Debug. You may see "no permission". After I changed the file permission of "/YourProjekt/dist/Debug/GNU-Linux/file" to runable and everyone can read and write the error disappeared. (BTW: I had the bug because I was on a NTFS System with my Projekt, it have to be ext partition)
Hope I can help you with that.
Try taking out the std:: next to cout
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.