Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » CGoto page 1, 2, 3, 4  Next

echo charecter program...

 
Jump to:  
 
hdsalbki
PostPosted: Sun Jul 13, 2008 10:10 am    Post subject: echo charecter program...
       
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);
}
}
 

 
Joachim Schmitz
PostPosted: Sun Jul 13, 2008 10:10 am    Post subject: Re: echo charecter program...
       
vippstar@gmail.com wrote:
Quote:
On Jul 13, 1:10 pm, hdsalbki <hdsal...@gmail.com> wrote:
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


Quote:
#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;


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


Quote:
}

return 0;
}

Bye, Jojo
 

 
Walter Roberson
PostPosted: Sun Jul 13, 2008 10:40 am    Post subject: Re: echo charecter program...
       
In article <6417fffa-c97c-4c9b-a60d-1cf3f980d005@m3g2000hsc.googlegroups.com>,
hdsalbki <hdsalbki@gmail.com> wrote:

Quote:
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: ");

You need to fflush(stdout); at this point.

Quote:
scanf("%c",&a);

You need to check the return value of scanf at this point, in
order to find out if there has been an end-of-file or I/O error.

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

Your code does not have an 'int' before the 'main', so your
code is not valid C99 (C99 no longer supports the implicit int
type specifier.) But your code does not return a value to the
operating system; in C89 if you do not return a value to the
operating system when you exit the routine, the behaviour
is implementation defined, which can include crashing the computer.
This particular program is saved only because you never return
to the operating system except by terminating the program
through some external mechanism.


The reason you are getting an extra prompt is that when you
type the character of input, after typing the character you
are pressing return -- and that newline is still there in the
buffer. You only read the character itself with the first
scanf() call; the second time through the loop, there is already
input waiting, namely the newline character. A similar thing would
happen if you were to type several characters before pressing
return: your code would output the prompt for each one of them.

--
"He wove a great web of knowledge, linking everything together,
and sat modestly at a switchboard at the center, eager to help."
-- Walter Kerr
 

 
Guest
PostPosted: Sun Jul 13, 2008 10:40 am    Post subject: Re: echo charecter program...
       
On Jul 13, 1:10 pm, hdsalbki <hdsal...@gmail.com> wrote:
Quote:
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.
Quote:
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;
}
 

 
hdsalbki
PostPosted: Sun Jul 13, 2008 11:03 am    Post subject: Re: echo charecter program...
       
Walter Roberson wrote:
Quote:
In article <6417fffa-c97c-4c9b-a60d-1cf3f980d005@m3g2000hsc.googlegroups.com>,
hdsalbki <hdsalbki@gmail.com> wrote:

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: ");

You need to fflush(stdout); at this point.

scanf("%c",&a);

You need to check the return value of scanf at this point, in
order to find out if there has been an end-of-file or I/O error.

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

Your code does not have an 'int' before the 'main', so your
code is not valid C99 (C99 no longer supports the implicit int
type specifier.) But your code does not return a value to the
operating system; in C89 if you do not return a value to the
operating system when you exit the routine, the behaviour
is implementation defined, which can include crashing the computer.
This particular program is saved only because you never return
to the operating system except by terminating the program
through some external mechanism.


The reason you are getting an extra prompt is that when you
type the character of input, after typing the character you
are pressing return -- and that newline is still there in the
buffer. You only read the character itself with the first
scanf() call; the second time through the loop, there is already
input waiting, namely the newline character. A similar thing would
happen if you were to type several characters before pressing
return: your code would output the prompt for each one of them.

--
"He wove a great web of knowledge, linking everything together,
and sat modestly at a switchboard at the center, eager to help."
-- Walter Kerr

Thank you for our comment Walter. Actually in my book the \n is given
at the end of the prompt...I changed it to the front because otherwise
the extra prompt was getting printed on the same line as the character
echoed. I don't know what fflush(stdout) (just in the second chapter)
but I will include it. wrt to scanf how can I get an end-of-file or I/
O error? And main() returns nothing...that's why I didn't return
anything... is that wrong? My book has this everywhere (in someplaces
it is main(void) ). and I kill the program with ctrl-c because I
wanted to echo all that the user types. I get it now. So I think to
fix this I can have another scanf() call before my main one and just
read the '\n' to a dummy variable? Is this okay?

Once again thanks for your input.
 

 
hdsalbki
PostPosted: Sun Jul 13, 2008 11:07 am    Post subject: Re: echo charecter program...
       
vipps...@gmail.com wrote:
Quote:
On Jul 13, 1:10 pm, hdsalbki <hdsal...@gmail.com> wrote:
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.
 

 
Guest
PostPosted: Sun Jul 13, 2008 11:19 am    Post subject: Re: echo charecter program...
       
On Jul 13, 2:07 pm, hdsalbki <hdsal...@gmail.com> wrote:
Quote:
vipps...@gmail.com wrote:
snip
if(scanf("%c%*[^\n]%*c") < 1) break;
snip
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.

See Mr Schmitz correction on my post to get the correct source code.
%* means match conversion but do not "write" to object.
So %*d would read an int but just discard it.

[^\n] means read everything until a newline or eof. push newline back.
%*c means read a byte and discard it.
%*[^\n]%*c reads and discards everything until a newline is met, which
it also discards.
 

 
hdsalbki
PostPosted: Sun Jul 13, 2008 11:42 am    Post subject: Re: echo charecter program...
       
vipps...@gmail.com wrote:
Quote:
On Jul 13, 2:07 pm, hdsalbki <hdsal...@gmail.com> wrote:
vipps...@gmail.com wrote:
snip
if(scanf("%c%*[^\n]%*c") < 1) break;
snip
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.

See Mr Schmitz correction on my post to get the correct source code.
%* means match conversion but do not "write" to object.
So %*d would read an int but just discard it.
[^\n] means read everything until a newline or eof. push newline back.
%*c means read a byte and discard it.
%*[^\n]%*c reads and discards everything until a newline is met, which
it also discards.

Please correct me if I'm getting this wrong: in your call first
scanf() reads a character and loads it to a then it reads and throws
away everthing to the line end but leaves alone the \n and then reads
the \n and also throws it away. whew! I wish my book explained it like
this. thanks a lot! your code works perfectly. Can you also tell me
why I'm not able to read the function keys and arrow keys? Any way I
can do that?
 

 
Joachim Schmitz
PostPosted: Sun Jul 13, 2008 11:44 am    Post subject: Re: echo charecter program...
       
hdsalbki wrote:
Quote:
hdsalbki wrote:
Thanks to everyone I was able to do this. apart from what vippstar
has sugested I also came with the below code to read the rest of
line in stdin. Is this okay or am I doing something wrong. Thanks
for any comments.

#include <stdio.h

int main()
{
char a;
while(1)
{
printf("\nPlease enter a character: ");
fflush(stdout);
scanf("%c",&a);
printf("You entered: %c\n",a);
fflush(stdout);
while(1)
{
scanf("%c",&a);
if(a=='\r' || a=='\n')
{
break;
}
}
}
return 0;
}

Sorry some errors were there. Corrected code follows:

#include <stdio.h

int main(void)
{
char a;
while(1)
{
printf("\nPlease enter a character: ");
fflush(stdout);
scanf("%c",&a);
printf("You entered: %c\n",a);
while(1)
{
scanf("%c",&a);
if(a=='\r' || a=='\n')
{
break;
}
Why not

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

Quote:
}
}
return 0;
}

Bye, Jojo
 

 
Joachim Schmitz
PostPosted: Sun Jul 13, 2008 11:50 am    Post subject: Re: echo charecter program...
       
Joachim Schmitz wrote:
Quote:
hdsalbki wrote:
hdsalbki wrote:
Thanks to everyone I was able to do this. apart from what vippstar
has sugested I also came with the below code to read the rest of
line in stdin. Is this okay or am I doing something wrong. Thanks
for any comments.

#include <stdio.h

int main()
{
char a;
while(1)
{
printf("\nPlease enter a character: ");
fflush(stdout);
scanf("%c",&a);
printf("You entered: %c\n",a);
fflush(stdout);
while(1)
{
scanf("%c",&a);
if(a=='\r' || a=='\n')
{
break;
}
}
}
return 0;
}

Sorry some errors were there. Corrected code follows:

#include <stdio.h

int main(void)
{
char a;
while(1)
{
printf("\nPlease enter a character: ");
fflush(stdout);
scanf("%c",&a);
printf("You entered: %c\n",a);
while(1)
{
scanf("%c",&a);
if(a=='\r' || a=='\n')
{
break;
}
Why not
if(scanf("%c%*[^\r\n]%*c", &a) < 1) break;
Oops. Should be in the outer while, the inner while deleted entirely.


Quote:

}
}
return 0;
}

Bye, Jojo
 

Page 1 of 4 .:. Goto page 1, 2, 3, 4  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

current local time in Stwarzanie świata - Miłosz Czesław Ich Szatan - Leśmian Bolesław spadki freeskiing