get_double输入函数(getchar + scanf) [英] get_double input function (getchar+scanf)
问题描述
因为我是新手,我有一些小而快的(可能)愚蠢的
问题:-)
这是我的get_double使用默认参数的函数
类型为double。该函数返回一个double类型的值,所以我可以写一下像$ value $ get $ double(0.5);在我的程序中。
--------------
double get_double(双默认)
{
双输入;
char ch;
while(scanf("%lf" ,& input)!= 1)/ *如果未输入有效输入* /
{
while((ch = getchar())!=''\\ \\ n'')
putchar(ch); / *处理错误输入* /
printf(无效输入。假设默认值:%lf \ n,默认值);
input = default;
}
返回输入;
}
--------- -----
编译器说:
语法错误:在''默认'之前缺少''''''br />
语法错误:在''默认'之前缺少''''''
语法错误:''默认''
语法错误:'' )''
它可能认为我正在试图投入一些价值来打造双倍的价值
猜猜....?抱怨的是什么?
它在开头宣布为:
double get_double(double); / *输入类型为double * /
Med venlig hilsen /最好的问候
Martin J?rgensen
-
------------------------------------------- --------------------------------
Martin J?rgensen的家 - http://www.martinjoergensen.dk
你好,
默认是C中的关键字。
它在switch语句中使用..
尝试改变那个;我唯一看到的就是问题
你发布的代码中的
..
Broeisi
Martin J?rgensen写道:
因为我是新手我也有一些小而快的(可能)愚蠢的问题:-)
这是我的get_double
类型为double的默认参数的函数。该函数返回double类型的值,所以我可以写一些像value = getdouble(0.5);在我的程序中。
--------------
double get_double(双默认)
{
双输入;
char ch;
while(scanf("%lf",& input)!= 1)/ *如果没有输入有效输入* /
{
while((ch = getchar())!=''\ n'')
putchar(ch); / *处理错误输入* /
printf(无效输入。假设默认值:%lf \ n,默认值);
input = default;
}
返回输入;
--------------
编译器说:
>语法错误:在''默认''之前缺少'''''语法错误:在''默认''之前缺少'''''
语法错误:''默认''
语法错误:'')''
它可能认为我正在尝试输入一些值来输入双倍的猜测......?抱怨的是什么?
它在开头宣布为:
double get_double(double); / *输入类型为double * /
Med venlig hilsen /最好的问候
Martin J?rgensen
>
Martin J?rgensen在02/16/06 13:45写道:[...]
double get_double(双默认)
[...]
编译器说:
语法错误:缺少'')''''默认'''之前/ blockquote>
`default''是一个关键字;你不能将它用作
标识符。你写的非常像
double get_double(双倍返回)
-
呃********* @ sun.com
Eric Sosman写道:
-snip-
`default''是一个关键词;你不能将它用作
标识符。你写的非常像
double get_double(双返)
好的,谢谢Eric。
下一个(小)问题:
double get_double(double defaultvalue)
{
双输入;
if(scanf("%lf",& input)!= 1 || input ==''\ n''|| input< 0 ||输入> 1
)/ *如果未输入有效输入* /
{
printf("那是无效输入( 0 <= theta< = 1)。假设默认
值:%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) stupid
questions also :-)
This is my "get_double" function which takes a default argument also of
type double. The function returns a value of type double so I can write
something 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 I
guess....? 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 regards
Martin J?rgensen
--
---------------------------------------------------------------------------
Home of Martin J?rgensen - http://www.martinjoergensen.dk解决方案Hello,
default is a keyword in C.
It''s used in the switch statement..
Try changing that as that;s the only thing I see that can be a problem
in the code you posted..
Broeisi
Martin J?rgensen wrote:
Hi,
Since I''m a newbie I have some small but quick (probably) stupid
questions also :-)
This is my "get_double" function which takes a default argument also of
type double. The function returns a value of type double so I can write
something 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 I
guess....? 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 regards
Martin J?rgensen
Martin J?rgensen wrote On 02/16/06 13:45,:[...]
double get_double(double default)
[...]
The compiler says:
syntax error : missing '')'' before ''default''
`default'' is a keyword; you cannot use it as an
identifier. What you''ve written is very much like
double get_double(double return)
--
Er*********@sun.com
Eric Sosman wrote:
-snip-
`default'' is a keyword; you cannot use it as an
identifier. What you''ve written is very much like
double get_double(double return)
Ok, 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 default
value: %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 regards
Martin J?rgensen
--
---------------------------------------------------------------------------
Home of Martin J?rgensen - http://www.martinjoergensen.dk
这篇关于get_double输入函数(getchar + scanf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!