关于输入的奇怪问题

tufanshu 2003-08-22 02:54:11
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "c_funcs.h"


void write_head();
int x_max,x_min;
int y_max,y_min;
int x_abs,y_abs;
long unsigned total_sti;
void main()
{
FILE *fp; //指向输入文件的指针
int i;
char pre; //通常反复优先方向

char infile[15];
char headfile[13];
float x_times,y_times;
int direction,angle;

int head_tail;


int r_xtimes,r_ytimes;
int rep_xdis,rep_ydis;

for (i=0;i<13;i++)
headfile[i]=' ';
printf("\n");
printf("please input filename\n");
scanf("%s",infile);
if ((fp=fopen(infile,"rb"))==NULL) //原文件
{
printf("can't open file ");
exit(0);
}
printf("\n");


//readfileinfo(fp);
//-------------------------输入各种参数--------------------


printf("please input first direction(x~y):"); //优先方向
scanf("%c",&pre);
/*就这里有问题!!!!每次程序都直接跳过,也就是不执行这个scanf语句我觉得很奇怪????其它的都正常*/

printf("please input head to tail times(1~99):"); //首尾反复
scanf("%d",&head_tail);
printf("please input rep_x times(1~9):"); //x方向反复次数
scanf("%d",&r_xtimes);
printf("please input rep_y times(1~9):"); //y方向反复次数
scanf("%d",&r_ytimes);
printf("please input x_displace(-999.9~+999.9):");
scanf("%d",&rep_xdis);
printf("please input y_displace(-999.9~+999.9):");
scanf("%d",&rep_ydis);
printf("----%c",pre);

printf("please input x_direction times:");
scanf("%f",&x_times);
printf("please input y_direction times:");
scanf("%f",&y_times);
printf("please input rotate direction:(0~7)");
scanf("%d",&direction);
printf("please input rotate angle:(0~89)");
scanf("%d",&angle);

//-----------------------------------------

setupstitch(fp,x_times,y_times,direction,angle,head_tail,pre,
r_xtimes,r_ytimes,rep_xdis,rep_ydis);

//--------------------------------------
printf("X+%d,X-:%d,Y+:%d,Y-:%d",x_max,x_min,y_max,y_min);

fclose(fp);

}

不知道各位大虾们又没有碰到这种问题,我如果在出问题的那里改成这样
while ((pre!='x')&&(pre!='y'))
{
printf("please input first direction(x~y):");
scanf("%c",&pre);
printf("%c \n",pre);
}
则循环一次以后就可以正确读入字符 ‘pre’;


...全文
57 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuhelh 2003-08-22
  • 打赏
  • 举报
回复
将scanf("%c",&pre) 改为scanf(" %c",&pre)就可以了
我以前也遇到过这样的问题
aflyinghorse 2003-08-22
  • 打赏
  • 举报
回复
scanf("%s",infile);
这一句之后应该加一句getchar()来读回车健
否则scanf("%c",&pre); 读的就是回车
yzb1000 2003-08-22
  • 打赏
  • 举报
回复
可以用scanf("%d",&pre);
yzb1000 2003-08-22
  • 打赏
  • 举报
回复
不要用scanf("%c",&pre);
它会接受回车符的
在这之前有
scanf("%s",infile);这个,你输入的时候当然要敲回车的啊
tufanshu 2003-08-22
  • 打赏
  • 举报
回复
简单问题帮帮忙,大哥们!
tufanshu 2003-08-22
  • 打赏
  • 举报
回复
up

69,371

社区成员

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

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