By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,907 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,907 IT Pros & Developers. It's quick & easy.

Loop and getchar problems

P: n/a
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;
}

Jun 1 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Tak
On 6月1日, 下午5时25分, Samuel.Codd...@gmail.com wrote:
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;

}
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??????

Jun 1 '07 #2

P: n/a
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...
>
/*
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;

}

Jun 1 '07 #3

P: n/a
Sa************@gmail.com wrote:

Minor notes (since Mark has started the major ones):
#include <stdio.h>

int main (){
/* Declare variable, Assign a initial value x. */
Pointless comment.
char a = 'x';

while (a != '\n') {
/* Get a user input */
Ditto.
a = getchar();
`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)
return 1;
Not portable. 0, EXIT_SUCCESS, EXIT_FAILURE (those from stdlib)
are portable.
}
--
"- born in the lab under strict supervision -", - Magenta, /Genetesis/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 1 '07 #4

P: n/a
Hi, Tak,

Your code is much clearner. But the result on my end is the same.

I wanna make the program so that the program prints all the inputs
until a user input a new like (generally by pressing return).

So, it should open for a new input.
That string "That's it for today" should be displayed only when a user
type a newline.

Jun 1 '07 #5

P: n/a
Tak said:
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??????
Wrong. Well, you're right that it's your program, but you're wrong that
it's right. It's wrong.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 1 '07 #6

P: n/a
On 1 Jun, 10:53, mark_blue...@pobox.com wrote:
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.
[Snip]

I'd better qualify my comments by saying that I didn't bother with the
nitpicking about whether you should declare main as "int main(void)"
or that a should be an int.

Jun 1 '07 #7

P: n/a
ma**********@pobox.com said:
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.
Actually, I think it tells you all you need to know.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 1 '07 #8

P: n/a
Sa************@gmail.com said:
Hi,

I'm a newbie programmer. I can't get work the following code.
Here's your program again, properly indented for readability:

#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;
}

Problems:

1) a has a lousy name!
2) a has the wrong type - it should be int.

Whilst I wouldn't want to suggest that your program is otherwise devoid
of problems, it does appear to do what is asked of it, except in cases
where it hits the end-of-file, where it does get a bit messy, but you
presumably aren't worried about that.

For the record, here's how I'd have written it:

#include <stdio.h>

int main(void)
{
int ch = 0;
while((ch = getchar()) != EOF && ch != '\n')
{
putchar(ch);
}

printf("\nThat's it for today.\n");

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 1 '07 #9

P: n/a
On 1 Jun, 11:27, Richard Heathfield <r...@see.sig.invalidwrote:
mark_blue...@pobox.com said:
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.

Actually, I think it tells you all you need to know.
Meow! Harsh but essentially fair, I guess.

However, he did post a complete, compilable, relatively cleanly
formatted program - give him some points for that...

Jun 1 '07 #10

P: n/a
On 1 Jun, 11:00, Samuel.Codd...@gmail.com wrote:
Hi, Tak,

Your code is much clearner. But the result on my end is the same.

I wanna make the program so that the program prints all the inputs
until a user input a new like (generally by pressing return).
That's what I thought you wanted - why didn't you say so in your
original posting.

What you are saying, I think, is that you want to read and echo the
characters typed as they are typed, without waiting for a line-full of
input. Am I right? I'll assume so.

Do you know, the C language specification doesn't give you a way that
you can guarantee to do this. Input will frequently be buffered, in
some way. You might spend some time with the FAQ at c-faq.com,
especially http://c-faq.com/stdio/index.html questions 12.1 and 12.5,
to understand about this and other issues with your code.
So, it should open for a new input.

That string "That's it for today" should be displayed only when a user
type a newline.

Jun 1 '07 #11

P: n/a
Thanks, guys. Sorry for my ill-mannered post.

I started programming a few days ago. I will read more about what you
wrote.

I need to spend more time before understanding your posts.

Regards,

Jun 1 '07 #12

P: n/a
Sa************@gmail.com wrote:
>
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;
}
Here's a slightly different program, which should work. Note the
proper indentation etc.

#include <stdio.h>

int main (void) {
int a;

while ((EOF != (a = getchar()) && (a != '\n'))
printf("%c",a);
printf("\nThat's it for today.\n");
return 0
} /* untested */

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 1 '07 #13

P: n/a
ma**********@pobox.com wrote:
>
.... snip ...
>
I'd better qualify my comments by saying that I didn't bother
with the nitpicking about whether you should declare main as
"int main(void)" or that a should be an int.
None of that is a nit.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 1 '07 #14

P: n/a
On Fri, 01 Jun 2007 09:25:00 -0000, Sa************@gmail.com wrote:
>Hi,

I'm a newbie programmer. I can't get work the following code.
snip code

It helps a whole bunch if you tell us what is happening that is
different than what you expect.
Remove del for email
Jun 2 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.