Thread: EOF Help NEEDED!

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

    EOF Help NEEDED!

    Hi i have a program getting input from a user, I need to incorporate a way for the program to end if the user presses CTRL + D while entering their input. I know you use EOF but i dont understand how to use it and where to use it. heres my code:

    Code:
    #include <stdio.h>
    
    void selection_sort(int a[], int size);
    
    int main(void) {
    
        int a[100], i, j, N;
    
        printf("How many numbers will you be entering: ");
        scanf("%d", &N);
    
        printf("Enter numbers to be sorted: ");
        for(i = 0; i < N; i++) {
            scanf("%d", &a[i]);
        }
        
        selection_sort(a, N);
        
        printf("Your sorted list of integers is : ");
        for(j = 0; j < N; j++) {
        printf("%4d ", a[j]);
        }
        printf("\n");
    
        return 0;
        }
    
    void selection_sort(int a[], int size) {
        int i, j, x;
    
        for(i = 1; i <= size - 1; i++) {
    
        j = i;
        x = a[i];
        
        while(a[j - 1] > x && j > 0) {
            a[j] = a[j-1];
            j = j - 1;
        }
        
        a[j] = x;
        }
    }

  2. #2
    Registered User
    Join Date
    Jan 2013
    Location
    UK
    Posts
    19
    I'm helping by adding comments, so please read through your code below. I don't think their is a way to exit the program with CTRL + D, but anyone can close a program with CTRL + C

    Code:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    #include <stdio.h> void selection_sort(int a[], int size); int main(void) { int a[100], i, j, N; printf("How many numbers will you be entering: "); scanf("%d", &N); printf("Enter numbers to be sorted: "); //Its nice to space out your for loops with an empty line for(i = 0; i < N; i++) { //Because Arrays are pointers, you don't need the & also add an /n //scanf("%d /n", a[i[); scanf("%d", &a[i]); } selection_sort(a, N); printf("Your sorted list of integers is : "); for(j = 0; j < N; j++) { printf("%4d ", a[j]); } printf("\n"); return 0; } //While this is correct you should have a function as follows //void selection_sort(int *a, int size){ void selection_sort(int a[], int size) { int i, j, x; //remove the smaller than or equal to and you can just have //for(i = 1; i < size; i++){ for(i = 1; i <= size - 1; i++) { j = i; x = a[i]; //remember removing the smaller than or equal to //while(a[j] > x && j > 0){ while(a[j - 1] > x && j > 0) { a[j] = a[j-1]; j = j - 1; } a[j] = x; } }
    I might be missing some errors, I'm new to trying to help people with out giving them the full answer =/. Memory allocation and everything else is pretty much terminated when the process is closed, but its not nice to leave it like that... but the code you have, shouldn't raise any problems.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Make your first loop this, then study the output.
    Code:
        for(i = 0; i < N; i++) {
            int result = scanf("%d", &a[i]);
            printf("Scan result = %d, is it EOF=%d\n", result, result == EOF );
        }
    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.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by 0x47617279 View Post
    Code:
    //Because Arrays are pointers, you don't need the & also add an /n
    //scanf("%d /n", a[i[);
    scanf("%d", &a[i]);
    clmoore3's scanf()-call is correct. a[i] is one element of the array (an int), thus the & is necessary.

    And please don't post code inside a table.

    Bye, Andreas

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by AndiPersti View Post
    clmoore3's scanf()-call is correct. a[i] is one element of the array (an int), thus the & is necessary.

    And please don't post code inside a table.

    Bye, Andreas
    Also, arrays are not pointers.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed on this !!!!
    By code_lover in forum C Programming
    Replies: 11
    Last Post: 08-07-2011, 02:15 AM
  2. Help needed!
    By cherek in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2006, 03:34 AM
  3. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  4. Some Help Needed!
    By ob1cnobe in forum C++ Programming
    Replies: 7
    Last Post: 05-29-2002, 03:08 PM