0

My program in C++ doesn't stop on if(scanf()==EOF)break;, below is sketch of my program, for example input:

X XjhgXjhX

gives output:

jhgjh

that is - it prints all characters except X, but it doesn't stop on Ctrl+Z.

using namespace std;

int main()
{
    char str[100]={0},znak,forbd;
    int i=0,j=0;
    while(true)
    {
        i=0;
        j=0;

        if(scanf("%c",&forbd)==EOF)
            break;

        if(scanf("%c",&znak)==EOF)
            break;

        while(znak!='\n')
        {
            if(forbd!=znak && znak!=' ')
            {
                str[i]=znak;
                i++;
                //cout<<i<<"\n";
            }

            if(scanf("%c",&znak)==EOF)
                break;
        }

        while(j<i)
        {
            printf("%c",str[j]);
            j++;
        }
        printf("%c",'\n');
    }
    return 0;   
}

I don't want to use cin, because of trouble with reading \n.

Qbik
  • 5,885
  • 14
  • 62
  • 93
  • 5
    I think you should rather ask about solving your problem with `cin` instead. – moooeeeep Mar 13 '13 at 18:31
  • @moooeeeep but what about `cin` and `\n` issue then ? I prefer `scanf` because it's really faster. – Qbik Mar 13 '13 at 18:32
  • @Qbik: What is the issue with cin and `\n`? For the above code I doubt you would see any difference in performance. C++ streams are slower for 2 reasons 1) They bind to stdin/stdout (unbind them and it becomes quicker) 2) The formatted read on streams do a lot more work with locals (so you pay for the work). If you don't care about that then don;t use formatted reads. They are still slightly slower but not what I would call significant enough to worry about. – Martin York Mar 13 '13 at 18:38
  • @Loki `scanf` also does work with locales. I don’t think `cin` is any slower. In fact, I’d expect it to be faster since it doesn’t need to parse its formatting arguments at runtime. Using `scanf` for performance reasons is simply idiotic. – Konrad Rudolph Mar 13 '13 at 18:44
  • @KonradRudolph: I totally agree. But there is always somebody that wants to argue about it (and I don't care). So adding the last line pacifies them. As far as I can tell there is no *significant* performance differences and the added type safety is more than enough compensation. – Martin York Mar 13 '13 at 18:48
  • @Konrad Rudolph on SPOJ codes with `iostream` are often too slow – Qbik Mar 13 '13 at 18:49
  • Did you try Ctrl-D? (if this is a UniClone)? – WhozCraig Mar 13 '13 at 18:50
  • @Qbik I’m fairly certain that this is caused by incompetence rather than iostream (most likely culprit: sync with stdio). There are plenty of benchmarks which demonstrate that their performance is on par. – Konrad Rudolph Mar 13 '13 at 18:50
  • @Konrad Rudolph I'm too novice too judge it, but I believe you, I was reading a few benchmarks – Qbik Mar 13 '13 at 18:52
  • 2
    The above code, after fixing the syntax errors, [works just fine for me](http://ideone.com/YsYlYJ). Maybe it's a Windows thing. – Robᵩ Mar 13 '13 at 18:53
  • @WhozCraig it's pure XP – Qbik Mar 13 '13 at 18:54
  • 1
    Found it: article that examines stdin Vs std::cin shows no difference as long as you unbind them: http://stackoverflow.com/a/9747716/14065 – Martin York Mar 13 '13 at 18:54
  • I am still interested to know what the issue with '\n' is? – Martin York Mar 13 '13 at 18:56
  • The considering you never read more than one char, why not just use `fgetc(stdin)` ? – WhozCraig Mar 13 '13 at 18:57
  • @Rob it still doesn't work for me – Qbik Mar 13 '13 at 18:58
  • What does 'it doesn't work' mean? What, precisely, do you type? What, precisely, do you observe? – Robᵩ Mar 13 '13 at 18:58
  • @Rob it still doesn't stop on `Ctrl+Z` – Qbik Mar 13 '13 at 19:02
  • What does 'it still doesn't stop' mean? Key-by-key, what do you type? What do you see? How do you conclude that it doesn't stop? – Robᵩ Mar 13 '13 at 19:05
  • @Rob a asa`enter` `ctrl+z`it doesn't break main `while` loop. – Qbik Mar 13 '13 at 19:07
  • I believe that you are missing one keystroke. IIRC, in order to send an EOF to a Windows console program, you have to type `enter`, `ctrl+z`, `enter`. – Robᵩ Mar 13 '13 at 19:08

2 Answers2

0

ok, I'm going to use iostream instead of cstdio with code if(cin.peek()=='\n')break; detecting end of line, this solves issue.

Qbik
  • 5,885
  • 14
  • 62
  • 93
0

scanf reads from stdin reference witch:

stdin is known to not refer to an interactive device, the stream is fully buffered. Otherwise, it is library-dependent whether the stream is line buffered or not buffered by default (see setvbuf).

Usually, stdin is line buffered and you need to type '\n' to read.

From an correlated question you can find solutions for this type use case of character reading.

Community
  • 1
  • 1
lgabriellp
  • 82
  • 2
  • 5