Averaging 3 integers

后端 未结 4 1635
花落未央
花落未央 2021-01-22 01:16

My assignment is to fix the code. I have my edited code below and the original code below that. I figure I still have a few errors in here. My error checking doesnt seem to w

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-22 01:51

    The issues with the original program are:

    1. getchar() returns an ASCII code, and the condition was wrong. When checking for boundaries, use ((c<'0') || (c>'9')).
    2. For the exit function you need to include "stdlib.h".
    3. For the main function to understand what is get_number, you need to either move the function to be before the main function, or you can use a forward declaration.
    4. The assignment operator (=) has lower precedence than the inequality operator (!=), so you need to use parenthesis, like: ((c = getchar()) != '\n')

    You have actually created several more issues, so I wouldn't rely on your code.

    In any case, in general - it is advised you study how to use a debugger. Once your code is compiling (and for that you'll need to get accustomed to the compilation error messages), you need to start debugging your code. I suggest you take some time to learn how to set breakpoints, set watches, and go step by step into your code. That skill is absolutely essential for a good developer.

提交回复
热议问题