Pulling data from an array

This is a discussion on Pulling data from an array within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by lpaulgib Is there a reason why I should declare the integer in the actual loop rather than ...

  1. #16
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by lpaulgib View Post
    Is there a reason why I should declare the integer in the actual loop rather than outside the loop in the beginning? I thought it would be cleaner code to have all the integers declared in the beginning. Is there a functional reason to do it inside the loop? I had forgotten to take the x variable out just because it was being used, then i cleaned out that loop...


    And also my indention and tabbing. Does it look clean? So far the only people that normally read the code i write is myself. And of course it looks good to me. Do you see any potential problems?
    Personally, I think it is much cleaner code in a for loop to declare the iterator inside. That way you don't have to go searching for the declaration elsewhere in the code, and so you can use the same name if you need to for a different variable outside the loop.
    As to your indentation and tabbing, I'm not particularly fond of it. I personally like to do something like this in a code block:

    Code:
    for (int i = 0; i < exampleNum; i++) {
        //put 4 spaces starting from 'f' in 'for' before this line of code if it were code instead of a comment
        //do stuff on this line...
        //and do stuff on this line....
        //etc..
    }
    That way its easier to read when you're scanning the code with your eyes. Its easier to pick out where the code block begins and ends. You crammed your for loop code all on the same columns, plus you put extra spaces in places where there shouldn't be any extra spaces, making it look like its a new code block or something, i.e.:

    Code:
    int familyfunct(int combinedages, int famtot){
    
        cout<<"\nAbout to do the math.....\n\n\n";
        int average = (combinedages/famtot);
        cout<< average << endl << endl;
    
             if(average>80){ 
                cout<<"You're all old!" << endl; 
             } 
    
             if(average< 20){
                 cout<<"You're a young family!" << endl;
             }
    
             if(average< 30){
                 cout<<"You're getting older!" << endl;
             }
             else{
                cout<<"You're pretty normal" << endl;
             }
    
    }
    You should also be aware that both the middle if statements will execute if the average is less than 20, meaning the output would be:

    You're a young family!
    You're getting older!
    I don't think that was intentional.
    You should use "else if" instead of "if" in the second middle one.
    Last edited by Programmer_P; 06-12-2010 at 06:29 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,618
    You need to add a check so that you can't enter more than 100 family members since you're using a C array.
    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
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    You need to add a check so that you can't enter more than 100 family members since you're using a C array.
    Oh yeah, good catch.
    @OP: Do something like this:

    Code:
    int loopCount = 1;
    do {
        cout<< "How many family members do you have? ";
        cin>>FamTotal;
        if (FamTotal > 100) { 
            cout<< "\nError! You have inputted too many family members.\n"
                   "Please try again." <<endl;
        }
        if (loopCount > 1) {
            cout<< "\nWe can do this all day if you want, but I suggest you get it right this time..." <<endl;
        }
        loopCount++;
    } while(FamTotal > 100);
    Last edited by Programmer_P; 06-13-2010 at 08:25 AM. Reason: fixed small logical error
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,618
    Actually, it should be > 100 since you already have 100 elements in the array (0-99).
    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.

  5. #20
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Actually, it should be > 100 since you already have 100 elements in the array (0-99).
    Right. My bad.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

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

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Pick data from file store in array
    By swgh in forum C Programming
    Replies: 1
    Last Post: 07-10-2009, 09:57 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Dynamic Array substitution in composite data variable
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-12-2008, 04:29 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM

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