/* this is just a function I'm trying to make for a bigger program that calcs the area of a room. When the answer <= 0 I want to get the function to take over, but I keep getting to few arguments error when I compile it. Any help will be gladly appreciated.*/
#include <stdio.h>
int validInput(double len);
int main(void)
{
double len ;
int check;
printf("\nEnter length of room in feet:");
scanf("%lf", &len);
check = validInput();
return 0;
}
int validInput(double len)
{
int check;
if (len <= 0 )
printf("\nNumbers entered must be positive.");
return check;
}
答案 0 :(得分:1)
First... you didn't pass any value to validInput(). I guess your code here above won't even compile...
You should change this line:
check = validInput();
to:
check = validInput(len);
Also consider that variable "check" returned by your function is neither initialised nor set...
P.S. please indent your code...
答案 1 :(得分:1)
这一行:
check = validInput();
缺少参数。建议:
check = validInput( len );
当然,代码应该从调用scanf()
检查返回值而不是参数值,以确保操作成功。在这种情况下,返回值应为1.任何其他返回值都表示发生了错误。
关于validInput()
函数:
变量check
永远不会初始化为任何特定值。建议:
int validInput(double len)
{
int check = 1; // indicate valid
if (len <= 0 )
{
check = 0; // indicate not valid
printf("\nNumbers entered must be positive.\n");
}
return check;
}
注意:调用\n
时的尾随printf()
是强制立即输出文本而不是在内部stdout
缓冲区中设置,直到程序退出,此时它会输出。
将适当的错误检查纳入main()
函数会导致:
#include <stdio.h> // scanf(), printf()
#include <stdlib.h> // exit(), EXIT_FAILURE
int validInput(double len);
int main(void)
{
double len ;
int check;
printf("\nEnter length of room in feet:");
if( 1 != scanf("%lf", &len) )
{ // then scanf failed
perror( "scanf for length of room, in feet, failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
check = validInput( len );
printf( "the user input was %s\n", (check)? "valid" : "not valid");
return 0;
} // end function: main