Thread: does it mater if there are indents before some code?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    20

    does it mater if there are indents before some code?

    im just wondering if you have to put indents, and im also wondering if you have to put a space between each line. lets take the hello world program on your site. does it need the spaces between the lines and indents?
    Code:
    #include <iostream.h>
    
    int main()
    
    {
    
      cout<<"HEY, you, I'm alive!  Oh, and Hello World!";
    
      return 0;    
    
    }
    or does it not need them? like so:
    Code:
    #include <iostream.h>
    int main()
    {
    cout<<"HEY, you, I'm alive!  Oh, and Hello World!";
    return 0;    
    }
    ???

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    To the compiler, it doesn't matter how many tabs or new lines you use; or don't use, but it's preferred you use some method of indentation (it makes the code MUCH easier to read) and spacing. The more whitespace you use the clearer your code can be read by you and others. (Within reason, don't go crazy)

    It doesn't effect the size of your final executable; so don't be afraid to space things out. This is perfectly legal:

    Code:
    #include <iostream>
    using namespace std;int main(int argc,char *argv[]){int myint, yourint=0;cout<<myint+yourint<<endl;return 0;};

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    20
    oh ok, thats what i thought. thanks.

  4. #4
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    YES! for the code you wrote right there it doesn't matter much, but it's the same argument with as code tage, try reading this without indents:


    Code:
    main()
    {
    ...
    if (!infile) {cout << "ERROR" << endl << endl; return 1;}
    	
    for (i=0; (!infile.eof()) && (i <= 12); i++)
    {
    j=0;
    ch = infile.get();
    while ( (ch != ',') && (!infile.eof()) )
    {
    dummybuffer[j++] = ch;
    //cout << ch << '\t' << j << '\t' << dummybuffer[j] << endl;
    ch = infile.get();
    }
    dummybuffer[j] = '\0';  //string terminator
    stock[i].SetName(dummybuffer);	//sets the name of the item
    		
    j=0;
    ch = infile.get();
    while ( (ch != ',') && (!infile.eof()) )
    {
    dummybuffer[j++] = ch;
    ch = infile.get();
    }
    dummybuffer[j] = '\0';  //string terminator
    stock[i].ChangeQuan(atoi(dummybuffer));	//changes how many of the item
    		
    j=0;
    ch = infile.get();
    while ( (ch != ',') && (!infile.eof()) )
    {
    dummybuffer[j++] = ch;	
    ch = infile.get();
    }
    dummybuffer[j] = '\0';  //string terminator
    stock[i].ChangeValue(atof(dummybuffer));
    }
    infile.close();			// end of outputting to structure
    
    for (i=0; i<12; i++)	// this for loop outputs everything
    {
    cout << stock[i].name << "," << stock[i].GetQuan() << "," << stock[i].GetSValue() << "," << stock[i].GetValue() << endl;
    }
    return 0;
    }
    not fun!


    Now with:

    Code:
    main()
    {
    	...
    	if (!infile) {cout << "ERROR" << endl << endl; return 1;}
    	
    	for (i=0; (!infile.eof()) && (i <= 12); i++)
    	{
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (!infile.eof()) )
    		{
    			dummybuffer[j++] = ch;
    			//cout << ch << '\t' << j << '\t' << dummybuffer[j] << endl;
    			ch = infile.get();
    		}
    		dummybuffer[j] = '\0';  //string terminator
    		stock[i].SetName(dummybuffer);	//sets the name of the item
    		
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (!infile.eof()) )
    		{
    			dummybuffer[j++] = ch;
    			ch = infile.get();
    		}
    		dummybuffer[j] = '\0';  //string terminator
    		stock[i].ChangeQuan(atoi(dummybuffer));	//changes how many of the item
    		
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (!infile.eof()) )
    		{
    			dummybuffer[j++] = ch;	
    			ch = infile.get();
    		}
    		dummybuffer[j] = '\0';  //string terminator
    		stock[i].ChangeValue(atof(dummybuffer));
    
    	}
    
    	infile.close();			// end of outputting to structure
    
    	for (i=0; i<12; i++)	// this for loop outputs everything
    	{
    		cout << stock[i].name << "," << stock[i].GetQuan() << "," << stock[i].GetSValue() << "," << stock[i].GetValue() << endl;
    	}
    	return 0;
    }
    Seeing where loops and if statements ebgin and end is a real pain without.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM