如何将用户希望的适当输入附加为整数到一个数组中,在C语言中?

huangapple go评论85阅读模式
英文:

How do i append proper input as integers to an array in C, as long the user wishes to?

问题

  1. int main(){
  2. char answer;
  3. int numbers[100];
  4. int i = 0;
  5. int size;
  6. int max = -9999;
  7. do{
  8. printf("请输入一个数字:");
  9. scanf("%d", &numbers[i]);
  10. printf("是否继续添加数字:(Y/N)");
  11. scanf(" %c", &answer); // 注意加一个空格,避免之前输入的换行符被读取
  12. i++;
  13. }while(answer == 'Y');
  14. size = sizeof(numbers)/sizeof(numbers[0]);
  15. for(int j = 0; j<size; j++){
  16. if(numbers[j]>= max){
  17. max = numbers[j];
  18. }
  19. }
  20. printf("最大的数字是:%d", max);
  21. return 0;
  22. }
英文:
  1. int main(){
  2. char answer;
  3. int numbers[100];
  4. int i = 0;
  5. int size;
  6. int max = -9999;
  7. do{
  8. printf(&quot;Please enter an number: &quot;);
  9. scanf(&quot;%d&quot;, &amp;numbers[i]);
  10. printf(&quot;Would you like to keep adding numbers:(Y/N)&quot;);
  11. scanf(&quot;%c&quot;, &amp;answer);
  12. scanf(&quot;%c&quot;);
  13. i++;
  14. }while(answer == &#39;Y&#39;);
  15. size = sizeof(numbers)/sizeof(numbers[0]);
  16. for(int j = 0; j&lt;size; j++){
  17. if(numbers[j]&gt;= max){
  18. max = numbers[j];
  19. }
  20. }
  21. printf(&quot;The max number is: %d&quot;, max);
  22. return 0;
  23. }

Hello beginner in C, here in my code i am trying to take an arbitrary amount of (the user enters Y if he/she wishes to enter another number.) input as integers and add them to an array and find the maximum of the input using a for loop, however i am not getting the correct output. What could be the error in my code?

答案1

得分: 2

问题包括:

在预期为字母时读取 &#39;\n&#39;

scanf(&quot;%c&quot;, &amp;answer); 在先前输入数字(例如:<kbd>9</kbd>)之后读取字符,这很可能是先前输入的 `'\n'' <kbd>Enter</kbd>。

使用空格来消耗前导空白字符,如 &#39;\n&#39;',空格,制表符...

  1. // scanf(&quot;%c&quot;, &amp;answer);
  2. scanf(&quot; %c&quot;, &amp;answer);

启用所有警告

无效/不必要的代码 scanf(&quot;%c&quot;); 将在启用了良好编译器的情况下引发编译器警告。

  • 这个答案中的最佳建议:启用所有编译器警告以节省时间。

INT_MIN 开始

最大输入可能小于 -9999。
&lt;limits.h&gt; 中的 INT_MIN

  1. // int max = -9999;
  2. int max = INT_MIN;

迭代到 i

与其迭代到100,只需要迭代到 i,输入的值的计数。

  1. // for(int j = 0; j&lt;size; j++){
  2. for(int j = 0; j&lt;i; j++){

检查返回值

scanf() 返回一个值,表示成功转换的数量。使用它来验证成功发生了输入。

  1. // scanf(&quot;%d&quot;, &amp;numbers[i]);
  2. if (scanf(&quot;%d&quot;, &amp;numbers[i]) != 1) {
  3. ; // 使用待定代码报告错误。
  4. }

不要循环太多次

  1. // } while(answer == &#39;Y&#39;);
  2. } while(answer == &#39;Y&#39; &amp;&amp; i &lt; 100);

没有保存值数组的必要

最大值可以在输入数据时计算。

  1. int max = INT_MIN;
  2. do {
  3. int num;
  4. printf(&quot;请输入一个数字:&quot;);
  5. if (scanf(&quot;%d&quot;, number) != 1) {
  6. break;
  7. }
  8. if (num &gt; max) {
  9. max = num;
  10. }
  11. printf(&quot;是否继续添加数字:(Y/N)&quot;);
  12. if (scanf(&quot; %c&quot;, &amp;answer) != 1) {
  13. break;
  14. }
  15. } while (answer == &#39;Y&#39; || answer == &#39;y&#39;);
  16. printf(&quot;最大数字是:%d&quot;, max);
  17. ----
  18. **未来的改进**
  19. * 处理超出 `int` 范围的值。研究 `intmax_t`
  20. * 检测没有输入有效数据的情况。研究 `fgets()`
  21. * 检测非有效的 `Y/N` 输入。研究 `fgets()`
  22. * 恢复,而不是因无效输入而退出循环。
  23. <details>
  24. <summary>英文:</summary>
  25. Problems include:
  26. **Reading a `&#39;\n&#39;` when a letter is expected**
  27. `scanf(&quot;%c&quot;, &amp;answer);` reads the character after the prior input of a number (example:&lt;kbd&gt;9&lt;/kbd&gt;), which is likely the prior entry&#39;s `&#39;\n&#39;` &lt;kbd&gt;Enter&lt;/kbd&gt;.
  28. Use a space to consume leading white-space like `&#39;\n&#39;`, space, tab, ...
  29. // scanf(&quot;%c&quot;, &amp;answer);
  30. scanf(&quot; %c&quot;, &amp;answer);
  31. **Enable all warnings**
  32. Invalid/unnecessary code `scanf(&quot;%c&quot;);` will raise a compiler warning with a well enabled compiler.
  33. * Best advice in this answer: enable all compiler warnings to save time.
  34. **Start at `INT_MIN`**
  35. The maximum input may be less than -9999.
  36. `INT_MIN` in `&lt;limits.h&gt;`
  37. // int max = -9999;
  38. int max = INT_MIN;
  39. **Iterate to `i`**
  40. Rather than iterate to 100, only need to iterate to `i`, the count of values entered.
  41. // for(int j = 0; j&lt;size; j++){
  42. for(int j = 0; j&lt;i; j++){
  43. **Check return values**
  44. `scanf()` returns a value indicated the number of successful conversions. Use it to validated input successfully happened.
  45. // scanf(&quot;%d&quot;, &amp;numbers[i]);
  46. if (scanf(&quot;%d&quot;, &amp;numbers[i]) != 1) {
  47. ; // Report error with TBD code.
  48. }
  49. **Do not loop too often**
  50. // } while(answer == &#39;Y&#39;);
  51. } while(answer == &#39;Y&#39; &amp;&amp; i &lt; 100);
  52. **There is no reason to save an array of values**
  53. The maximum could be calculated as data is entered.
  54. int max = INT_MIN;
  55. do {
  56. int num;
  57. printf(&quot;Please enter an number: &quot;);
  58. if (scanf(&quot;%d&quot;, number) != 1) {
  59. break;
  60. }
  61. if (num &gt; max) {
  62. max = num;
  63. }
  64. printf(&quot;Would you like to keep adding numbers:(Y/N)&quot;);
  65. if (scanf(&quot; %c&quot;, &amp;answer) != 1) {
  66. break;
  67. }
  68. } while (answer == &#39;Y&#39; || answer == &#39;y&#39;);
  69. printf(&quot;The max number is: %d&quot;, max);
  70. ----
  71. **Future Improvements**
  72. * Handle values outside `int` range. Research `intmax_t`.
  73. * Detect case of no valid input entered. Research `fgets()`
  74. * Detect non-valid `Y/N` input. Research `fgets()`
  75. * Recover, rather than quit loop with invalid input.
  76. </details>

huangapple
  • 本文由 发表于 2023年2月8日 19:49:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定