PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 31, 2022

[FIXED] How i can stop reading values after EOF

 October 31, 2022     c, eof, getchar, stdin     No comments   

Issue

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)


Solution

#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;
}    


Answered By - John Bode
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing