联通老用户充话费:请问一个C语言程序的问题?

来源:百度文库 编辑:高考问答 时间:2024/04/28 05:34:08
#include "stdio.h"
main()
{
int i,j,num=0;
char choose;
char s[50][20];
printf("input\n");
for(i=0;i<50;i++)
{
j=0;
do{
s[i][j]=getchar();
j++;
}while(s[i][j-1]!='\n');
num++;
printf("Do you want to go on y ore n:\n");
choose=getchar();
if(choose!='y')
break;
}
for(i=0;i<num;i++)
{
printf("******the %d th person is:\n",i);
for(j=0;j<20&&s[i][j]!='\n';j++)
putchar(s[i][j]);

}
}

上面是我的C程序,我是想通过程序输入几个人的姓名(字符串表示),每输入一个后回车,通过Do you want to go on y ore n提示是否再输入,最后显示输入的人名。可当输入完一个后,输入'y'后,程序却再一次出现Do you want to go on y ore n提示,我知道这是由于choose=getchar();里面的回车导致的,可请问:怎么解决哪?才能达到我想要的功能?
choose=getchar();
getchar();
或者
scanf("%c",&choose);
都已经试过了,不行!还是有回车。

这个我调试通过的,但接收字符确实有问题,改用数值就正常了,有可能是缓冲区要清空的问题,或者使用getch()函数试试,getch()不用回车的,楼上说错了,试编译通过的s[i]是指针值,配%s选项打印字符串
#include "stdio.h"
#include "malloc.h"
int main()
{
int i,num=0;
int choose;
char *s[50];
printf("input\n");
for(i=0;i<50;i++)
{
printf("Do you want to go on 1 or 0:\n");

scanf("%d",&choose);

if(choose!=1)
break;
s[i]=(char *)malloc(sizeof(char)*256);

scanf("%s",s[i]);
num++;
}
for(i=0;i<num;i++)
{
printf("******the %d th person is:",i);
printf("%s\n",s[i]);
}
}

for(i=0;i<num;i++) //改成for(i=0;i<=num;i++)
{
printf("******the %d th person is:",i);
printf("%s\n",s[i]);
}

不能编译 自己看的

#include \"stdio.h\"
void main()
{
int i,j,num=0;
char choose;
char s[50][20];
printf(\"input\\n\");
for(i=0;i<50;i++)
{
j=0;
do{
s[i][j]=getchar();
j++;
}while(s[i][j-1]!=\'\\n\');
num++;
printf(\"Do you want to go on y ore n:\\n\");
choose=getchar();
if(choose==\'y\')
{
choose=getchar();
if(choose==\'\\n\')//这个是我加的
{
choose=getchar();
}
}
else break;

}
for(i=0;i<num;i++)
{
printf(\"******the %d th person is:\\n\",i);
for(j=0;j<20&&s[i][j]!=\'\\n\';j++)
putchar(s[i][j]);

}
}
这个在得到y字符时,你在得到一个字符判断他是不是回车就好了!