我的下面的小程序将从用户获取5个数字,将它们存储到整数数组中并使用函数将它们打印出来.真诚地它不起作用并且没有打印出来.我找不到错误,所以我很乐意提出任何建议.谢谢.
#include <stdio.h>
void printarray(int intarray[], int n)
{
int i;
for(i = 0; i < n; i ++)
{
printf("%d", intarray[i]);
}
}
int main ()
{
const int n = 5;
int temp = 0;
int i;
int intarray [n];
char check;
printf("Please type in your numbers!\n");
for(i = 0; i < n; i ++)
{
printf("");
scanf("%d", &temp);
intarray[i] = temp;
}
printf("Do you want to print them out? (yes/no): ");
scanf("%c", &check);
if (check == 'y')
printarray(intarray, n);
getchar();
getchar();
getchar();
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
将输出更改printarray()
为:
printf("%d\n", intarray[i]);
^^
Run Code Online (Sandbox Code Playgroud)
这将在每个数字后添加换行符.
通常,在C中写入控制台的输出将被缓冲,直到输出完整的行.您的printarray()
函数不会写任何换行符,因此输出会被缓冲,直到您打印出来.但是,在打印换行符之前,请等待用户输入.
小智 5
改为:
char check[2];
Run Code Online (Sandbox Code Playgroud)
还有:
scanf("%s", check);
if (!strcmp(check,"y"))
printarray(intarray, n);
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.你scanf("%c", &check);
失败了.而不是y
你最终拥有NL
(ASCII码10
),这意味着该if
部分失败.
我不知道它是否是一个很好的解决方案.也许有人可以给一个更好的.请记住,如果你输入更大的东西(例如yess
),你会有点不走运;)
归档时间: |
|
查看次数: |
267 次 |
最近记录: |