为什么我的程序接受一个整数太多而输入一个太少? [英] Why does my program accept one integer too many and input one too few?
本文介绍了为什么我的程序接受一个整数太多而输入一个太少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想了解为什么当我将SIZE定义为2时,程序允许我输入3个整数.返回数组时,它仅返回两个数字而不是我输入的三个数字.感谢您的帮助./p>
I'd like to understand why the program allows me to input 3 integers when I have defined SIZE as 2. And when it returns the array it only returns two numbers NOT the three I have entered.Thanks for your help.
//C How to Program Exercises 2.23
#include <stdio.h>
#include <conio.h>
#define SIZE 2
int main (void){
int myArray[SIZE];
int count;
printf("Please enter 5 integers\n");
for (count=1;count<=SIZE;count++){
scanf("%d\n",&myArray[count]);
}
for (count=1;count<=SIZE;count++){
printf("The values of myArray are %d\n",myArray[count]);
}
getch();
return 0;
}
推荐答案
您的循环应该是
for (count=0;count<SIZE;count++)
数组索引是基于C的0
.
由于在scanf()
调用中有一个空白字符(\n
),它将等待您输入一个非空白字符以完成每个调用.删除\n
:
Since you have a whitespace chatacter (\n
) in scanf()
call, it waits for you input a non-whitespace character to complete each call. Remove the \n
:
for (count=0;count<SIZE;count++){
scanf("%d",&myArray[count]);
}
这篇关于为什么我的程序接受一个整数太多而输入一个太少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文