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

前端 未结 5 1488
梦如初夏
梦如初夏 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:39

    • Your data is of type double. It can't scan a literal "done".

    Instead use EOF for checking end of input.

    while(scanf("%lf",&data[i]) != EOF) {
        ...
    }
    

    Another way:

    while(scanf("%lf",&data[i]) == 1) {
        ...
    }
    
    • Another thing, initialize count to zero, i.e. count = 0;

提交回复
热议问题