My loop doesnt work

This is a discussion on My loop doesnt work within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to get my program to loop through an integer which will have 1-10 digits and check ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    11

    My loop doesnt work

    Hello,
    I am trying to get my program to loop through an integer which will have 1-10 digits and check to make sure they all are 1's and 0's. At the moment, my code doesnt work. It gives me an answer as long as I enter a 1 or 0 as the last digit. I suspect it is a problem with my use of the modulus operator. Here is what I have:

    Code:
    while(control<10)
    {
    printf("Enter a binary number (1-10 digits):");
    scanf("%d",&binary);
    
    for(i=0;i<10;i++)
    {
    temp=binary%10;
    if(temp==0||temp==1)
    {
    control+=1;
    }
    else
    {
    control=0;
    break;
    }
    }
    }
    Thanks in advance for any and all help.

    Evan


    Reply Quote Top Bottom Edit

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    --edit--
    post deleted

    sorry, i misread what you were trying to do
    Last edited by jverkoey; 05-17-2004 at 10:55 PM.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Doesn't anyone teach noobs how to format their code? INDENT! INDENT! INDENT!

    Quote Originally Posted by Evandb
    Hello,
    I am trying to get my program to loop through an integer which will have 1-10 digits and check to make sure they all are 1's and 0's. At the moment, my code doesnt work. It gives me an answer as long as I enter a 1 or 0 as the last digit. I suspect it is a problem with my use of the modulus operator.
    No, the problem is your for loop simply tests the last digit 10 times. After testing the digit, you need to remove it so you can test the next to last digit.

    Then, consider what happens when you enter only 5 digits? What happens to your loop then?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    11
    Yeah, I am an idiot. I forgot to add binary=floor(binary/10); to the loop so it would test the next digit. Thanks for your response

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 03:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  3. Replies: 26
    Last Post: 06-15-2005, 03:38 PM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

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