用scanf输入字符有碰到难题了

云原生商店 2005-12-18 08:41:42


for(count=0;count<n;count++)
{
printf("\ndata:");
scanf("%c",&temp);
Push_SeqStack(S,temp);
}


运行的时候,系统总是跳过一次循环。
出现每两次data系统才提示输入数据
data:
data:_
...全文
192 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
云原生商店 2005-12-18
  • 打赏
  • 举报
回复
fflush(stdin); 解决了 谢谢各位的指教
云原生商店 2005-12-18
  • 打赏
  • 举报
回复
老师们这是咱回事呢?跪拜求教
herman~~ 2005-12-18
  • 打赏
  • 举报
回复
我在我的电脑试了一下,vc6.0,好像不会这样 出现每两次data系统才提示输入数据
云原生商店 2005-12-18
  • 打赏
  • 举报
回复
主要是第一次循环出现两个datan data
接下来的循环正常 因此我每次确定n个循环后,总是只能输入你n-1个数据
csucdl 2005-12-18
  • 打赏
  • 举报
回复
刷新是恢复为初始状态
云原生商店 2005-12-18
  • 打赏
  • 举报
回复
我的程序是模拟堆栈的
/*
本程序实现栈操作,函数Stack_Symmetry()判断输入在字符串是否中心对称
*/

#include <stdio.h>
#include <conio.h>

#define maxsize 128
typedef char datatype;

/*定义堆栈*/
typedef struct
{
datatype data[maxsize];
int top;
}SeqStack;

SeqStack *Init_SeqStack();
int Empty_SeqStack(SeqStack *S);
int Push_SeqStack (SeqStack *S, datatype x);
int Pop_SeqStack(SeqStack *S, datatype *x);
/*取栈顶元素*/
datatype Top_SeqStack(SeqStack *s);
/*判断是否对称*/
int Symmetry_SeqStack(SeqStack *S);
void Print_SeqStack(SeqStack *S);



void main()
{
int count,n;
datatype temp;
SeqStack *S;
S=Init_SeqStack();


printf("\nHow many elements will you add?"); scanf("%d",&n);
if (n>maxsize)
printf("\noverflow!");
else
{

printf("n=%d\n",n);
printf("\nPlease input the data:\n");


for(count=0;count<n;count++)
{
printf("\ndata:");
scanf("%c",&temp);
getch();
Push_SeqStack(S,temp);
}
Print_SeqStack(S);
Symmetry_SeqStack(S);

}
getch();

}

SeqStack *Init_SeqStack()
{
SeqStack *S;
S=malloc(sizeof(SeqStack));
S->top=-1;
return S;
}

int Empty_SeqStack(SeqStack *S)
{
if (S->top==-1)
return 1;
else
return 0;
}


int Push_SeqStack (SeqStack *S, datatype x)
{
if (S->top==maxsize-1) return 0; /*栈满不能入栈*/
else
{
S->top++;
S->data[S->top]=x;
return 1;
}
}


int Pop_SeqStack(SeqStack *S, datatype *x)
{
if (Empty_SeqStack (S)) return 0; /*栈空不能出栈 */
else
{
*x=S->data[S->top];
S->top--; return 1;
} /*栈顶元素存入*x,返回*/
}


datatype Top_SeqStack(SeqStack *S)
{
if (Empty_SeqStack(S)) return 0; /*栈空*/
else return (S->data[S->top] );
}


int Symmetry_SeqStack(SeqStack *S)
{
int j;
if (S->top%2==0)
{printf("not have symmetry!\n");return(-1);}
else
for(j=0;j<S->top/2;j++)
{
if(S->data[j]!=S->data[S->top-j])
{printf("not have symmetry!\n");return(-1);}
}
printf("have symmetry!\n");
return(1);
}



void Print_SeqStack(SeqStack *S)
{
int i=0;
for (i=S->top;i>=0;i--)
{printf("%d %c\n",i,S->data[i]);}
printf("\nS->top=%d\n",S->top);

}
csucdl 2005-12-18
  • 打赏
  • 举报
回复
fflush(stdin)来刷新输入缓冲区
云原生商店 2005-12-18
  • 打赏
  • 举报
回复
我用的是win-tc,一个小软件。可以在windows下编译c程序。说明上说getch()作为main()
的最后一条语句即可
云原生商店 2005-12-18
  • 打赏
  • 举报
回复
我的main()函数如下:

void main()
{
int count,n;
datatype temp;
SeqStack *S;
S=Init_SeqStack();


printf("\nHow many elements will you add?"); scanf("%d",&n);
if (n>maxsize)
printf("\noverflow!");
else
{

printf("n=%d\n",n);
printf("\nPlease input the data:\n");


for(count=0;count<n;count++)
{
printf("\ndata:");
scanf("%c",&temp);
Push_SeqStack(S,temp);
}
Print_SeqStack(S);
Symmetry_SeqStack(S);

}
getch();

}
greenteanet 2005-12-18
  • 打赏
  • 举报
回复
for(count=0;count<n;count++)
{
printf("\ndata:");
scanf("%c",&temp);
getch();
Push_SeqStack(S,temp);
}
RainWindy 2005-12-18
  • 打赏
  • 举报
回复
for(count=0;count<n;count++)
{
printf("\ndata:");
scanf("%c",&temp);
getchar();
Push_SeqStack(S,temp);
}
Mr_Yang 2005-12-18
  • 打赏
  • 举报
回复
会不会是for语句的前面还有printf("\ndata:");

70,022

社区成员

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

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