Stop a for loop when user is finished entering input in c

前端 未结 5 1427
梦如初夏
梦如初夏 2021-01-20 21:27

First of all, thank you for the assist!

I\'m new to the C language (and programming in general) and I\'m trying to write a program wherein the user inputs data point

5条回答
  •  孤街浪徒
    2021-01-20 21:55

    No element of data[] will ever be 'done' (they're floats). If you want to scanf() directly, you'll need to choose a double value that ends the sequence (commonly zero or -1 or something). If that won't work, you can either use something like:

    1. Use fgets() to pull a string, then strncmp() to check for the terminating value and sscanf() to pull out the double, or:
    2. Have the user use Ctrl-D to terminate and check the scan value for EOF.

    Oh, and strictly speaking you have an upper limit of entries. You should check i to make sure that you don't exceed that. Never assume your input won't exceed boundaries. sizeof() on a statically-allocated variable or some #defined macro to track that.

提交回复
热议问题