循环和getchar问题 [英] Loop and getchar problems
问题描述
我是新手程序员。我无法得到以下代码。
/ *
目标:
使用循环打印出所有输入字符,直到找到换行符
.
* /
#include< stdio。 h>
int main(){
/ *声明变量,分配初始值x。 * /
char a =''x'';
while(a!=''\ n''){
/ *获取用户输入* /
a = getchar();
if(a ==''\ n''){
/ *如果输入是一个新行,请告诉用户* /
printf(\ nThat'是今天的。\ n ;);
} else {
/ *打印用户输入* /
printf("%c", a);
}
}
返回1;
}
Hi,
I''m a newbie programmer. I can''t get work the following code.
/*
Objective:
Use a loop to print out all of the input characters until a newline is
found.
*/
#include <stdio.h>
int main (){
/* Declare variable, Assign a initial value x. */
char a = ''x'';
while (a != ''\n'') {
/* Get a user input */
a = getchar();
if (a == ''\n''){
/* If the input is a new line, say good bye to user */
printf("\nThat''s it for today.\n");
} else {
/* Prints a user input */
printf("%c",a);
}
}
return 1;
}
推荐答案
On 6 ??1è?,????5ê±25·?, Samuel.Codd ... @ gmail。 com写道:
On 6??1è?, ????5ê±25·?, Samuel.Codd...@gmail.com wrote:
我是新手程序员。我无法得到以下代码。
/ *
目标:
使用循环打印出所有输入字符,直到找到换行符
.
* /
#include < stdio.h>
int main(){
/ *声明变量,分配初始值x。 * /
char a =''x'';
while(a!=''\ n''){
/ *获取用户输入* /
a = getchar();
if(a ==''\ n ''){
/ *如果输入是一个新行,请告诉用户* /
printf(\ n它今天就是它' 。$ \\ n");
} else {
/ *打印用户输入* /
printf(" %c,a);
}
}
返回1;
}
Hi,
I''m a newbie programmer. I can''t get work the following code.
/*
Objective:
Use a loop to print out all of the input characters until a newline is
found.
*/
#include <stdio.h>
int main (){
/* Declare variable, Assign a initial value x. */
char a = ''x'';
while (a != ''\n'') {
/* Get a user input */
a = getchar();
if (a == ''\n''){
/* If the input is a new line, say good bye to user */
printf("\nThat''s it for today.\n");
} else {
/* Prints a user input */
printf("%c",a);
}
}
return 1;
}
这是我的程序:
#include< stdio.h>
int main(){
char a;
while((a = getchar())!=''\\ \\ n''){
printf("%c",a);
}
printf(" \\\
That'这是今天的。\ n");
返回1;
}
正确??????
It''s my program:
#include <stdio.h>
int main (){
char a;
while ((a = getchar()) != ''\n'') {
printf("%c",a);
}
printf("\nThat''s it for today.\n");
return 1;
}
right??????
6月1日,10:25,Samuel.Codd ... @ gmail.com写道:
On 1 Jun, 10:25, Samuel.Codd...@gmail.com wrote:
我是新手程序员。我不能得到以下代码的工作。
Hi,
I''m a newbie programmer. I can''t get work the following code.
我不能上班并没有告诉我很多关于你的问题。
一般来说如果你有问题需要帮助,那么
告诉你很有帮助我们
*您期望发生什么?
*发生了什么?
您的代码适用于我,并且行为正如我所期待的那样,但那不是你想要的,所以你可能需要告诉我们
你的预期。
>
我会给你一个提示 - 标准输入可能是行缓冲...
"I can''t get work" doesn''t tell me much about your problem.
Generally if there''s a problem you need help with, it''s helpful to
tell us
* What did you expect to happen?
* What did happen?
Your code works for me, and behaves exactly as I expect, but that''s
not necessarily what you expected, so you probably need to tell us
what you expected.
I''ll give you a hint - standard input is probably line-buffered...
>
/ *
目标:
使用循环打印出所有输入字符,直到找到换行符
。 br />
* /
#include< stdio.h>
int main(){
/ *声明变量,分配初始值x。 * /
char a =''x'';
while(a!=''\ n''){
/ *获取用户输入* /
a = getchar();
if(a ==''\ n ''){
/ *如果输入是一个新行,请告诉用户* /
printf(\ n它今天就是它' 。$ \\ n");
} else {
/ *打印用户输入* /
printf(" %c,a);
}
}
返回1;
}
>
/*
Objective:
Use a loop to print out all of the input characters until a newline is
found.
*/
#include <stdio.h>
int main (){
/* Declare variable, Assign a initial value x. */
char a = ''x'';
while (a != ''\n'') {
/* Get a user input */
a = getchar();
if (a == ''\n''){
/* If the input is a new line, say good bye to user */
printf("\nThat''s it for today.\n");
} else {
/* Prints a user input */
printf("%c",a);
}
}
return 1;
}
Sa ************ @ gmail.com 写道:
小笔记(因为马克已经开始了专业其中):
Sa************@gmail.com wrote:
Minor notes (since Mark has started the major ones):
#include< stdio.h>
int main(){
/ *声明变量,分配初始值呃x。 * /
#include <stdio.h>
int main (){
/* Declare variable, Assign a initial value x. */
毫无意义的评论。
Pointless comment.
char a =''x'';
while(a!=''\ n''){
/ *获取用户输入* /
char a = ''x'';
while (a != ''\n'') {
/* Get a user input */
同上。
Ditto.
a = getchar();
a = getchar();
`getchar`返回`int`。所以`a`应该是`int`。 (这是因为
'getchar`的结果必须允许每个字符/和/
EOF。)
(fx:snip)
`getchar` returns `int`. So `a` should be `int`. (This is because
the result of `getchar` has to allow for every character /and/
EOF.)
(fx:snip)
返回1;
return 1;
不可携带。 0,EXIT_SUCCESS,EXIT_FAILURE(来自stdlib的那些)
是便携式的。
Not portable. 0, EXIT_SUCCESS, EXIT_FAILURE (those from stdlib)
are portable.
}
}
-
- 在严格监督下在实验室出生 - , - Magenta,/ Genetesis /
Hewlett-Packard Limited Cain Road,Bracknell,注册号:
注册办事处:Berks RG12 1HN 690597英格兰
--
"- born in the lab under strict supervision -", - Magenta, /Genetesis/
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
这篇关于循环和getchar问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!