Thread: Why the loop does not work as expected in this program?

  1. #1
    Registered User
    Join Date
    Jul 2016
    Location
    Pune, Maharashtra, India
    Posts
    6

    Exclamation Why the loop does not work as expected in this program?

    Code:
    /* Write a program to find the range of a set of numbers. Range
    is the difference between the smallest and biggest number in
    the list
    
    Date : 16/07/2016  Time : 6.51 PM IST */
    
    #include<stdio.h>
    
    int main()
    
    {
        int num_1;
        int max_num=0;
        int min_num=0;
        char choice ='Y';
    
        printf("Enter a number : ");
        scanf("%d", &num_1);
    
        if(choice == 'Y')
            {
            if(num_1 >= max_num)
                {
                max_num = num_1;
                }
    
            else min_num = num_1;
    
            printf("Do you want to enter a new number? ( Y/N ):");
    
            scanf(" %c",&choice);
            }
    
        printf(" \n Max Number : %d | Min Number : %d | Difference : %d", max_num,min_num,max_num-min_num);
    
    return 0;
    }
    The output is not what I had expected:
    Why the loop does not work as expected in this program?-2016-07-16-22_11_59-_e__c_ex-b-i-129-exe_-png

    The loop is terminating prematurely. Where am I going wrong I wonder..

    Please help. Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Perhaps you want
    while ( choice == 'Y' )

    instead of if ( choice == 'Y' )
    which happens only once (or not at all)

    You also need to scan in a number as well, inside the loop.
    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
    Jul 2016
    Location
    Pune, Maharashtra, India
    Posts
    6
    Quote Originally Posted by Salem View Post
    Perhaps you want
    while ( choice == 'Y' )

    instead of if ( choice == 'Y' )
    which happens only once (or not at all)

    You also need to scan in a number as well, inside the loop.
    Thanks Salem! For looking into the code, and helping me with it. indeed using the While() rather than() actually helped. and how could I have missed the scanf() within the loop.

    thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My First programme will not work as expected
    By Macattack in forum C Programming
    Replies: 3
    Last Post: 02-06-2014, 08:26 PM
  2. Replies: 2
    Last Post: 03-17-2011, 06:03 AM
  3. One pointer that does not work as expected
    By h3ro in forum C++ Programming
    Replies: 13
    Last Post: 07-16-2008, 07:58 AM
  4. Why this do-while loop doesn't work as I expected?
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-25-2002, 09:47 AM

Tags for this Thread