while (scanf("%d",&i)==1) 是什么意思?

匚匚 2009-11-21 03:19:38
不知道while (scanf("%d",&i)==1) 是什么意思?要如何结束?goto 语句运用是否得当?

#include "stdio.h"
int main(void)
{
int i;
hehe:
while (scanf("%d",&i)==1) /*如何正常结束输入?*/
printf("%d\n",i);
printf("输入错误!");
printf("是否继续Y/N?");
if (getchar()=='y') goto hehe; /*goto 语句运用是否得当?*/
getch();
return 0;
}
...全文
5856 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
匚匚 2009-11-21
  • 打赏
  • 举报
回复
为什么一用free(p); 就出错?
#include "stdio.h"
#include "stdlib.h"
#define LEN 5;
int main(void)
{
int i,*p;
int n=0,size=LEN;
p=malloc(size*sizeof(int));
hehe: while (scanf("%d",&i)!=EOF) /*CTRL+Z 正常结束输入?*/
{
n+=1;
if (n>size)
{
size+=LEN;
p=realloc(p,size*sizeof(int));
if (p==NULL) return ;
}
p[n]=i;
}
if (n<size)
{
p=realloc(p,n*sizeof(int));
if (p==NULL); return;
}
p[0]=n; /*首元素为输入有效数字数*/

printf("是否打印此数组:Y/N?");
if (getchar()=='y'||getchar()=='Y')
for (i=0;i<=n;i++) printf("%d ",p[i]);
printf("\n");
goto hehe;
free(p); /*此名为什么不能放在这儿?*/
return 0;
}
swimingzhou 2009-11-21
  • 打赏
  • 举报
回复
我试了一下.vc6.0中好象go to 没作用啊.直接"Press any key to continue"
挺无语的....还有while (scanf("%d",&i)==1) printf("%d\n",i);强制转换类型,还是会输出....
匚匚 2009-11-21
  • 打赏
  • 举报
回复
给大家看一个函数吧:
#define LEN 100

int *zwint(void)
{
int *array;
int size,count,value;
size=LEN;
array=malloc((size+1)*sizeof(int));
if(array==NULL) return NULL;

count=0;
while(scanf("%d",&value)==1)
{
count+=1;
if(count>size)
{
size+=LEN;
array=realloc(array,(size+1)*sizeof(int));
if(array==NULL) return NULL;
}
array[count]=value;
}
if(count<size)
{
array=realloc(array,(count+1)*sizeof(int));
if(array==NULL); return NULL;
}
array[0]=count;
return array;
}
匚匚 2009-11-21
  • 打赏
  • 举报
回复
输入浮点数后,即使输入Y,也不能继续的,这就是如何正常退出WHILE循环的问题
东大坡居士 2009-11-21
  • 打赏
  • 举报
回复
输入一个字符啥的就退出了啊~~~
匚匚 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zmin002 的回复:]
引用 5 楼 zhw952 的回复:
那要如何正常结束循环呢?

printf("是否继续Y/N?");
结束后,不要输入Y,输入其它字符就可以结束了。

[/Quote]
我说的不是goto,是这一对循环体: while (scanf("%d",&i)==1) /*如何正常结束输入?*/
printf("%d\n",i);
proxukun 2009-11-21
  • 打赏
  • 举报
回复
首先感谢楼主给了我灵感!!!!!
#include <stdio.h>
void main()
{
int i;
hehe:
if ((scanf("%d",&i))==1)
printf("%d\n",i);
else
{
printf("the number is wrong.\n");
printf("do you want to continue?y or n.\n");
if (getchar()=='y')
goto hehe;
}
}


大家帮忙看看...只是貌似现在不提倡使用goto语句了....



Apple_Demo 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhw952 的回复:]
那要如何正常结束循环呢?
[/Quote]
printf("是否继续Y/N?");
结束后,不要输入Y,输入其它字符就可以结束了。
匚匚 2009-11-21
  • 打赏
  • 举报
回复
那要如何正常结束循环呢?
benbshmily 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hu50486066 的回复:]
while (scanf("%d",&i)==1)
输入一个数赋变量i,然后判断变量i是否等于1 等于1就继续输入,不等于1就跳出循环。
goto的用法是正确的。
[/Quote]
否。
scanf()函数返回的值的意义为:正确按指定格式输入变量的个数;也即能正确接收到值的变量个数。
就是说正确接收值的个数。此时scanf("%d",&i)要求输入一个int数。因此只要按格式输入一个int数,结果都是1.
假如scanf("%d%d%d",&i,&j,&k)。你输入时10 20 d则scanf返回为2
kouwenlong 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hu50486066 的回复:]
while (scanf("%d",&i)==1)
输入一个数赋变量i,然后判断变量i是否等于1 等于1就继续输入,不等于1就跳出循环。
goto的用法是正确的。
[/Quote]
不要误导Lz。
看看scanf的返回值。
Return Value
On success, the function returns the number of items succesfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read, EOF is returned.
honghu069 2009-11-21
  • 打赏
  • 举报
回复
scanf 的返回值 是输入的个数
while (scanf("%d",&i)==1)
判断是否读到文件结束
hu50486066 2009-11-21
  • 打赏
  • 举报
回复
while (scanf("%d",&i)==1)
输入一个数赋变量i,然后判断变量i是否等于1 等于1就继续输入,不等于1就跳出循环。
goto的用法是正确的。

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧