Thread: I need help with this program to find Hi-low,med of three numbers.

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    10

    I need help with this program to find Hi-low,med of three numbers.

    I cannot get this program from having a continuous loop. I need to have a user-defined loop, but can't figure it out. Any help would be appreciated. Yes, this is an assignment for my 1st programming class.

    Here is what I have so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    
    
    {
     int a,b,c,high,med,low;
    
         printf("This programs sorts numbers from highest to lowest\n");
    
         printf("Enter 3 numbers seperated by a comma or 'Q' to quit\n");
    
          while(scanf("%d,%d,%d", &a,&b,&c) != 'Q')
    {
    
       if (a < c && c < b)
    
      {
          high = b;
           med = c;
          low = a;
       }
    
          if (c < a && a < b)
        {
           low = c;
           med = a;
           high = b;
        }
          if (b < a && a < c)
       {
          high = c;
          low = b;
          med = a;
        }
        
        printf("Here are your numbers high to        low:%d,%d,%d\n",high,med,low);
    
         printf("Enter 3 numbers or 'Q' to quit:\n");
       }
         return 0;
       }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Look up the return value of scanf().
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    Can you give me a hint? I am not sure what you are saying.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hint˛ : Scanf - Ref.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're testing the return from scanf() in your while loop -- not the entered variable from the user. So the test will be SOMETHING that scanf() returns, which will (realistically), never equal to 'Q'.

    It will be an integer, in a very low range of values.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    So, are you guys saying I need to get rid of the while statement !='Q'? I tried that but it still won't give me the user-defined loop I desire. I might be doing something wrong, obviously, but I do appreciate all your help people.
    Thanks again!

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm saying you're not testing for the 'Q', like you believe you are, in that test by the while loop. scanf() returns an integer, and not a 'Q'.

    What should you be testing for? Work it on out!

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    Thanks Adak, I was losing brain cells trying to figure this out. It works the way I want it to now. Thank all of you for your explanations.!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-09-2013, 07:41 PM
  2. Program to find largest of 3 numbers
    By stuartbaggs in forum C Programming
    Replies: 9
    Last Post: 10-11-2012, 04:50 AM
  3. Find largest numbers
    By krakatao in forum C Programming
    Replies: 1
    Last Post: 02-04-2012, 09:58 AM
  4. Help! I cant find line numbers
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2005, 07:02 AM
  5. find median of numbers
    By SnoFinK in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2001, 09:54 PM