0

I've got a school assignment to program a simulation of self-checkout machine. To keep it on topic, my problem is with inputting the values of coins/notes.

The way the input is suppossed to work is that you enter it into a single line and end the line with 0, or you can end it by pressing Ctl+d once (specifically said once)

What I've is this:

#inlcude <stdio.h>

void main(){
    double number=0, sum=0;

    while(scanf("%lf", &number)==1 && number!=0){
        //do some stuff with number to check if it's a valid coin        

        sum+=number;
    }
}

It work fine if I end the line with a 0, but if I try to end with Ctrl+d, I have to press it twice. I'm not the only in my class who has this problem, but the teacher says she got it working by using scanf() and the way she has it only needs one Ctrl+d.

Is there a way of doing so?

Dávid Čano
  • 81
  • 1
  • 2
  • 3
  • 1
    In Windows, the equivalent Ctrl-Z only works as the first entry after a newline entry. – Weather Vane Nov 04 '18 at 22:55
  • 1
    `while(scanf("%lf", &number)==1 && number!=0 && number!=0x04)`? Assuming `0x04 == CTRL+D`. – Fiddling Bits Nov 04 '18 at 22:55
  • Yes it should equaI that and I tried adding (number!=0x04) but it still requires me to press it twice. – Dávid Čano Nov 04 '18 at 23:03
  • 1
    @FiddlingBits That makes no sense. – melpomene Nov 04 '18 at 23:04
  • @melpomene I realize that now, as `number` is a `double`. – Fiddling Bits Nov 04 '18 at 23:05
  • Ctrl-D serving as an end-of-file signal is a function of the (UNIX) terminal driver. So are you using an unusual terminal? WSL, for instance? – John Bollinger Nov 04 '18 at 23:05
  • 1
    Wait, a single line? Are you pressing Enter before Ctrl-D? – melpomene Nov 04 '18 at 23:10
  • If you've hit the return key for the previous line of input, then a single control-D should terminate the input. If you've not yet hit return, you need to hit control-D twice. If you want to cheat, you could map control-D to interrupt so that when you type control-D, your program stops (and ignores any input on the last line) but it is unlikely to be what your teacher had in mind. – Jonathan Leffler Nov 04 '18 at 23:11
  • Note that `void main()` is wrong on Unix systems, and dubious (but documented) on Windows systems. See [What should `main()` return in C and C++](https://stackoverflow.com/questions/204476/) for the sordid details. Using Control-D to indicate EOF strongly suggests you're working on a Unix machine, so `void main()` is simply wrong. – Jonathan Leffler Nov 04 '18 at 23:13
  • 1
    Your description of the input format sounds strange, and does not seem to match the program. I think you've misunderstood. I think the idea is that you're supposed to enter one nonzero number *per line*, and to end by entering a line containing (only) zero or by typing (only) Ctrl-D. In fact the program will accept a broader range of input format than that, however, so perhaps that contributed to your confusion. – John Bollinger Nov 04 '18 at 23:22
  • 1
    @JohnBollinger: You may find it interesting to know control-D does not actually signal EOF. Control-D tells the terminal driver to send all buffered (typed) characters immediately. You can do this in the middle of a line to send characters then instead of when they are normally sent (when enter/return is pressed). When you press control-D at the beginning of a line, there are no characters buffered, so the program’s pending I/O is completed with zero bytes read, which gets sent to the C code as an EOF. That is also why two control-Ds act as an EOF. – Eric Postpischil Nov 04 '18 at 23:43
  • 2
    @eric: Ctrl-D is the default setting for the Posix `EOF` character; it can be altered with `tcsetattr` by changing the value of `c_cc[VEOF]` in the `termios` struct.. [Posix base defs] (http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap11.html#tag_11_01_09), descibes the effect of the `EOF` character: "...if there are no bytes waiting..., a byte count of zero shall be returned from the `read()`, **representing an end-of-file indication**." (Emphasis added.) Your description is precise, but it seems to me that if Posix can call that an end-of-file indication, so can John. – rici Nov 05 '18 at 02:32
  • @JonathanLeffler and how would I do that? I'm still going to look for a way to get it working without it, but just out of curiosity. – Dávid Čano Nov 05 '18 at 08:40
  • Presuming that you want to make the control-D into the interrupt key, then you use the [`tcgetattr()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/tcgetattr.html) and [`tcsetattr()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/tcsetattr.html) functions and the [``](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/termios.h.html) header etc to adjust both the EOF character (to something other than control-D) and the interrupt character to control-D. – Jonathan Leffler Nov 05 '18 at 08:45
  • See also [rici](https://stackoverflow.com/users/1566221/rici)'s [comment](https://stackoverflow.com/questions/53146309/how-to-end-input-with-only-one-ctrld?noredirect=1#comment93186203_53146309). – Jonathan Leffler Nov 05 '18 at 08:46
  • @rici: I wrote solely about what control-d does. I wrote nothing about what it should be called. – Eric Postpischil Nov 05 '18 at 12:02

0 Answers0