Yeah... I did read it... I'm just a little bit confused
Yeah... I did read it... I'm just a little bit confused
I don't really get it at all, what are indentation levels?
A function starts at indentation level 0, then statements inside at level 1, and any control statements (if, while, for, switch, etc) pushes the indentation in one level.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Indentation level means, in the case of indenting with tabs, the number of tabs before your code.
So indentation level 0 means no tabs, level 1 means one tab, level 2 means 2 tabs, etc.
With spaces, one indentation level is a fixed amount of spaces you use to indent. So, let's say we use 4 spaces to indent. Indentation level 0 is 0 spaces, level 1 is 4 spaces, level 2 is 8 spaces, etc.
Makes sense?
Yeah ok, but where are my levels inconsistent?
Taking a look at main:
Above is a few of them. Each block = +1 indentation level.Code:int main(void) { // Indentation level: 0! char str[10]; srand((unsigned)time(0)); ofstream a_file ( "example.txt" ); int random_integer; // Indentation level: 1 for(int index=0; index<7; index++) { // 11 spaces: 11 / 3 = 9 + 2 = Level 3 + 2 spaces random_integer = (rand()%99)+1; // 8 spaces: 8 / 3 = 6 + 2 = Level 2 + 2 spaces if (index) cout << ", "; { a_file << random_integer; cout << random_integer; } } cout << endl; a_file.close(); mypause(); }
What about now?
Code:#include <stdio.h> #include <fstream> #include <iostream> using namespace std; void myflush(std::istream& in) { in.clear(); in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } void mypause() { myflush(std::cin); std::cin.get(); } int main(void) { char str[10]; srand((unsigned)time(0)); ofstream a_file ( "example.txt" ); int random_integer; for(int index=0; index<7; index++) { random_integer = (rand()%99)+1; a_file << random_integer; if (index) cout << ", "; { cout << random_integer; } } cout << endl; a_file.close(); mypause(); }
No I get it, I made a mistake by thinking that each level was 4 spaces, not 3, Let me try it again. I also presume by you putting a "!" after the level 0 indentation, I should make it a level 1 indentation, am I correct?
Here is my code, each level consists of 3 spaces:
By the way, is indenting necessarily needed, or just programming etiquette?Code:#include <stdio.h> #include <fstream> #include <iostream> using namespace std; void myflush(std::istream& in) { in.clear(); in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } void mypause() { myflush(std::cin); std::cin.get(); } int main(void) { char str[10]; srand((unsigned)time(0)); ofstream a_file ( "example.txt" ); int random_integer; for(int index=0; index<7; index++) { random_integer = (rand()%99)+1; a_file << random_integer; if (index) cout << ", "; { cout << random_integer; } } cout << endl; a_file.close(); mypause(); }
Indentation is necessary so that you and others can easily read your code and to catch silly bugs such as code placed where it shouldn't be and missing brackets.
Bracket placements seem a little off. Take a look again at rule 2: http://cpwiki.sourceforge.net/User:E...ntation#Rule_2
Indentation levels are still not quite there. Sometimes you use two spaces and sometimes 3. Try reading rule 1: http://cpwiki.sourceforge.net/User:E...ntation#Rule_1
I would also like you to explain to me why the if statement only has two spaces preceding it (making it, what seems, level 1)?
You say three spaces, but you have a lot of two-space levels in there. Elysia just used three-space levels in the comment because the first indentation you did was three spaces. And that for-loop appears to be messing with your mind, as you still haven't gotten close. I have fixed it above, so that you do actually have three-space levels. Compare and learn.
As to needed, the compiling process strips out whitespace (except in strings, etc.), so it will compile the same. Indentation is only needed if you plan to read your program, edit your program, debug your program, or show your program to someone else.