Problem with sentinel controlled loop

This is a discussion on Problem with sentinel controlled loop within the C Programming forums, part of the General Programming Boards category; So far I have written a code to find the reverse of a number as well as the sum of ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    Problem with sentinel controlled loop

    So far I have written a code to find the reverse of a number as well as the sum of the digits for an integer. The program is meant to keep asking the user for a new integer so that it will find the sum and reverse of that new integer again. However when the program asks the user for a new integer it just adds it to the original one how can I separate the operation. For example this is what came out:

    Enter an integer:
    123
    The sum of the digits is: 6
    The reverse of the number is : 321
    1 <------------ (at this part it didn't even go back to the original printf statement)
    The sum of the digits is: 7
    The reverse of the number is : 3211




    Code:
    #include <stdio.h>
    int main()
    {
    
    int n, sum=0, r, reverse=0;
    
    printf("Enter an integer:\n");
    scanf("%d", &n);
    
    while(n!=-1)
    {
    while(n!=0)
    {
    r=n%10;
    sum=sum+r;
    
    reverse=reverse*10;
    reverse=reverse+n%10;
    n=n/10;
    }
    
    printf("The sum of the digits is: %d\n", sum);
    printf("The reverse of the number is : %d\n", reverse);
    
    scanf("%d", &n);
    
    }
    return 0;
    }



    By the way I am new with this so please bear with me, thank you in advance.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,779
    That's because the original printf statement is not part of your loops.

    Jim

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    You failed to reset sum and reverse inside the outer loop.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    I have reset the sum and reverse but how do I get the printf statement to be part of the loop?

  5. #5
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by n.jimenez View Post
    I have reset the sum and reverse but how do I get the printf statement to be part of the loop?
    You change sum and reverse inside the inner loop, you do not change the variables (exclusively) in the outer loop (the one controlled by "while (n != -1)").

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,516
    Quote Originally Posted by n.jimenez View Post
    I have reset the sum and reverse but how do I get the printf statement to be part of the loop?
    Same way you got any of those other statements to be part of the loop. Simply put it between the opening and closing curly brace of the loop you want. Where exactly in the loop you put it depends on where/when you want the print statement to appear relative to other statements in your loop. I recommend right before you ask for user input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Event Controlled Loop.
    By Manuel Garcia in forum C++ Programming
    Replies: 8
    Last Post: 07-02-2011, 12:06 PM
  2. sentinel controlled do-while loop
    By theCanuck in forum C++ Programming
    Replies: 10
    Last Post: 03-22-2011, 11:07 PM
  3. Sentinel controlled loop
    By arjay in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 04:30 AM
  4. sentinel controlled repetition example
    By droseman in forum C Programming
    Replies: 7
    Last Post: 09-26-2008, 02:17 AM
  5. Sentinel-Controlled repetition
    By Hybrid in forum C Programming
    Replies: 7
    Last Post: 02-07-2008, 08:43 PM

Tags for this Thread


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