货币兑换计划 [英] Currency conversion program

查看:83
本文介绍了货币兑换计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C代码很新,我在做作业时遇到很多麻烦

分配。


这个程序应该是拿用户

输入的欧元金额并将其兑换成美元。


如果用户输入一个号码,它运行正常,但如果是用户输入一封

的信件。


我现在已经工作了3个小时,尝试不同的东西

剩下的对。我确定它与isdigit

函数有关。所以,我尝试添加一个char cResponse并在检查一个数字后将

值切换到fResponse。


我被炸了,请帮助!


---------------------------------------


#include< stdio.h>

#include< system.h>

#include< ctype.h> ;


main()


{


//设置变量

float fUSD,fEUR,fResponse;


//初始化变量

fUSD = 0;

fEUR = 0;

fResponse = 0;


//设定值

fUSD = 1.00;

fEUR = .7435;

//打印输出屏幕的标题

printf(" \\\
***货币转换*** \ n");

printf(\ nConverts Euro into US dollar \ n);

printf(\ n请输入欧元金额:);


//获取用户输入

scanf("%f",& fResponse);

//检查大于零的数字

while(fResponse< = 0.0){

if(isdigit(fResponse)){

printf(" \\\
您输入的数字是invald.\ n);

printf("请输入大于零的数字:");

scanf("%f",& fResponse);

}

if(isdigit(fResponse)== 0)

printf(\ nYou你输入了一封信。\ n);

printf(请输入一个大于零的数字:);

scanf("%f"& fResponse);


}


printf(" \\\
您输入的欧元转换为$%。2f US

dollar.\ n,(fUSD / fEUR)* fResponse);


printf(" ; \ n\\\
按任​​意键关闭此窗口。 。 。);


// getch函数将结果保留在屏幕上直到选择任何键

getch();

}

I am very new to C code and I''m having a lot of trouble with a homework
assignment.

This program is supposed to take the amount of Euros that the user
enters and convert it to US dollars.

It runs fine if the user enters a number, but if the user enters a
letter it loops.

I have been working on this for 3 hours now, trying different things
left and right. I''m sure it has something to do with the isdigit
function. So, I tried adding in a char cResponse and switching the
value to fResponse after checking for a number.

I am fried, please help!

---------------------------------------

#include <stdio.h>
#include <system.h>
#include <ctype.h>

main ()

{

//set variables
float fUSD, fEUR, fResponse;

//initialize variables
fUSD = 0;
fEUR = 0;
fResponse = 0;

//set values
fUSD = 1.00;
fEUR = .7435;
//print headers for output screen
printf("\n***Currency Conversion***\n");
printf("\nConverts Euro into US dollar\n");
printf("\nPlease enter the amount in Euro: ");

//get user input
scanf("%f", &fResponse);

//check for number greater than zero
while (fResponse<=0.0) {
if (isdigit(fResponse)) {
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
if (isdigit(fResponse)==0)
printf("\nYou entered a letter.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);

}

printf("\nThe value of the Euro you entered converts to $%.2f US
dollars.\n", (fUSD/fEUR) * fResponse);

printf("\n\nPress any key to close this window. . .");

//getch function to leave results on screen until any key is chosen
getch ();

}

推荐答案

%。2f US

dollars.\\\
", (fUSD / fEUR)* fResponse);


printf(\ n \ n按任意键关闭此窗口... ...;


// getch函数将结果保留在屏幕上直到选择任何键

getch();


}

%.2f US
dollars.\n", (fUSD/fEUR) * fResponse);

printf("\n\nPress any key to close this window. . .");

//getch function to leave results on screen until any key is chosen
getch ();

}


刚刚开始写道:
Just starting out wrote:
我对C代码很新,我''在做作业
任务时遇到很多麻烦。


这个程序应该花费用户进入的欧元金额并将其兑换成美元。


如果用户输入一个数字,它运行正常,但如果用户输入一个
字母就会循环播放。
为什么它几乎循环是由于循环条件,关注

条件来检查问题的关键点是

展开你的循环语句。

我现在已经工作了3个小时,尝试不同的东西
左右。我确定它与isdigit
功能有关。
也许,但是,我不认为它是不可或缺的。所以,我尝试添加一个char cResponse并在检查一个数字后将
值切换到fResponse。


我被炒了,请帮忙!



------------------------------ * --- ------

I am very new to C code and I''m having a lot of trouble with a homework
assignment.
This program is supposed to take the amount of Euros that the user
enters and convert it to US dollars.
It runs fine if the user enters a number, but if the user enters a
letter it loops. why it loops almostly is due to the loop conditions, focus on the
conditions to check out what''s the key point of the problem by
unfolding your loop statement.
I have been working on this for 3 hours now, trying different things
left and right. I''m sure it has something to do with the isdigit
function. maybe, but, I dont''s think it is indispensable. So, I tried adding in a char cResponse and switching the
value to fResponse after checking for a number.
I am fried, please help!

------------------------------*---------

#include< stdio.h>
#include< system.h>
#include<文件ctype.h>


main()
未定义的行为。

int main(无效)

{


//设置变量
float fUSD,fEUR,fResponse;


//初始化变量
fUSD = 0;
fEUR = 0;
fResponse = 0;
我认为初始化可能更好如下:

fUSD = 0.0;

fEUR = 0.0;

fResponse = 0.0;



fUSD = 0f;

fEUR = 0f;

fResponse = 0f;

//设定值
fUSD = 1.00;
fEUR = .7435;


//打印输出屏幕的标题
printf(" \\\
***货币转换*** \ n");
printf( \ n将欧元兑换成美元兑换;;
printf(\ n请输入欧元金额:);


//获取用户输入
scanf("%f",& fResponse);


//检查数字大于零
while(fResponse< = 0.0){
if(isdigit(fResponse)){
printf( \\\
您输入的数字是invald.\ n;);
printf(请输入一个大于零的数字:);
scanf("%f",& ; fResponse);
}
if(isdigit(fResponse)== 0)
printf(&\\;你输了一封信。\ n");
printf( 请输入大于零的数字:;;
scanf("%f",& fResponse);

}
#include <stdio.h>
#include <system.h>
#include <ctype.h>
main () undefined behavior.
int main(void)
{
//set variables
float fUSD, fEUR, fResponse;
//initialize variables
fUSD = 0;
fEUR = 0;
fResponse = 0; I think it may be better to initialize like the following:
fUSD = 0.0;
fEUR = 0.0;
fResponse = 0.0;
or
fUSD = 0f;
fEUR = 0f;
fResponse = 0f;
//set values
fUSD = 1.00;
fEUR = .7435;
//print headers for output screen
printf("\n***Currency Conversion***\n");
printf("\nConverts Euro into US dollar\n");
printf("\nPlease enter the amount in Euro: ");
//get user input
scanf("%f", &fResponse);
//check for number greater than zero
while (fResponse<=0.0) { if (isdigit(fResponse)) {
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
if (isdigit(fResponse)==0)
printf("\nYou entered a letter.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}



我认为代码片段获取用户输入以上可以用

替换这一个:

************************** ************************ *********

while(scanf("%f" ,& fResponse)== 0)

{

printf(\ n您输入的数字是invald.\ n);

printf(请输入一个大于零的数字:);

getchar();

}

** ************************************************ ** ********

如果scanf("%f",& fResponse)"得到一个数字数据,它将返回它获得的数据的数量,并返回0,而它什么也没有。在其他

的话,它什么都没有意味着用户输入错误的格式数据。

getchar()将在下一个循环开始之前吃掉前一个scanf()

留下的新行字符。
printf(" \\\
您输入的欧元值转换为


I think the code fragment "get user input" above could be replaced by
this one:
************************************************** *********
while(scanf("%f", &fResponse) == 0)
{
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
getchar();
}
************************************************** **********
if "scanf("%f", &fResponse)" get a digital data, it will return the
number of data it gets,and return 0 while it gets nothing.in other
words it gets nothing means the user input the wrong format data.
getchar() will eat the new-line character left by the previous scanf()
before the next loop starting. printf("\nThe value of the Euro you entered converts to


%。2f USdollars.\ n,(fUSD / fEUR)* fResponse);


printf(\ n \ n按任意键关闭此窗口......);


// getch函数将结果保留在屏幕上直到选择任何键
getch();
%.2f USdollars.\n", (fUSD/fEUR) * fResponse);
printf("\n\nPress any key to close this window. . .");
//getch function to leave results on screen until any key is chosen
getch ();




}



}


这篇关于货币兑换计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆