Improving code

This is a discussion on Improving code within the C++ Programming forums, part of the General Programming Boards category; Is there anything I can do to this code to make it better? As in faster, more attractive, and easier ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    Cool Improving code

    Is there anything I can do to this code to make it better? As in faster, more attractive, and easier to understand (with out using comments).

    Code:
    #include <stdlib.h>
    #include <time.h>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ()
    {
    	int count=0;
    	srand ( time(NULL) );
    	fstream fout("rand.txt");
    	while (count != 50)
    	{
    		fout << rand()%6+1 << "\n";
    		count++;
    	}
    	return 0;
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Looks good to me, although I think most programmers would have preferred a for loop instead of a while, but to each his own.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Use void main, saves on the return........KIDDIN!!! you dont saw
    that coming did you?

    You might remove that value assigning of count since its 0
    at the start anyhow. Actually you shouldnt, but i cant thing of
    anything you can improve here while keeping good code.

    BTW. you're not using time.h or iostream.h

    Right! yelton said it, use a for loop, makes it 1 line shorter

  4. #4
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Originally posted by Travis Dane

    BTW. you're not using time.h or iostream.h
    I seed using time so I need time.h. But I don't see why I can't remove iostream. Thanks.

    And usually VC++6 gives you a warning when you include a file and then you don't reference it. But it didn't give that with iostream.h... why is this?
    Last edited by Munkey01; 01-10-2003 at 02:59 PM.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    actually, if you make it <ctime> and <cstdlib>, the code would look better.

  6. #6
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Yea it does look better with that.

    I think I will use ofstream so I do not have to make the file first.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Or you could do away with the explicit loop -

    Code:
    #include <ctime>
    #include <algorithm>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    class randgen
    {
    public:
    	randgen()
    	{
    		srand((unsigned)time(NULL));
    	}
    	int operator()()
    	{
    		return rand()%6+1;
    	}
    };
    
    int main() 
    {
    	ofstream fout("rand.txt");
    	generate_n(ostream_iterator<int>(fout,"\n"),50,randgen());
    	return 0;
    }
    Joe

  8. #8
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    I was looking for clearer/cleaner/easier ways because this is something I must show to a bunch of computer illiterate people.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    with out using comments
    I assume you mean in addition to using comments.

    Your program should always use enough comments to explain what you are doing. If its an example program for beginners, as this seems to be, then at least one comment per line is not overdoing it

    If you're writing the program for yourself, or one that may need to be "maintained" by another skilled programmer, then you don't need to comment stuff like x = y + 1, or return 0, but you should still include lots of comments. I'll have to count the comments in one of my larger programs to confirm this, bit I'll bet there are 200 comments in 500 lines of my code. (Of course, many of these comments are 1-3 words long.)

    Some poople think its macho to leave out comments and cram as much into one line as possible. Not me! I want to make my programs as readable as possible without "cluttering" them with comments. Sometimes there is a trade-off between making readable code and efficient code, but this does NOT apply to comments.

    One argument for omitting comments is that sometimes the code gets changed, and the comments don't. This can get really confusing. This is a wimpy excuse.

    So don't be too macho or too wimpy. Just do it right and take the time to keep the comments up-to-date.

  10. #10
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    you guys missed the obvious one, he forgot:
    Code:
    fout.close();
    PHP and XML
    Let's talk about SAX

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Originally posted by Waldo2k2
    you guys missed the obvious one, he forgot:
    Code:
    fout.close();
    No, it's redundant.
    Joe

  12. #12
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    >>redundant

    why?
    PHP and XML
    Let's talk about SAX

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    It's called when the ostream gets destructed. If you included this in the example the people reading the code may gain the incorrect impression that it was necessary to call ostream::close() always; when this isn't the case.
    Joe

  14. #14
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    >>incorrect assumption that it was necessary to call ostream::close() always; when this isn't the case.

    ahhhh, i was under the assumption that it was necessary, I've always done it in my code. Ok then, i guess I was wrong.
    PHP and XML
    Let's talk about SAX

  15. #15
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    EDIT: never mind =P

    anyway

    Code:
    int main (void)
    {
    	srand (time(NULL));
    	fstream fout("rand.txt");
    	for (int count = 0; count < 50; ++count) {
    		fout << rand()%6+1 << endl;
    	}
    
    	return 0;
    }
    For loops are cleaner and in C++ you should try to initialize variables at the point you're using them.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Improving Code Interface
    By helloamuro in forum C Programming
    Replies: 20
    Last Post: 05-02-2008, 04:34 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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