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;
}
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;
}