#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Unhappy Seems so simple but my brain just won't..


    Hello, I'm just another newbie in need of help.

    I'm trying to write a simple while loop which will read int values each repetition. The thing is, it has to read a total of 6 int values each time, instead of one.

    I'm getting a hunch that it'd be a very small alteration to the usual while loop, but I can't seem to figure it out. It has definitely got me feeling incredibly stupid.

    Could you please help me?
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    You need to break the problem down into smaller pieces to tackle it. We all start off trying to solve everything in one go, and have to train ourselves to get out of that habit.

    Your general 'while' loop is something like ...

    Code:
    int i = 0;
    while( i < some_number_of_times ) {
       // read an int
       // do something with the int
       i++;
    }
    So you've two things to do inside the loop. But now, one of those things has changed from 'read an int' to 'read 6 ints'. But the thing is to think about that smaller problem separate to the while loop you've already written.

    The problem 'read 6 ints' could also be interpreted as 'read an int 6 times'. Now there's a hint in there for you. Any time you have to do the same thing multiple times, you'll want a 'while' (or 'for') loop. So now you should try to write a loop that will read an int 6 times (you'll probably want to set up an array to store them, rather than a single variable like you'd use if you were only reading a single int). When you've done that, you can paste that code into the correct place in your original 'while' loop.

    Hope that helps.

IMN logo majestic logo threadwatch logo seochat tools logo