Thread: basic question (for loop and %10)

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    1

    Question basic question (for loop and %10)

    I'm fairly new to C++ and have an idea of what is happening in this chunk of code but would really like to get validation or correction, please.
    Code:
    for (int digit=4 ; digit >- ; digit --){
    
    switch (digit){
    case 1:
    break;
    }
    aNumber(bNumber % 10);
    bNumber /=10;
    }
    I've truncated the code down but I believe this is the essential part of it. My question is around what the aNumber(bNumber %10) and bNumber /=10 are actually doing. I'm assuming that aNumber is returning the left most integer in bNumber. Then bNumber /=10 is removing the left most integer from bNumber. Can you confirm or correct my assumption?

    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes it is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I have no idea what the function aNumber does since you haven't show it to us.
    But the modulus operator (%) returns the right-most digit (not left-most).
    And the integer division "removes" the right-most digit.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 03-28-2015, 10:28 PM
  2. Basic C programming: using kbhit to stop Loop
    By Random49 in forum C Programming
    Replies: 4
    Last Post: 05-16-2012, 12:18 AM
  3. Need help with a basic while loop program
    By eastham.robert in forum C++ Programming
    Replies: 2
    Last Post: 03-03-2012, 02:02 AM
  4. Basic Loop
    By DuckCowMooQuack in forum C Programming
    Replies: 17
    Last Post: 10-20-2011, 10:37 AM
  5. Basic Loop Question
    By demiurge in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2009, 09:10 AM

Tags for this Thread