Need suggestion on program structure

This is a discussion on Need suggestion on program structure within the C++ Programming forums, part of the General Programming Boards category; Hi, I need suggestions on how to make a C++ indenter.. The program will read a C++ file, first unindent ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    Need suggestion on program structure

    Hi,
    I need suggestions on how to make a C++ indenter..
    The program will read a C++ file, first unindent it (a function will get rid of the leading spaces/tabs of each line that's read), then according to the following specifications, indent it:

    every line inside a function (including main): 1 tab
    line after if (also else if, else) statement: +3 spaces
    statements inside a for/while/do loop: +1 tab
    ...

    (and then deducting the same number of spaces/tabs once the loop/if statement, etc is done)..

    So there will be one function that will unindent. And there will be one that will take the number of spaces/tabs (and which one of the two) it should add, and add it.
    But any suggestions on how to determine that number?


    Any help will be much appreciated..

    Linette

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The most common amounts of indention are 2 spaces, 4 spaces, and 8 spaces. It would be nice to let the user decide. You also have to account for different coding formats such as K&R or Allman, then consider indented brace formats. For example:
    Code:
    #include <iostream>
    using namespace std;
    
    int main ( void )
    { // Function opening brace, print and add 2 spaces
      int i = 0, j = 4;
      if ( i == 0 ) { // Conditional/loop beginning, add 2 spaces
        cout<<"Fooby";
        if ( j < 5 ) // Conditional/loop beginning, add 2 spaces
          cout<<" is the best!";
        // No opening brace, subtract 2 spaces after printing 1 line
      } // Closing brace, subtract 2 spaces and print
      cout<<endl;
      return EXIT_SUCCESS;
    } // Closing brace, subtract 2 spaces and print
    Using both keywords and tokens to determine when to add or subtract spaces, and if a conditional or loop doesn't contain an opening brace then subtract the spaces after the next line. Opening braces you can set a flag after printing, but closing braces the flag would have to be set before printing or the closing brace would be indented incorrectly. How you set the flags is up to you though.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  2. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  3. Simple program structure.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-15-2002, 04:36 AM
  4. structure program
    By prlove71 in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2002, 08:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21