Read until negative number

This is a discussion on Read until negative number within the C Programming forums, part of the General Programming Boards category; Hi, I have a program to do read numbers until negative number read Code: include <stdio.h> void main () { ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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
    Maz
    Maz is offline
    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
    21,451
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,451
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,451
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10: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, 01:32 PM

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