-1

I have little problem with my code. I try to make 'while' loop which will read all my input values and then stop when i clicked EOF. I have read that EOF in windows is CTRL+Z and so on but my 'while' doesn't want to stop and after all input values and CTRL+Z, it stay and wait for next values. This is my code and hope you will help me thanks):

#include <stdio.h>
#include <stdbool.h> 

#define gc getchar



inline int scan_integer();
inline void zaprzeczenie(bool*);


int main() {
  bool rosnie=true;

  int poprzednia;
  register int terazniejsza;


  terazniejsza = scan_integer();
  poprzednia = terazniejsza;

  int sumaAktualnego=terazniejsza;
  int sumaNajwiekszego=terazniejsza;

  int iloscAktualnego=1;
  int iloscNajwiekszego=0;

  int staly=1;
  int sumaStalego=0;


  while(!feof(stdin))
  {
    printf("%d ",terazniejsza);
    terazniejsza = scan_integer();


    if(terazniejsza<poprzednia){
        if(rosnie){
            if(iloscAktualnego>iloscNajwiekszego){
                iloscNajwiekszego=iloscAktualnego;
                sumaNajwiekszego=sumaAktualnego;
            }
            iloscAktualnego=1;
            sumaAktualnego=terazniejsza;

            if(staly>1){
                iloscAktualnego+=staly;
                sumaAktualnego+=sumaStalego;
                staly=1;
                sumaStalego=0;
            }

            zaprzeczenie(&rosnie);
        }
        else{
            sumaAktualnego+=terazniejsza;
            iloscAktualnego++;
        }
    }
    else if(terazniejsza>poprzednia){
        if(rosnie){
            sumaAktualnego+=terazniejsza;
            iloscAktualnego++;
        }
        else{
            if(iloscAktualnego>iloscNajwiekszego){
                iloscNajwiekszego=iloscAktualnego;
                sumaNajwiekszego=sumaAktualnego;
            }
            iloscAktualnego=1;
            sumaAktualnego=terazniejsza;

            if(staly>0){
                iloscAktualnego+=staly;
                sumaAktualnego+=sumaStalego;
                staly=1;
                sumaStalego=0;
            }

            zaprzeczenie(&rosnie);
        }
    }
    else if(terazniejsza==poprzednia){
        staly++;
        sumaStalego+=poprzednia;
        sumaStalego+=terazniejsza;
        sumaAktualnego+=terazniejsza;
        iloscAktualnego++;
    }

    poprzednia=terazniejsza;
}
  if(iloscAktualnego>iloscNajwiekszego){
      iloscNajwiekszego=iloscAktualnego;
      sumaNajwiekszego=sumaAktualnego;
  }

  printf("%d %d",iloscNajwiekszego, sumaNajwiekszego);
 }

 inline int scan_integer()
{
  register int c = gc();
  int wejsciowa = 0;
  for( ; ((c<48 || c>57)); c = gc() );

  for( ;c>47 && c<58; c = gc() ) {
      wejsciowa = (wejsciowa << 1) + (wejsciowa << 3) + c - 48;
  }
  return wejsciowa;
}

inline void zaprzeczenie(bool* boo){
  boo=!boo;
}

P.S.:Sorry for polish variables)

1 Answers1

1
#define gc getchar

Please don't do this - it makes it harder for others to read and understand your code.

while( !feof( stream )) doesn't work the way most people expect it to, and will wind up looping one too many times. Instead, you need to check the result of your last input operation. Since you're using getchar, you can check the result of that:

while ( (terazniejsza = scan_integer()) != EOF )
{
  ...
}

...

inline int scan_integer()
{
  register int c = gc();
  int wejsciowa = 0;
  for( ; ((c<48 || c>57) && c != EOF ); c = gc() );

  for( ;c>47 && c<58 && c != EOF; c = gc() ) {
      wejsciowa = (wejsciowa << 1) + (wejsciowa << 3) + c - 48;
  }
  return c != EOF ? wejsciowa : c;
}    
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Thanks but it still doesn't work... 'While' doesn't want to stop waiting and seems like scan_ineger doesn't want to return EOF or it return not EOF... – Maks Karashchuk Nov 02 '15 at 15:34