我正在写一个函数getIntLimited,只能在某个最大值和最小值之间的数字之外。目前没有指定的最大值或最小值,但代码应该基本上起作用。但是,我似乎得到一个错误,说我在函数中的参数太少,但我不确定为什么会这样。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
//tools
void welcome(void);
void printTitle(void);
void printFooter(double gTotal);
void flushKeyboard(void);
void pause(void);
int getInt(void);
double getDouble(void);
int getIntLimited(int lowerLimit, int upperLimit);
//app interface
int yes(void);
void GroceryInventorySystem(void);
int menu(void);
int main(void){
int iVal;
double dVal;
welcome();
printTitle();
double grandtotal = 1234.57;
printFooter(grandtotal);
flushKeyboard();
pause();
getInt();
int lowLimit;
int upLimit;
getIntLimited(int lowLimit, int upLimit);
return 0;
}
//code your functions here:
void welcome(void)
{
printf("---=== Grocery Inventory System ===---");
printf("\n");
return;
}
void printTitle(void)
{
printf("Row |SKU| Name | Price |Taxed| Qty | Min | Total |Atn\n");
printf("----+---+---------------+-------+-----+-----+-----+-------------|---");
printf("\n");
return;
}
void printFooter(double grandTotal)
{
printf("--------------------------------------------------+-----------------");
printf("\n");
if (grandTotal > 0) {
printf(" Grand Total: | %12.2lf", grandTotal);
}
printf("\n");
return;
}
void flushKeyboard(void)
{
int read;
while (( read = getchar()) != '\n')
return;
}
void pause(void)
{
printf("Press <ENTER> to continue...\n");
flushKeyboard();
return;
}
int getInt(void)
{
int Value;
char NL = 'x';
while (NL != '\n') {
scanf("%d%c", &Value, &NL);
if (NL != '\n') {
flushKeyboard();
printf("Invalid integer, please try again: \n");
}
}
return Value;
}
int getIntLimited(int lowerLimit, int upperLimit)
{
int limit;
do {
limit = getInt();
if(lowerLimit > limit || limit > upperLimit) {
printf("Invalid value, %d < %d < %d: ", lowerLimit, limit, upperLimit);
}
}
while(lowerLimit < limit && limit < upperLimit);
return limit;
}
答案 0 :(得分:6)
在var locations = [
['FULL',3.720185, 103.124075,1],
['EMPTY',3.719693, 103.123896,0],
['FULL',3.720916, 103.12492,1],
['EMPTY',3.721032, 103.124532,0],
['FULL',3.722299, 103.124587,1],
['FULL',3.723189, 103.124706,1],
['FULL',3.725067, 103.124593,1]
];
var full = 0;
var empty = 0;
for(var i = 0; i < locations.length; i++){
for(var j = 0; j < locations[i].length; j++){
if(locations[i][j] == 'FULL') full++;
if(locations[i][j] == 'EMPTY') empty++;
}
}
console.log("full " + full + " empty " + empty );
函数中,这不是函数调用:
main
删除getIntLimited(int lowLimit, int upLimit);
个关键字:
int
另请注意,getIntLimited(lowLimit, upLimit);
和lowLimit
在传递给upLimit
时未初始化。读取未初始化的值会调用未定义的行为。
答案 1 :(得分:1)
getIntLimited(int lowLimit,int upLimit);而不是这个使用getIntLimited(lowLimit,upLimit);在函数调用期间不需要数据类型