Aleramo said:
I don't understand the reason cos this cycle:
do {
printf ("\nCome devono essere posizionati gli elementi?\n") ;
printf ("* premere E, nel caso di posizionamento sul piano
E\n") ;
printf ("(ricezione onde orizzontali);\n") ;
printf ("* premere H, nel caso di posizionamento sul piano
H\n") ;
printf ("(ricezione onde verticali).\n") ;
scanf ("%c", &scelta_piano) ;
} while ((scelta_piano != 'H') && (scelta_piano != 'h') &&
(scelta_piano != 'E') && (scelta_piano != 'e')) ;
write its content two time. So the output is:
I suggest you think about it differently. I believe you want to
present a prompt, and get one of a subset of possible response
characters. So isolate this operation in a function.
int promptandgetresponse(const char *prompt,
const char *acceptable);
Note the return of an int, so that an EOF can be detected. The
assumption is being made that stdout is used for prompting, and
stdin for input. You could add parameters to control this, if
desired. So the first step is to present the prompt:
do {
if (prompt) {
printf("%s", prompt); fflush(stdout);
}
/* Now get a reply */
} while ( /* reply is not satisfactory */ );
Note the fflush, which allows the prompt to not end with a \n. The
test on prompt allows you to avoid prompting entirely, by supplying
NULL.
Looking at this you may see a possible problem. How is the user to
know why his response is not acceptable? So you may want to modify
the system to give feedback on this later. This is left as an
exercise.
Now, how do we get a reply? There is no need to complicate things
with the highly unruly scanf if we keep a couple of useful
auxiliary routines around:
int skipblanks(void) {
int ch;
while (' ' == (ch = getchar())) continue;
return ch;
}
which will gobble up leading blanks. We can also gobble up all
unused line portions with:
+
int flushln(void) {
int ch;
while (('\n' != (ch = getchar())) && (EOF != ch)) continue;
return ch;
}
Notice that both routines will exit, and return EOF, if an EOF is
encountered. These routines are so useful that you should always
have them available for interactive use. They are better
generalized to access arbitrary files, supplied as a parameter.
Now lets embellish the promptandgetresponse routine to use these.
int promptandgetresponse(const char *prompt,
const char *acceptable)
{
int ch;
do {
if (prompt) {
printf("%s", prompt); fflush(stdout);
}
/* Now get a reply */
ch = skipblanks(); /* get first non-blank char */
if ('\n' != ch) flushln(); /* set up to try again */
} while ( /* reply is not satisfactory */ );
return ch;
}
This hasn't resolved "reply is not satisfactory" yet, and has some
other minor difficulties. You should try the condition:
strchr(acceptable, ch)
to replace that comment. This will require #include <strings.h>.
Things to think about: What if the user enters only an empty
line? You could make this select a default, such as the first
character in acceptable. What if an EOF is encountered while
skipping blanks? while executing flushln? You can resolve most of
these by insisting that responses be properly terminated with '\n',
i.e. an <enter> on most machines. Otherwise you will return an EOF
and not try to preserve data from an invalid entry. After all,
once EOF has been signalled you can't do anything more.
Once you get this running cleanly, you will have solved many of
your interactive problems for the future. Note that the routine(s)
need not be long and complex, rather they should be short and you
should be able to see flaws easily on inspection.
BTW, notice that the code I am proposing is not confused by
language difficulties. What the prompt is, and its language, have
no effect whatsoever. Similarly the translation of single char.
responses.
The above code is untested. So beware.
--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html