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("\nPleas e type a character: ");
scanf("%c",&a);
printf("\nYou typed: %c",a);
}
} 35 1972
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("\nPleas e 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("\nPleas e 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("\nPleas e 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.
On Jul 13, 2:07 pm, hdsalbki <hdsal...@gmail .comwrote:
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.
vipps...@gmail. com wrote:
On Jul 13, 2:07 pm, hdsalbki <hdsal...@gmail .comwrote:
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?
okay I followed everyone ideas and wrote another version of the
program. Now there is no extra prompt but the code misses every
alternate character I type. Can anyone help? Thanks a lot, never
imagined such a simple thing will be so difficult!
#include <stdio.h>
int main()
{
char a;
while(1)
{
printf("\nPleas e enter a character: ");
fflush(stdout);
if(scanf("%c%*[^\n]%*c",&a) < 1)
{
break;
}
printf("You typed: %c\n",a);
fflush(stdout);
}
return(0);
}
o/p is:
dodo@sapphire:~/src/c/ws$ ./echo_char1
Please enter a character: a
You typed: a
Please enter a character: b
You typed:
Please enter a character: c
You typed: c
Please enter a character: d
You typed:
Please enter a character:
On Jul 13, 2:58 pm, hdsalbki <hdsal...@gmail .comwrote:
okay I followed everyone ideas and wrote another version of the
program. Now there is no extra prompt but the code misses every
alternate character I type. Can anyone help? Thanks a lot, never
imagined such a simple thing will be so difficult!
#include <stdio.h>
int main()
I told you int main(void), not int main().
{
char a;
while(1)
{
printf("\nPleas e enter a character: ");
fflush(stdout);
if(scanf("%c%*[^\n]%*c",&a) < 1)
{
break;
}
printf("You typed: %c\n",a);
fflush(stdout);
That fflush() is not necessary, as you write a newline to stdout. That
will flush the stream, since it's line buffered. (_IOLBF in setvbuf)
}
return(0);
return is not a function, it's a keyword. the parenthesis are not
needed, just like sizeof. (do not confuse it with sizeof (cast), it's
the cast that requires parenthesis, not sizeof)
>
}
o/p is:
dodo@sapphire:~/src/c/ws$ ./echo_char1
Please enter a character: a
You typed: a
Please enter a character: b
You typed:
Please enter a character: c
You typed: c
Please enter a character: d
You typed:
Please enter a character:
Yeah, it seems that it does not actually read the newline with %*c.
I can't tell you why this happends, perhaps someone else can explain.
Remove the %*c and add a getchar(), and it should work:
if(scanf("%c%^[\n]", &a) < 1) break;
(void)getchar() ;
Note that the cast to (void) is not necessary; I only added it to make
more clear that you don't need to check the return value of getchar.
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("\nPleas e 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;
}
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("\nPleas e 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("\nPleas e 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;
}
}
}
return 0;
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Durgesh Sharma |
last post by:
Hi All,
Pleas help me .I am a starter as far as C Language is concerned .
How can i Right Trim all the white spaces of a very long (2000 chars)
Charecter string ( from the Right Side ) ? or how can i make a fast
Right Trim Function in c,using Binary search kind of fast algorithm ?
Offcourse...I can use the classical approach too.
like :
Start from the right most charecter of the string to the left of the
|
by: QT |
last post by:
Dear sirs,
I try to put limit for text field to stop charecter entery after 2
charecter. But I have no success.
Any idea how can I do?
Best Regards
|
by: ayan4u |
last post by:
well i need to deal with white spaces in charecter arrays...
with static arrays its fine..
char ss;
cin.getline(ss, sizeof ss);
.... //deals with white spaces
|
by: situ |
last post by:
Hello all,
i have a column in a table which stores tag lines which are usually
less than 500 charecter. when we display the data through the
browser some character display as "boxes" or junks. i predict it may
be the newline or carriage return character.
is there any function so that i can ward of these characters
my database is under utf 8
and my db2level is
|
by: ayan4u |
last post by:
ok i have two problems...
firstly in strict C enviornment is it possible to have a true dynamic charecter array with no predefined length...i mean to say...
suppose..
char *array =NULL;
/*
actually i dnt know what the exact size of my string is so i aquire sace for 1 charecter from heap
/*
| |
by: phanimadhav |
last post by:
Actually this is my tabledetails
Table Name:Organisation
Colums:Org_Id,org_Name,Org_Nationality
Organisation namess is:Wipro,satyam,CTS,CMC,Infotech,Intel,Syentel,CISCO
my requirement is if i enter the one charecter in the textbox.i want to display all releted Organisation names display within LISTBOX.That means firstcharacter of the Org_Name must be match with the with in the textbox character.
i am asking number of developers.Some...
|
by: mail2ravi123 |
last post by:
hi all,
plz provide me a function that is used to compare wild char and as well as to compare * charecter and \ charecter in the string
FYI
if we want to compare a string for * charecter then we give as \* and add a new slash to it for comparing
and for slash also \\ to compare single \
and for wild char just *
|
by: santoshramancha |
last post by:
Hi all,
what is a conversion charecter and why and where it is used?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |