How can I clear a character buffer?
Author Message
How can I clear a character buffer?

This is a question I asked myself when I was writing a school program.
I came up with a way around it, and now I have a larger problem. I am
looking for a program that could be compiled for any platform, so I am
using ANSI standard.
Could someone please look at the code(below) and tell me what is wrong?

/************************************************/
/* (C) 28th May 1998 David
Illsley                                         */
/* Higher Computing Project                     */
/* Version 2.3                                  */
/* A Program which converts:                    */
/*                   cm to inches (Distance)    */
/*                   kg to pounds (Weight)      */
/*                   C to F (Temperature)       */
/*                   Litres to Gallons (Volume) */
/************************************************/

/*Sets the names of the standard librarys that we need to use*/
#include <stdio.h>

/*initialise all of the global variables*/
float toconvert = 1;
float converted = 1;

/*Function Definitions*/
void choose(void);           /* Processes Main Menu choice */
void again(void);            /* Asks what to do next */
void cmin(void);             /* Centimetres to Inches */
void incm(void);             /* Inches to Centimetres */
void kgpounds(void);         /* Kilograms to Pounds */
void poundskg(void);         /* Pounds to Kilograms */
void centfar(void);          /* Centigrade to Farenheight */
void farcent(void);          /* Farenheight to Centigrade */
void litgal(void);           /* Litres to Gallons */
void gallit(void);           /* Gallons to Litres */

/* This Function is in all C Programs and calls all of the other
functions */
int main(void){

Quote:
}

printf("\n\n           Conversions"
"\n\n  1.    Centimetres to Inches"
"\n\n  2.    Inches to Centimetres"
"\n\n  3.    Kilograms to Pounds"
"\n\n  4.    Pounds to Kilograms"
"\n\n  7.    Litres to Gallons"
"\n\n  8.    Gallons to Litres"
choose();

Quote:
}

void choose(void){
{
case 49: cmin();
break;
case 50: incm();
break;
case 51: kgpounds();
break;
case 52: poundskg();
break;
case 53: centfar();
break;
case 54: farcent();
break;
case 55: litgal();
break;
case 56: gallit();
break;
default :{
printf("\nYou entered a wrong number. Please try again");

Quote:
}
}
}

void again(void){
printf("\n\nDo you wish to..."
"\n1. Do the same conversion again"
"\n2. Do a different comversion from the main menu"
"\n3. Exit the program\n"
{
case 49:{
choose();
Quote:
}

break;
case 50:{
break;
Quote:
}

case 51:
break;
default:{
printf("You entered a wrong number. Please try again");
again();

Quote:
}
}
}

void cmin(void){
printf("Centimetres to Inches Conversion");
printf("\n\n Please enter the number of Centimetres: ");
scanf("%f", &toconvert );
converted = (toconvert * 0.4);
printf("\n\nThat is %f Inches.", converted );
again();

Quote:
}

void incm(void){
printf("Inches to Centimetres Conversion");
printf("\n\n Please enter the number of Inches: ");
scanf("%f", &toconvert );
converted = (toconvert * 2.5);
printf("\n\nThat is %f Centimetres.", converted );
again();

Quote:
}

void kgpounds(void){
printf("Kilograms to Pounds Conversion");
printf("\n\n Please enter the number of Kilograms: ");
scanf("%f", &toconvert );
converted = (toconvert * 2.2);
printf("\n\nThat is %f Pounds.", converted );
again();

Quote:
}

void poundskg(void){
printf("Pounds to Kilograms Conversion");
printf("\n\n Please enter the number of Pounds: ");
scanf("%f", &toconvert );
converted = (toconvert * 0.454);
printf("\n\nThat is %f Kilograms.", converted );
again();

Quote:
}

void centfar(void){
scanf("%f", &toconvert );
converted = ((9 * toconvert) + 160) / 5;
printf("\n\nThat is %f degrees Farenheight.", converted );
again();

Quote:
}

void farcent(void){
printf("\n\n Please enter the number of degrees Farenheight: ");
scanf("%f", &toconvert );
converted = ((toconvert - 32) * 5) / 9;
printf("\n\nThat is %f degrees Centigrade.", converted );
again();

Quote:
}

void litgal(void){
printf("Litres to Gallons Conversion");
printf("\n\n Please enter the number of Litres: ");
scanf("%f", &toconvert );
converted = (toconvert * 0.22);
printf("\n\nThat is %f Gallons.", converted );
again();

Quote:
}

void gallit(void){
printf("Gallons to Litres Conversion");
printf("\n\n Please enter the number of Gallons: ");
scanf("%f", &toconvert );
converted = (toconvert * 4.6);
printf("\n\nThat is %f Litres.", converted );
again();
Quote:
}

--

http://www.{*filter*}junkie.com/oxymoron
___________________________________________________
'If there were no Macintosh, it would be necessary to invent one.'
Michael E. Cohen

Mon, 13 Nov 2000 03:00:00 GMT
How can I clear a character buffer?

Quote:

> This is a question I asked myself when I was writing a school program.
> I came up with a way around it, and now I have a larger problem. I am
> looking for a program that could be compiled for any platform, so I am
> using ANSI standard.

You're almost there - I can only see a few portability problems.

Quote:
> Could someone please look at the code(below) and tell me what is wrong?
> /* Higher Computing Project                     */

Cor.  I had to use RM-BASIC, I did - structured programming with line
numbers.  I wish I'd thought of that one.

Quote:
> /*initialise all of the global variables*/
> float toconvert = 1;
> float converted = 1;

If you've learned about parameter passing, then all these variables
could be placed within functions.  This is a Good Thing.  It makes it
clearer where things are being changed (useful if you see something
has a wrong value and want to see how it got there), it shows the flow
of data around your program and it makes it easier to discover exactly
what a variable is supposed to represent.  If you haven't learned
about function arguments and return values, ignore this.

Quote:
> /* This Function is in all C Programs and calls all of the other
> functions */
> int main(void){
> }

You should return something from main() - 0 would indicate success.
If you include stdlib.h you could also use EXIT_FAILURE or
EXIT_SUCCESS which have the obvious meanings.

Quote:

It's somewhat nitpicky, but you should check the return value of
scanf() - it will return the number of values it managed to store
successfuly, which might be 0 if there wasn't a character waiting (if,
for example, there was an input error).

Quote:
> void choose(void){
> {
> case 49: cmin();

Character constants are really small integer values (they have type
int) and so you can say

case '1': cmin();

which is much more legible and means your program doesn't rely on a
particular character set.

Quote:
> default :{
>   printf("\nYou entered a wrong number. Please try again");

If you are seeing the menu printed twice it is because your
implementation is buffering input by the line and the user must type
return for the character or number to become availible.  When this
happens what the program sees waiting to be read is the input you are
interested in followed by a '\n' character.

You read the input, and the '\n' is left to be read.  If the next
thing the program tries to read is a character, it will be given '\n'.
To get rid of this behaviour, after each input you should read in
characters until you find a '\n'.

Quote:
> }

You don't need the braces round these two statements, but they don't
hurt either.  break is just a statement like the two function calls
above or

i = i + 1;

is which tells the compiler that the program should exit the loop or
case statement it is in and carry on afterwards.

One final point about the structure of the program.  You have written
it in a "do this, then that" fashion - at the end of each function, it
calls another function which does whatever should be done next.  This
will work, but it is not a natural or clear way to solve the problem.
What your program does is approximately this

do
if valid input
perform conversion
do
ask user if they want to quit
get and check response
until valid input
else
display error message
until the user wants to quit

but as it stands, your program doesn't really show clearly what's
going on - we know what the next step will be in any given case, but
we normally can't easily see further ahead than that.  Until we see
the again() function it's not at all apparent that the program is
going to loop.  In a program this size, it is possible to see what is
going on but for a larger program the problems would multiply.

Your program would benefit from being restructured with explicit loops
to show when you are looping.  If you're feeling adventurous you might
like to consider what you could accomplish with arrays.

--

http://www.tardis.ed.ac.uk/~broonie/
EUFS        http://www.ed.ac.uk/~filmsoc/

Mon, 13 Nov 2000 03:00:00 GMT
How can I clear a character buffer?

Quote:

> Could someone please look at the code(below) and tell me what is wrong?

Your scanf("%c") has problems with the newlines that pressing Enter will

Try changing the type of againmenu and mainmenu to int and the scanf()
call to scanf("%d", ...);

Now you will be scanning integers so the switch() should also be changed
to reflect this (you will not have to worry about the ASCII codes of

Hope this helps without ruining the fun of learning.

--
Regards, Anders                <URL:http://www.kampsax.dtu.dk/~and/>

Lewis's Law of Travel:
The first piece of luggage out of the chute doesn't belong to anyone, ever.

Tue, 14 Nov 2000 03:00:00 GMT
How can I clear a character buffer?

Quote:

> Try using the line

> while(getchar()!='\n');

...until it gets stuck on EOF, in which case you might want to try to
check for that, too.

--

I believe we can change anything.
I believe in my dream.
- Joe Satriani

Tue, 14 Nov 2000 03:00:00 GMT
How can I clear a character buffer?

Try using the line

while(getchar()!='\n');

Al

Wed, 15 Nov 2000 03:00:00 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages