Thread: How can we input -1 to stop program?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    How can we input -1 to stop program?

    how can do a code for -1 to stop program?

    Code:
       printf("Enter number of elements in array\n");
       scanf("%d", &n);
    
       printf("Enter %d elements\n", n);
    
       for ( c = 0 ; c < n ; c++ )
          scanf("%d", &array[c]);
    example: Enter number of elements in array: -1
    output: End Program

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Check the value before you use it, and react accordingly. Use an "if()". Is that what you're asking?

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Assuming you are in the main function you can use a return statement, or the exit function.

    The correct way to do it within another function is to return a failed value from the function back to main - You may want to save data before exiting the application.

    An if statement is enough to check to see if -1 was entered. You should be checking to see if a Goldilocks value was entered (not too high, not too low) before trying to use it.

    Code:
    
        int array[ARRAY_SIZE];
        int n;
    
    
        puts("Enter number of elements in array (max 5 elements)");
    
    
        if (1 == scanf("%d", &n) && 0 <= n && ARRAY_SIZE >= n)
        {
            int c;
            printf("Enter %d elements\n", n);
            for ( c = 0 ; c < n ; c++ )
            {
                // TODO: Also needs checking to see if scanf succeeded
                scanf("%d", &array[c]);
            }
        }
        else if (-1 == n)
        {
            return EXIT_SUCCESS;
        }
        else
        {
            fputs("Unexpected input...", stderr);
            return EXIT_FAILURE;
        }
    
    
        return EXIT_SUCCESS;
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    int main()
    {
       int array[100], num1, c, n, num2;
    
       printf("Enter number of elements in array\n");
       scanf("%d", &n);
    
       printf("Enter %d elements\n", n);
    
       for ( c = 0 ; c < n ; c++ )
          scanf("%d", &array[c]);
       
       printf("Enter the number to swap\n");
       scanf("%d", &num1);
       
       printf("Enter the number to swap with\n");
       scanf("%d", &num2);
       
        printf("%d is swap place with %d.\n", num1, num2);
         
       
       for (c = 0; c < n; c++)
       {    
       if (array[c] == num1)
       {
        array[c] = num2;
       }
       else if (array[c] == num2)
       {
        array[c] = num1;
       }
    }
       printf("The new output will be\n");
        for (c = 0; c < n; c++)
          printf("%d\n", array[c]);
        getch();       
        return 0;
    }
    This is my coding for swapping element. so how should i input the -1 inside this code? i tried to use: if ( -1 == n) printf("end program"); but don't works.
    can anyone help me out?

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    "Didn't work" is not a satisfactory response -> I don't know what you want.

    How about this:
    Code:
      printf("Enter number of elements in array\n");
      scanf("%d", &n);
    
      if (-1 == n)
      {
        exit(0);
      }
    And you may want to be careful using conio.h -> It's only available on some systems. If you are making the program for yourself, you are limiting where you can use. If you are submitting this program as an assignment, not all markers will have access to conio.h (Your program will not compile on there computer). All you are using it for is the getch at the end -> You can use getchar() instead.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stop/pause program after asking for input
    By mrppinzon in forum C Programming
    Replies: 1
    Last Post: 10-11-2012, 01:46 PM
  2. How can I get input to stop at 'Esc' instead of \n ?
    By xkronix in forum C Programming
    Replies: 11
    Last Post: 09-17-2012, 11:07 AM
  3. How to stop reading input...
    By intull in forum C Programming
    Replies: 8
    Last Post: 02-20-2011, 03:38 AM
  4. Scanf sometimes do not stop for input
    By kapil1089thekin in forum C Programming
    Replies: 1
    Last Post: 10-03-2010, 07:25 AM
  5. stop input being echoed
    By Kinasz in forum C Programming
    Replies: 11
    Last Post: 07-25-2004, 11:02 AM