Code leapt over

This is a discussion on Code leapt over within the C Programming forums, part of the General Programming Boards category; The problem is that the program seems to leap over the central part of the code when executed, so it ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Exclamation Code leapt over

    The problem is that the program seems to leap over the central part of the code when executed, so it goes directly from the while loop to the system pause as indicated, ignoring all the ifs...

    Some help...

    Code:
    #include <stdio.h>
    
    #define NMAX 100
    
    main(void)
    {
      float datos[NMAX],max,min;
      int nelementos=0,i=0;
      
      printf("Introducir datos (0 para finalizar)\n");
      printf("dato[%d]: ",i);
      while(i<NMAX&&scanf("%f",&datos[i])!=EOF){
        i++;
        printf("dato[%d]: ",i);
      }
      
    //this part isnt executed apparently...  
     
     nelementos=i;
      if(nelementos>0){
        max=min=datos[0];
        for(i=0;i<nelementos;i++){
          if(datos[i]>max)
            max=datos[i];
          if(datos[i]<min)
            min=datos[i];
          }
        printf("Valor maximo: %g,valor minimo: %g",max,min);
      }        
      else printf("no hay datos\n");
    
    //and program continues here 
      system("pause");
      return 0;
    }
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    paninaro3000@yahoo.com

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That part isn't executed when you enter EOF. If you change your number to say 5, and actually enter all five, it works fine.

    [edit]
    printf("datos[0]: " ); scanf( "%f",&datos[0] );
    printf("datos[1]: " ); scanf( "%f",&datos[1] );
    printf("datos[2]: " ); scanf( "%f",&datos[2] );
    printf("datos[3]: " ); scanf( "%f",&datos[3] );
    printf("datos[4]: " ); scanf( "%f",&datos[4] );

    Replace your first loop with that. Then, once you've entered two of them, enter EOF, and watch your program do the exact same thing as with your loop.

    EOF kills your program.
    [/edit]

    Quzah.
    Last edited by quzah; 07-02-2002 at 07:13 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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