counters

This is a discussion on counters within the C++ Programming forums, part of the General Programming Boards category; Hey, guys. I have to follow a pseudocode tha includes loops etc. I got the loops so far, but I ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    72

    counters

    Hey, guys. I have to follow a pseudocode tha includes loops etc. I got the loops so far, but I am having troubles with the counters.


    counter is assigned 0
    while counter is less than 5
    display "\nI love computers!"
    increment counter
    endwhile

    Code:
    counter = 0
    {
    while
    (counter < 5)
    }
    cout << "\nI love computers!";
    {
    increment counter
    endwhile
    }
    I read up on it. But I don't really see how it works here.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The word "increment" means "add one to".

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Yes, I know, but it's a variable. Like int counter 0 to initialize it?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So add one to it. Generally speaking, we use "+" to add things together, and "=" as you've seen assigns a value to a variable.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    counter+; would come to my mind now. This stupid book dosen't tell me how to initialize the counter. Since it's a variable , I thought int counter = 0.
    Last edited by XodoX; 03-08-2009 at 09:50 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's the initialization, int counter = 0. That's not going to help with the increment. counter is fine, + is fine -- so, what do you want to add to counter?

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    It's asking the user for his age. Then it decides if he/she can or can't vote.

    Like.....

    if age greater than 17
    display "You can vote."
    else
    display "You can't vote."
    endif

    That's the first part, which i already have. So I think its counter+; I saw it also might be ++ ..depending on how you need to do it.
    Last edited by XodoX; 03-08-2009 at 10:04 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right. So counter adding one is counter + 1; since you want to assign that back to counter, you get counter = counter + 1.

    Now that we've done that, there is an operator "++" that adds one to a variable all in one shot.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Ok , so that's basically ok? For that particular part here...

    Code:
    counter = 0
    {
    while
    (counter < 5)
    }
    cout << "\nI love computers!";
    {
    counter +; 
    endwhile
    }
    Last edited by XodoX; 03-08-2009 at 10:10 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your braces have gone awry. You managed to completely ignore my last post, so you should maybe read it this time. The only line you had correct (int counter = 0 you managed to destroy.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    That should be it.

    Code:
    counter = 0;
            while (counter < 5)
            {
            cout << " I love computers ! \n" ;
            count + 1;
            }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by XodoX View Post
    That should be it.

    Code:
    
    counter = 0;
            while (counter < 5)
            {
            cout << " I love computers ! \n" ;
            count + 1;
            }
    Green = good; Red = bad. You need to assign a new value to counter as you go through the loop.

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    ops sorry, I got counter. Don't know why I copied it. But no matter what I type in it always says I love computers.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Did you expect it to do something else?

    And did you assign a new value to counter -- adding one is not enough, you have to put the answer somewhere.

  15. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Well, it's only supposed to say this if it's less than 5.

    And no, I probably didn't. This is so confusing. I didn't think I have to do it since it's just supposed to say yes you can vote or no you can't, and if it's less than 5 just I love computers.
    Last edited by XodoX; 03-08-2009 at 11:17 PM.

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

Similar Threads

  1. Cycle counters
    By cosmo1996 in forum C Programming
    Replies: 3
    Last Post: 08-14-2007, 10:33 AM
  2. [C] Best way to save matrix counters
    By Nazgulled in forum C Programming
    Replies: 7
    Last Post: 03-24-2006, 05:02 PM
  3. array of counters
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 12-11-2005, 02:25 AM
  4. Having trouble with counters
    By tameeyore in forum C Programming
    Replies: 30
    Last Post: 10-08-2004, 03:14 PM
  5. array of counters
    By dantestwin in forum C++ Programming
    Replies: 5
    Last Post: 07-04-2004, 12:14 PM

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