} I am very new to C code and I''m having a lot of trouble with a homeworkassignment. This program is supposed to take the amount of Euros that the userenters and convert it to US dollars. It runs fine if the user enters a number, but if the user enters aletter it loops. I have been working on this for 3 hours now, trying different thingsleft and right. I''m sure it has something to do with the isdigitfunction. So, I tried adding in a char cResponse and switching thevalue to fResponse after checking for a number. I am fried, please help! --------------------------------------- #include <stdio.h>#include <system.h>#include <ctype.h> main () { //set variablesfloat fUSD, fEUR, fResponse; //initialize variablesfUSD = 0;fEUR = 0;fResponse = 0; //set valuesfUSD = 1.00;fEUR = .7435;//print headers for output screenprintf("\n***Currency Conversion***\n");printf("\nConverts Euro into US dollar\n");printf("\nPlease enter the amount in Euro: "); //get user inputscanf("%f", &fResponse); //check for number greater than zerowhile (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 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 chosengetch (); } 推荐答案 %。2f US dollars.\\\", (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 chosengetch (); } 刚刚开始写道: 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 homeworkassignment.This program is supposed to take the amount of Euros that the userenters and convert it to US dollars.It runs fine if the user enters a number, but if the user enters aletter it loops.why it loops almostly is due to the loop conditions, focus on theconditions to check out what''s the key point of the problem byunfolding your loop statement.I have been working on this for 3 hours now, trying different thingsleft and right. I''m sure it has something to do with the isdigitfunction.maybe, but, I dont''s think it is indispensable. So, I tried adding in a char cResponse and switching thevalue 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 variablesfloat 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;orfUSD = 0f;fEUR = 0f;fResponse = 0f; //set values fUSD = 1.00; fEUR = .7435;//print headers for output screenprintf("\n***Currency Conversion***\n");printf("\nConverts Euro into US dollar\n");printf("\nPlease enter the amount in Euro: ");//get user inputscanf("%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 bythis 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 thenumber of data it gets,and return 0 while it gets nothing.in otherwords 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 chosengetch (); } } 这篇关于货币兑换计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 02:19