echo charecter program ... [英] echo charecter program...

查看:59
本文介绍了echo charecter program ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在一个小程序下方回显用户

类型的字符。它工作正常,但在每个

字符之间打印一个额外的提示。我能做什么?我是C新手,我的书非常难以支付...

感谢您的帮助。


#包括< stdio.h>


main()

{

char a;

while (1)

{

printf(&\\; \ n请输入一个字符:");

scanf("%c" ,& a);

printf(\ nYou typed:%c,a);

}

}

解决方案

7月13日下午1点10分,hdsalbki< hdsal ... @ gmail.comwrote:


大家好,

我有一个小程序,用来回显用户

类型的字符。它工作正常,但在每个

字符之间打印一个额外的提示。我能做什么?我是C新手,我的书非常难以支付...

感谢您的帮助。


#包括< stdio.h中>



这里的空间也不错,

#include< stdio.h>

注意两者都是有效的,但前者被认为是坏的风格,有些是b $ b。


main()



将其更改为int main(void)


{

char a;

while(1)

{

printf(" \\\
Please type a character:");



你应该fflush(stdout);在那之后printf()。


scanf("%c",& a);



您应该检查scanf()的返回值。


printf(" \ nYou typed:%c",a);

}


}



以下是修复:


#include< stdio.h>


int main(无效){


char c;


while(1){

printf(请输入一个字符:);

fflush(stdout);

if(scanf("%c%* [^ \\ \\ n \\ n]%* c")< 1)break;

printf("您键入:%c \ n",a);

}


返回0;

}


vi ****** @ gmail.com 写道:


7月13日晚上1点10分,hdsalbki < hdsal ... @ gmail.comwrote:


>大家好,
我在一个小程序下面回显用户的字符
类型。它工作正常但在每个
字符之间打印一个额外的提示。我能做什么?我是C新手,我的书很难......
感谢您的帮助。

#include< stdio.h>



这里的空间也不错,

#include< stdio.h>

注意两者都是有效的,但前者被认为是坏的风格,有些是b $ b。


> main()



将其更改为int main(void)
< blockquote class =post_quotes>
> {
char a;
while(1)
{/> printf(" \\\
Please type a character:" );



你应该fflush(stdout);在那之后printf()。


>的scanf(QUOT;%C",&安培;一个);



您应该检查scanf()的返回值。


> printf(" \ n你键入:%c",a);
}

}




以下是修复:



不完全


#include< stdio.h>


int main(无效){


char c;


while(1){

printf(请输入一个字符:");

fflush(stdout);

if(scanf("%c%* [ ^ \ n]%* c")< 1)中断;



if(scanf("%c%* [^ \ n]%* c",& c)< 1)break;


printf(" You typed:%c\ n",a);



printf("你输入:%c \ n",c);


}


返回0;

}



再见,Jojo




vipps ... @ gmail.com写道:


7月13日,下午1:10,hdsalbki< hdsal ... @ gmail.comwrote:


大家好,

我有一个小程序回显用户

类型的字符。它工作正常,但在每个

字符之间打印一个额外的提示。我能做什么?我是C新手,我的书非常难以支付...

感谢您的帮助。


#包括< stdio.h中>



这里的空间也不错,

#include< stdio.h>

注意两者都是有效的,但前者被认为是坏的风格,有些是b $ b。


main()



将其更改为int main(void)


{

char a;

while(1)

{

printf(" \\\
Please type a character:");



你应该fflush(stdout);在那之后printf()。


scanf("%c",& a);



您应该检查scanf()的返回值。


printf(" \ nYou typed:%c",a);

}


}




以下是修复:


#include< stdio.h>


int main(无效){


char c;


while(1){

printf(请键入一个字符:);

fflush(stdout);

if(scanf("%c%) * [^ \ n]%* c")< 1)休息;

printf("您输入:%c \ n",a);

}


返回0;

}



非常感谢vippstar!我真的为这个

问题摸不着头脑,我的书中没有提到scanf()将\\\
留在

输入中。我会做你建议的修复,但我不能理解

你的scanf()调用%c之后......所有这些角色做了什么?

你能解释一下吗?再次感谢。


hi everyone,
I have below a small program to echo back what character the user
types. It''s working OK but prints a extra prompt between every
character. what can I do? I''m new in C and my book is very
difficult...
thanks for any help.

#include<stdio.h>

main()
{
char a;
while(1)
{
printf("\nPlease type a character: ");
scanf("%c",&a);
printf("\nYou typed: %c",a);
}
}

解决方案

On Jul 13, 1:10 pm, hdsalbki <hdsal...@gmail.comwrote:

hi everyone,
I have below a small program to echo back what character the user
types. It''s working OK but prints a extra prompt between every
character. what can I do? I''m new in C and my book is very
difficult...
thanks for any help.

#include<stdio.h>

A space here would not be bad,
#include <stdio.h>
Note that both are valid, but the former is considered bad style by
some.

main()

Change that to int main(void)

{
char a;
while(1)
{
printf("\nPlease type a character: ");

You should fflush(stdout); after that printf().

scanf("%c",&a);

You should check the return value of scanf().

printf("\nYou typed: %c",a);
}

}


Here is the fix:

#include <stdio.h>

int main(void) {

char c;

while(1) {
printf("Please type a character: ");
fflush(stdout);
if(scanf("%c%*[^\n]%*c") < 1) break;
printf("You typed: %c\n", a);
}

return 0;
}


vi******@gmail.com wrote:

On Jul 13, 1:10 pm, hdsalbki <hdsal...@gmail.comwrote:

>hi everyone,
I have below a small program to echo back what character the user
types. It''s working OK but prints a extra prompt between every
character. what can I do? I''m new in C and my book is very
difficult...
thanks for any help.

#include<stdio.h>

A space here would not be bad,
#include <stdio.h>
Note that both are valid, but the former is considered bad style by
some.

>main()

Change that to int main(void)

>{
char a;
while(1)
{
printf("\nPlease type a character: ");

You should fflush(stdout); after that printf().

> scanf("%c",&a);

You should check the return value of scanf().

> printf("\nYou typed: %c",a);
}

}



Here is the fix:

not quite

#include <stdio.h>

int main(void) {

char c;

while(1) {
printf("Please type a character: ");
fflush(stdout);
if(scanf("%c%*[^\n]%*c") < 1) break;

if(scanf("%c%*[^\n]%*c", &c) < 1) break;

printf("You typed: %c\n", a);

printf("You typed: %c\n", c);

}

return 0;
}

Bye, Jojo




vipps...@gmail.com wrote:

On Jul 13, 1:10 pm, hdsalbki <hdsal...@gmail.comwrote:

hi everyone,
I have below a small program to echo back what character the user
types. It''s working OK but prints a extra prompt between every
character. what can I do? I''m new in C and my book is very
difficult...
thanks for any help.

#include<stdio.h>

A space here would not be bad,
#include <stdio.h>
Note that both are valid, but the former is considered bad style by
some.

main()

Change that to int main(void)

{
char a;
while(1)
{
printf("\nPlease type a character: ");

You should fflush(stdout); after that printf().

scanf("%c",&a);

You should check the return value of scanf().

printf("\nYou typed: %c",a);
}

}



Here is the fix:

#include <stdio.h>

int main(void) {

char c;

while(1) {
printf("Please type a character: ");
fflush(stdout);
if(scanf("%c%*[^\n]%*c") < 1) break;
printf("You typed: %c\n", a);
}

return 0;
}

Thank you so much vippstar! I was really scratching my head with this
problem and my book said nothing about scanf() leaving the \n in
input. I will do the fixes you suggest but I just can''t understand
your scanf() call after the "%c"...what do all those characters do?
can you please explain? thanks again.


这篇关于echo charecter program ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆