Thread: Saving number Output

  1. #16
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    Indenting...

    Yeah... I did read it... I'm just a little bit confused

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, so what parts confused you? Do you think you can elaborate a little?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    I guess I don't get much of it

    I don't really get it at all, what are indentation levels?

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Yeah ok, but where are my levels inconsistent?

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Taking a look at main:
    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()&#37;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();
    }
    Above is a few of them. Each block = +1 indentation level.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    How about now?

    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();
    }

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, really. Knowing indentation levels, I think you should re-read the indentation wiki faqs. At least the second. There are some rules to indentation, but you don't seem to grasp them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    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?

  11. #26
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    New indenting levels

    Here is my code, each level consists of 3 spaces:

    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();
    }
    By the way, is indenting necessarily needed, or just programming etiquette?

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Apocalyptic_end View Post
    By the way, is indenting necessarily needed, or just programming etiquette?
    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)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Apocalyptic_end View Post
    Here is my code, each level consists of 3 spaces:

    Code:
    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    
    using namespace st;
    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();
    }
    By the way, is indenting necessarily needed, or just programming etiquette?
    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.

  14. #29
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Quote Originally Posted by tabstop View Post
    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.
    Oh well I'm using Dev-C++ and it says it says 13:3 and I know the (Well I thought) the 3 meant 3 spaces. So I guess I was wrong. Thanks for the help.

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Can you explain why you put the if at indentation level 1?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting number of lines in my output file
    By zesty in forum C Programming
    Replies: 34
    Last Post: 12-25-2007, 09:15 PM
  2. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 10:08 PM
  3. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM
  4. Funny output on hex value of number
    By shav in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 08:19 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM