Is this good style?

This is a discussion on Is this good style? within the C++ Programming forums, part of the General Programming Boards category; Is the way I used a while loop good style or should it be avoided? Code: #include <iostream> using namespace ...

  1. #1
    Unregistered
    Guest

    Is this good style?

    Is the way I used a while loop good style or should it be avoided?
    Code:
    #include <iostream>
    using namespace std;
    
    int main(void){   
      int n;
      while(std::cout << "Enter the number of lines (e to quit): " && std::cin >> n){
        for(int i = 0; i <= n; ++i){	
          for(int k = 0; k < n - i; ++k)
            std::cout << " ";
          for(int m = 1; m < i * 2; ++m)
            std::cout << "*";
          std::cout << endl;
        }
      } return EXIT_SUCCESS;
    }

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Style is really not good nor bad, rather personal.

    I've never seen anyone && two streams together in a condition, but have no trouble understanding what it means, and it does actually condense the code, so its not really a bad idea in that case. I would have written the code like this

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void){  
      int n;
      while(std::cout << "Enter the number of lines (e to quit): " && std::cin >> n){
        for(int i = 0; i <= n; ++i){
          // if no braces, put it on the same line
          for(int k = 0; k < n - i; ++k)   std::cout << " ";
          // if using braces, put it on a different line
          for(int m = 1; m < i * 2; ++m) {
            std::cout << "*";
          }
          std::cout << endl;
        }
      } // no code after closing bracket
      return EXIT_SUCCESS;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Is the way I used a while loop good style or should it be avoided?

    It's fine if all you are doing is this, but if you are obtaining any input after the while loop then you'll have to clear the stream state.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    You don't need std::cout or std::cin since you included using namespace std;. Just use cout and cin.
    Alternatively, instead of using the whole namespace std, you can declare using std::cout and using std::cin in place of using namespace std, save a bit of space. Kind of a personal choice there.

  5. #5
    Unregistered
    Guest
    How would I clear the stream state? cin.clear()?

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >How would I clear the stream state? cin.clear()?

    Yes, and then a cin.ignore() to discard the bad input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  2. How to write a program as good as possible?
    By jinhao in forum C++ Programming
    Replies: 9
    Last Post: 06-15-2005, 12:59 PM
  3. Good resources for maths and electronics
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-22-2004, 03:23 PM
  4. Good C++ books for a begginer
    By Rare177 in forum C++ Programming
    Replies: 13
    Last Post: 06-22-2004, 04:30 PM
  5. i need links to good windows tuts...
    By Jackmar in forum Windows Programming
    Replies: 3
    Last Post: 05-18-2002, 11:16 PM

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