Thread: Read until negative number

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    Read until negative number

    Hi,

    I have a program to do
    read numbers until negative number read

    Code:
    include <stdio.h>
    void main ()
    
    {
    
         int numbers  
    
         printf ("Type a number:")  
    
         scanf ("%d", &number)  
    
         if (number < 0)  // check whether the number is a negative number
    
         printf ("Value is %d \n", number) 
    
    }
    I wan to modify this program and wanted to print biggest and smallest number entered.
    Say

    if (ngative)
    stop
    print(biggest value)
    print(smallest value)
    end

    anybody can help me
    Thanks in advanced

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is missing is the logic. If you would read a number of numbers from a page until you find a negative number, how would you do?
    You should also know about loops, if you do not know them already. They are present in the tutorials here.
    And don't use void main - use int main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    please look at this.hope this is ok

    Code:
    include <stdio.h>
    int main ()
    
    {
    
         int numbers  
    
         printf ("Type a number:")  
    
         scanf ("%d", &number)  
    
         do
    {
        printf ("Value is %d \n", number) 
    } while (number < 0);
    
    
    }

  4. #4
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    You might find "breaking out of a loop problem" thread helpfull.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Code:
    include <stdio.h>
    int main ()
    
    {
    
         int numbers  
    
         printf ("Type a number:")  
    
         scanf ("%d", &number)  
    
        while(1)
       {
        scan... 
       if (number < 0)  // check whether the number is a negative number
    
         printf ("Value is %d \n", number) 
    
         }
    
         
    }
    hope now ok.so whats the next step to do?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by m_user
    hope now ok
    No, now you have an infinite loop. Your earlier do while loop was along the right lines, but you need to use the correct loop condition, i.e., you loop while the condition is true, not while the condition is false. I presume that you are also assuming correct (i.e., integer input within range) for now.

    EDIT:
    Actually, a while loop is probably the most appropriate here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    Sorry for asking this

    Quote Originally Posted by laserlight View Post
    No, now you have an infinite loop. Your earlier do while loop was along the right lines, but you need to use the correct loop condition, i.e., you loop while the condition is true, not while the condition is false. I presume that you are also assuming correct (i.e., integer input within range) for now.

    EDIT:
    Actually, a while loop is probably the most appropriate here.
    Sorry for asking this.cant get it.please show me a sample one

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by m_user
    Sorry for asking this.cant get it.please show me a sample one
    I'll show you some pseudocode instead:
    Code:
    if scan into number and number is non-negative
        max_value = number
        min_value = number
        while scan into number and number is non-negative
            if number is greater than the current max_value
                max_value = number
            else if number is less than the current min_value
                min_value = number
        print max_value and min_value
    else
        print message informing user that no non-negative numbers were entered
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Hi what about this.I am not sure the while condition that i have used.Anything wrong correct the code

    Code:
    include <stdio.h>
    int main ()
    
    {
    
         int number,max_value,min_value;
         int valid = 0;
    
    
         printf ("Type a number:")  
         scanf ("%d", &number)  
    
    	if(number > 0) //non-negative
    
    		max_value=number
    		min_value=number
    
    			while(valid == 0)
    				{
    					if(number>max_value)
            				max_value=number
    					else if(number<min_value)
    					min_value=number
       					printf("The Max number is %d\n", max_value );
     					printf("The Min number is %d\n", min_value );
    					else
    					valid = 1;	
    				}
    
    	else
    
    	printf("The number is non-negative number %d\n", number );
    
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by m_user
    I am not sure the while condition that i have used.
    Yeah, your while loop is problematic because you never actually read the rest of the numbers entered. Also, from your description, you only want to print the max and min after the loop.

    Quote Originally Posted by m_user
    Anything wrong correct the code
    You are missing several terminating semi-colons and braces, and your code can be much better indented. Note that 0 is not negative.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    the header file is
    #include<stdio.h> not include<stdio.h>
    also your code should return 0 at last.also you are inputting only one number,you should have a scanf inside the while loop for inputting several numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  2. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM
  3. How can I read one number at a time?
    By Bad_Scooter in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2003, 09:58 PM
  4. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM
  5. how to read in a negative number using cin
    By revelation437 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2003, 02:32 PM