how does programme truly work when running the loop to read and output chars

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KITE
    New Member
    • Apr 2020
    • 1

    how does programme truly work when running the loop to read and output chars

    (scroll down to see the codes)

    I am writing a programme to read chars and output the new chars with continuous spaces being eliminated until one( say 'tes t' gonna to be 'tes t'

    And then i found that the programme can't be stopped when i gave the former input on GDB(a online compiler)

    My question is that what exactly happen after i press the key 'Enter', Does that key create a '\n' at the end of the input and gives the programme a command to start to run?

    it is confusing to me because it's different to the function 'input()' in python, which have a timing to input. i thought the chars should be input firstly but why the programme keep asking input after calculating?
    And how can i stop the following code to tell the programme my input ends?( namely, to make my last char == EOF )

    really thankful if someone can answer because i know it could be a commonly basic question to most of programmers.


    #include <stdio.h>

    int main()
    {
    int c;
    int button = 0;
    while((c=getcha r())!= EOF){
    if (c==' '&&button == 1){
    continue;
    }
    putchar(c);
    if (c ==' '){
    button = 1;
    }
    else{
    button = 0;
    }



    }

    return 0;
    }
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    EOF character may not be recognized in the console unless followed up with the enter key. It can be sent to the program with Ctrl-D (Unix) and Ctrl-Z (Microsoft).

    Comment

    Working...