Problem with DO-WHILE Loop

R

Redduck

Hello everyone. I am frustrated, I have written the simple program
below for a class and I am having problems with the DO-WHILE loop. On
the first run through the loop, everything works well, the menu is
displayed, the input is registered and the loop runs. On the second
(and following) runs the menu is printed twice. I am sure there is
something very basic that I am missing, but I cannot see it. Can
anyone help?

Thanks in advance.

Ken

****Here is the code****

/* currency.c - Version 1.2
This program receives a selection from the
user to determine the type of currency they
would like the exchange rate for. It then returns
the value of the amount of US currency they entered
in the currency type that they selected.*/

#include <stdio.h>

int main()
{
char nation; //Declare variable for the users choice of currencies

float us_dollar;
float canada_conv;
float euro_conv;
float krona_conv;
float yen_conv;
float pound_conv; //Declare variables of Float type

int cont = 0;

canada_conv = 1.2997; //Set exchange rates for all variables
euro_conv = 1.2056;
krona_conv = 7.5625;
yen_conv = 0.009057;
pound_conv = 0.7694;
us_dollar = 1.00; //set initial dollar amount

printf("Currency Conversion\n\n"); //Title of program

do {

cont = 0;

//Prompt user for currency
printf("Which currency would you like to convert?\n\n");
printf("Press 'C' for Canadian Dollars.\n");
printf("Press 'E' for Euros.\n");
printf("Press 'K' for Swedish Krona.\n");
printf("Press 'Y' for Japanese Yen.\n");
printf("Press 'P' for British Pounds.\n\n");
printf("Press 'Q' to quit.\n\n");

nation=getchar(); //Retrieves users choice and assigns "nation"
variable

switch (nation) //Selection process to give amount based on user
selection
{
case 'C':
case 'c':
printf("\nPlease enter the US dollar amount you want to
convert.\n\n");
scanf("%f", &us_dollar); //Gather the dollar amount they wish to
convert
printf("\n$%1.2f US is worth %1.2f Canadian Dollars.\n\n",
us_dollar, us_dollar * canada_conv);
cont = 0;
break;

case 'E':
case 'e':
printf("\nPlease enter the US dollar amount you want to
convert.\n\n");
scanf("%f", &us_dollar); //Gather the dollar amount they wish to
convert
printf("\n$%1.2f US is worth %1.2f Euros.\n", us_dollar, us_dollar
* euro_conv);
cont = 0;
break;

case 'K':
case 'k':
printf("\nPlease enter the US dollar amount you want to
convert.\n\n");
scanf("%f", &us_dollar); //Gather the dollar amount they wish to
convert
printf("\n$%1.2f US is worth %1.2f Swedish Krona.\n", us_dollar,
us_dollar * krona_conv);
cont = 0;
break;

case 'Y':
case 'y':
printf("\nPlease enter the US dollar amount you want to
convert.\n\n");
scanf("%f", &us_dollar); //Gather the dollar amount they wish to
convert
printf("\n$%1.2f US is worth %1.2f Japanese Yen.\n", us_dollar,
us_dollar * yen_conv);
cont = 0;
break;

case 'P':
case 'p':
printf("\nPlease enter the US dollar amount you want to
convert.\n\n");
scanf("%f", &us_dollar); //Gather the dollar amount they wish to
convert
printf("\n$%1.2f US is worth %1.2f British Pounds.\n", us_dollar,
us_dollar * pound_conv);
cont = 0;
break;

case 'Q':
case 'q':
cont = 1;
printf("Thanks for using the Currency Conversion program.\n\n");
break;

default: //This is the error check. If the enter an incorrect
choice, they get shut down
break;
}

} while (cont!=1);


//Give user time to see results and end program
printf("\n\nPress any key and Enter to exit the program.\n");
scanf("%d");
return 0;
}
 
P

Pete Gray

Hello Ken,

You probably have "leftover characters" in the input buffer following the
call to scanf() - I've seen this before with progs that mix scanf() and
getchar(). Easiest solution, for this program, is not to mix them. Flushing
the buffer would probably work too, but I've not tried it.

Regards,
-Pete.
 
A

Al Bowers

Redduck said:
Hello everyone. I am frustrated, I have written the simple program
below for a class and I am having problems with the DO-WHILE loop. On
the first run through the loop, everything works well, the menu is
displayed, the input is registered and the loop runs. On the second
(and following) runs the menu is printed twice. I am sure there is
something very basic that I am missing, but I cannot see it. Can
anyone help?

Using function getchar leaves a '\n' char in stdin. A quick
fix would use function fgets in place of getchar. See below.
****Here is the code****

/* currency.c - Version 1.2
This program receives a selection from the
user to determine the type of currency they
would like the exchange rate for. It then returns
the value of the amount of US currency they entered
in the currency type that they selected.*/

#include <stdio.h>

int main()
{
char nation; //Declare variable for the users choice of currencies

make nation a char array, ie.
char nation[8];
float us_dollar;
float canada_conv;
float euro_conv;
float krona_conv;
float yen_conv;
float pound_conv; //Declare variables of Float type

int cont = 0;

canada_conv = 1.2997; //Set exchange rates for all variables
euro_conv = 1.2056;
krona_conv = 7.5625;
yen_conv = 0.009057;
pound_conv = 0.7694;
us_dollar = 1.00; //set initial dollar amount

printf("Currency Conversion\n\n"); //Title of program

do {

cont = 0;

//Prompt user for currency
printf("Which currency would you like to convert?\n\n");
printf("Press 'C' for Canadian Dollars.\n");
printf("Press 'E' for Euros.\n");
printf("Press 'K' for Swedish Krona.\n");
printf("Press 'Y' for Japanese Yen.\n");
printf("Press 'P' for British Pounds.\n\n");
printf("Press 'Q' to quit.\n\n");

nation=getchar(); //Retrieves users choice and assigns "nation"
variable
use fgets in place of getchar.

fgets(nation, sizeof nation, stdin);
switch (nation) //Selection process to give amount based on user
selection
switch(nation[0]);

{

........... snip .............
 
C

CBFalconer

Redduck said:
Hello everyone. I am frustrated, I have written the simple program
below for a class and I am having problems with the DO-WHILE loop. On
the first run through the loop, everything works well, the menu is
displayed, the input is registered and the loop runs. On the second
(and following) runs the menu is printed twice. I am sure there is
something very basic that I am missing, but I cannot see it. Can
anyone help?

.... snip code ...

As usual the problem is the use of scanf for interactive input.
Think about what is left in the input stream after executing that
call. Also think about other methods of getting the users input
which allow you to be sure of the input stream condition.
 
R

ranjeet

The problem is of the clearence of the buffer:

Now when u enter the choice then after that u have to clear the buffer also.

As u know that *getchar* takes all the input into the buffer and then from
there it takes the value to the allocated mamory.

And the *getch* takes the each respective character and simultaneously stores
into the allocated memory. and once it over flows the memory sapsce a error
code is desplayed (this thing u have to handle manually)

So try to use the getch instead of getchar:

Or use the fflush function:

in this u will be handle the problem.

any way if u have the doubts then let me know.


Regards
Ranjeet
 
R

ranjeet

Here is your code corredtion it will work fine..... Enjoy :

Pete Gray said:
Hello Ken,

You probably have "leftover characters" in the input buffer following the
call to scanf() - I've seen this before with progs that mix scanf() and
getchar(). Easiest solution, for this program, is not to mix them. Flushing
the buffer would probably work too, but I've not tried it.

Regards,
-Pete.
getchar(); // this is used to take the character when u press the enter.
// other wise it will re take the character agin and
// whicle loop is going to operate two times.... here is the
// main problem

why u r using scanf("%d");
remove this.....
 
P

pete

ranjeet said:
use the getch instead

getch isn't part of the standard library.
What you do in the privacy of your own home,
is your own business, but no getch here.
 
R

Richard Bos

(e-mail address removed) (ranjeet) wrote:

[ Learn to snip. Do not use cuteisms such as "u" for "you".
Do not top-post. ]
The problem is of the clearence of the buffer:

Now when u enter the choice then after that u have to clear the buffer also.

As u know that *getchar* takes all the input into the buffer and then from
there it takes the value to the allocated mamory.

And the *getch*

There is no getch() in C. There's a getch() in curses, and one in conio;
neither is on-topic here, and in fact IIRC they behave similarly, but
not identically.
So try to use the getch instead of getchar:

Do not do this.
Or use the fflush function:

You can't fflush() an input stream.

All you need to do is filter out the newlines. With the hints of the
other posts in this thread, that shouldn't be hard.

Richard
 
C

CBFalconer

ranjeet said:
The problem is of the clearence of the buffer:

Now when u enter the choice then after that u have to clear the buffer also.

Please do not use silly abbreviations such as 'u' in c.l.c. It is
highly annoying and hard to read. Also do not toppost, your
answer belongs after (or intermixed with) the material to which
you reply, after snipping anything not germane to your answer.
 
R

ranjeet

Hello everyone. I am frustrated, I have written the simple program
below for a class and I am having problems with the DO-WHILE loop. On
the first run through the loop, everything works well, the menu is
displayed, the input is registered and the loop runs. On the second
(and following) runs the menu is printed twice. I am sure there is
something very basic that I am missing, but I cannot see it. Can
anyone help?

Thanks in advance.

Ken

****Here is the code****

/* currency.c - Version 1.2
This program receives a selection from the
user to determine the type of currency they
would like the exchange rate for. It then returns
the value of the amount of US currency they entered
in the currency type that they selected.*/

#include <stdio.h>

int main()
{
char nation; //Declare variable for the users choice of currencies

float us_dollar;
float canada_conv;
float euro_conv;
float krona_conv;
float yen_conv;
float pound_conv; //Declare variables of Float type

int cont = 0;

canada_conv = 1.2997; //Set exchange rates for all variables
euro_conv = 1.2056;
krona_conv = 7.5625;
yen_conv = 0.009057;
pound_conv = 0.7694;
us_dollar = 1.00; //set initial dollar amount

printf("Currency Conversion\n\n"); //Title of program

do {

cont = 0;

//Prompt user for currency
printf("Which currency would you like to convert?\n\n");
printf("Press 'C' for Canadian Dollars.\n");
printf("Press 'E' for Euros.\n");
printf("Press 'K' for Swedish Krona.\n");
printf("Press 'Y' for Japanese Yen.\n");
printf("Press 'P' for British Pounds.\n\n");
printf("Press 'Q' to quit.\n\n");

nation=getchar(); //Retrieves users choice and assigns "nation"
variable
OVER HERE U JUST ADD ONE MORE STAEMENT getchar();
every thing will work fine.
 
J

Joona I Palaste

Please do not use silly abbreviations such as 'u' in c.l.c. It is
highly annoying and hard to read.

I would have said that but it would have caused yet another flamewar.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow excitedly zooming."
- JIPsoft
 
B

Barry Schwarz

getchar(); // this is used to take the character when u press the enter.
// other wise it will re take the character agin and
// whicle loop is going to operate two times.... here is the
// main problem

And what happens if the user happens to type in multiple characters
before hitting enter?


<<Remove the del for email>>
 
R

ranjeet

Barry Schwarz said:
And what happens if the user happens to type in multiple characters
before hitting enter?
As each charcter will be replaced with the cuurent chararcter of multilipe
chararcter.
 
B

Barry Schwarz

As each charcter will be replaced with the cuurent chararcter of multilipe
chararcter.

Not until the whole loop iterates again, confusing the user just as it
did the OP.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,377
Latest member
MableYocum

Latest Threads

Top