好的,谢谢Eric。 下一个(小)问题: double get_double(double defaultvalue) { 双输入; if(scanf("%lf",& input)!= 1 || input ==''\ n''|| input< 0 ||输入> 1 )/ *如果未输入有效输入* / { printf("那是无效输入( 0 值:%lf \ n",defaultvalue); input = defaultvalue; } 返回输入; } 如果我点击输入它不会立即为输入分配defaultvalue, 但是一直在等待。我想:如果用户点击进入,输入= defaultvalue;我以为\ n做了这个工作,但它没有... Med venlig hilsen /祝好问候 Martin J?rgensen - --------------------------------------- ------------------------------------ Martin J?rgensen的故乡 - http://www.martinjoergensen.dk Hi,Since I''m a newbie I have some small but quick (probably) stupidquestions also :-)This is my "get_double" function which takes a default argument also oftype double. The function returns a value of type double so I can writesomething like value = getdouble(0.5); in my program.--------------double get_double(double default){double input;char ch;while (scanf("%lf", &input) != 1) /* if valid input was not entered */{while ((ch = getchar()) != ''\n'')putchar(ch); /* dispose bad input */printf(" is not valid input. Assuming default value: %lf\n", default);input = default;}return input;}--------------The compiler says:syntax error : missing '')'' before ''default''syntax error : missing ''{'' before ''default''syntax error : ''default''syntax error : '')''It probably thinks I''m trying to cast some value to type double Iguess....? What is it complaining about?It is declared in the beginning as:double get_double(double); /* get input of type double */Med venlig hilsen / Best regardsMartin J?rgensen-----------------------------------------------------------------------------Home of Martin J?rgensen - http://www.martinjoergensen.dk 解决方案`default'' is a keyword; you cannot use it as anidentifier. What you''ve written is very much likedouble get_double(double return)-- Er*********@sun.comOk, thanks Eric.Next (small) problem:double get_double(double defaultvalue){double input;if (scanf("%lf", &input) != 1 || input == ''\n'' || input < 0 || input >1) /* if valid input was not entered */{printf("That is not valid input (0 <= theta <= 1). Assuming defaultvalue: %lf\n", defaultvalue);input = defaultvalue;}return input;}If I hit "enter" it doesn''t immediately assign defaultvalue to input,but keeps waiting for something. I want: If the user hits enter, input =defaultvalue; I thought the "\n" did the job, but it doesn''t...Med venlig hilsen / Best regardsMartin J?rgensen-----------------------------------------------------------------------------Home of Martin J?rgensen - http://www.martinjoergensen.dk 这篇关于get_double输入函数(getchar + scanf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-15 10:15