Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Loop and getchar problems

Reply
Thread Tools

Loop and getchar problems

 
 
Samuel.Coddler@gmail.com
Guest
Posts: n/a
 
      06-01-2007
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;
}

 
Reply With Quote
 
 
 
 
Tak
Guest
Posts: n/a
 
      06-01-2007
On 6月1日, 下午5时25分, (E-Mail Removed) 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??????

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      06-01-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      06-01-2007
On 1 Jun, 10:25, (E-Mail Removed) 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;
>
> }



 
Reply With Quote
 
Samuel.Coddler@gmail.com
Guest
Posts: n/a
 
      06-01-2007
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.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-01-2007
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.
 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      06-01-2007
On 1 Jun, 10:53, (E-Mail Removed) wrote:
> On 1 Jun, 10:25, (E-Mail Removed) 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.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-01-2007
(E-Mail Removed) said:

> On 1 Jun, 10:25, (E-Mail Removed) 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.
 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      06-01-2007
On 1 Jun, 11:27, Richard Heathfield <(E-Mail Removed)> wrote:
> (E-Mail Removed) said:
>
> > On 1 Jun, 10:25, (E-Mail Removed) 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...

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-01-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
user input, getchar, and buffer - For C beginners and those withteaching skills... bpascal123@googlemail.com C Programming 42 08-31-2009 05:15 PM
Learning C - Scanf or Getch, or Getchar not working correctly after first loop. tesh.uk@googlemail.com C Programming 26 03-04-2007 02:27 AM
GetChar loop Daz C++ 4 06-26-2006 08:06 PM
Re: EOF and getchar/fgetc Martin Dickopp C Programming 0 02-14-2004 03:17 PM



Advertisments