DO-WHILE循环问题

DO-WHILE循环问题

问题描述:

大家好。我很沮丧,我已经为一个类编写了下面的简单程序

,而且我遇到了DO-WHILE循环的问题。在
第一次循环运行时,一切正常,菜单显示为

,输入已注册并且循环运行。在第二个

(及以下)运行时,菜单打印两次。我确信有一些非常基本的东西,我错过了,但我看不到它。可以

有人帮忙吗?


提前致谢。


Ken


****这是代码****


/ * currency.c - 版本1.2

此程序收到来自

用户确定他们的货币类型

想要汇率。然后它返回

他们输入的美元货币价值

他们选择的货币类型。* /


#include< stdio.h>


int main()

{

char nation; //为用户选择的货币声明变量


浮动us_dollar;

浮动canada_conv;

浮动euro_conv;

浮动krona_conv;

浮动yen_conv;

浮动pound_conv; //声明Float类型的变量


int cont = 0;


canada_conv = 1.2997; //设置所有变量的汇率

euro_conv = 1.2056;

krona_conv = 7.5625;

yen_conv = 0.009057;

pound_conv = 0.7694;

us_dollar = 1.00; //设置初始金额


printf(" Currency Conversion\\\
\ n); //程序标题


do {


cont = 0;


//提示用户的货币

printf(你要转换哪种货币?\ n \ nn);

printf("按''C'' for Canadian Dollars.\\\
;

printf(按''''代表Euros.\ nn;);

printf(" Press K代表瑞典语Krona。\ n");

printf(按''Y'代表日元。\ nn);

printf(英国英镑的''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' n);


nation = getchar(); //检索用户选择并指定国家

变量


switch(国家)//根据用户给出金额的选择过程

选择

{

案例''C'':

案例''c'':

printf(&\\; \ n请输入您想要的美元金额

convert.\\\
\ n);

scanf("%f" ;,& us_dollar); //收集他们想要的美元金额

兑换

printf(\ n $%1.2f美元价值%1.2f加元。\ n \\ n",

us_dollar,us_dollar * canada_conv);

cont = 0;

休息;


案例''E'':

案例''e'':

printf(\ n请输入您想要的美元金额

convert.\\\
\ n");

scanf("%f",& us_dollar); //收集他们想要的美元金额

兑换

printf(" \ n $%1.2f美元价值%1.2f Euros.\ n, us_dollar,us_dollar

* euro_conv);

cont = 0;

休息;


案例''K'':

case''k'':

printf(\ n请输入你想要的美元金额

convert.\\\
\\\
");

scanf("%f",& us_dollar); //收集他们希望的美元金额

兑换

printf(\ n $%1.2f美元价值%1.2f瑞典克朗。\ n ,us_dollar,

us_dollar * krona_conv);

cont = 0;

休息;


案例''Y'':

案例''y'':

printf(&\\; \ n请输入您想要的美元金额

convert.\\\
\\\
");

scanf("%f",& us_dollar); //收集他们希望的美元金额

兑换

printf(" \ n $%1.2f美元价值%1。2日元日元。\ n ,us_dollar,

us_dollar * yen_conv);

cont = 0;

休息;


案例''P'':

案例''p'':

printf(&\\; \ n请输入您想要的美元金额

convert.\\\
\\\
");

scanf("%f",& us_dollar); //收集他们想要的美元金额

兑换

printf(\ n $%1.2f美元价值%1.2f英镑。#n" ,us_dollar,

us_dollar * pound_conv);

cont = 0;

休息;


案例''Q'':

案例''q'':

cont = 1;

printf("感谢您使用货币兑换程序。\ n \\ nn \\;);

休息;


默认://这是错误检查。如果输入错误的

选择,他们就会关闭

休息;

}


} while(cont!= 1);

//给用户时间查看结果和结束程序

printf(" \ n\\\
按任​​意键并输入退出程序。\ n");

scanf("%d");

返回0;

}

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

%1.2f美国的价值%1.2f加拿大元。\ n \\ nn \\,

us_dollar,us_dollar * canada_conv);

cont = 0;

休息;


案例''E'':

case''e'':

printf(\ n请输入您想要的美元金额

convert.\\\
\ n );

scanf("%f",& us_dollar); //收集他们想要的美元金额

兑换

printf(" \\\
%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美国值得%1.2f Euros.\ n,us_dollar,us_dollar

* euro_conv);

cont = 0;

break;


案例''K'':

案例''k'':

printf(\ n请输入美元你想要的金额

convert.\\\
\ n");

scanf("%f",& us_dollar); //收集他们想要的美元金额

兑换

printf(" \\\
%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美国值得%1.2f瑞典语Krona.\,us_dollar,

us_dollar * krona_conv);

cont = 0;

break;


案例''Y'':

案例''y'':

printf(\ n \\ n请输入美国你想要的美元金额

convert.\\\
\ n");

scanf("%f",& us_dollar); //收集他们想要的美元金额

兑换

printf(" \\\
%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