C++ 如何修复函数的大小未知或为零,以及主函数中缺少函数调用

C++ 如何修复函数的大小未知或为零,以及主函数中缺少函数调用,c++,C++,我试图完成这个问题,但我不知道如何摆脱错误。我看过的其他使用数组和函数的代码看起来是一样的,可以工作,但我的代码由于某种原因无法工作。以下是我得到的错误: Error E2121 Q1.cpp 27: Function call missing ) in function main() Error E2449 Q1.cpp 32: Size of 'fifty' is unknown or zero Error E2356 Q1.cpp 32: Type mismatch in redeclar

我试图完成这个问题,但我不知道如何摆脱错误。我看过的其他使用数组和函数的代码看起来是一样的,可以工作,但我的代码由于某种原因无法工作。以下是我得到的错误:

Error E2121 Q1.cpp 27: Function call missing ) in function main()
Error E2449 Q1.cpp 32: Size of 'fifty' is unknown or zero
Error E2356 Q1.cpp 32: Type mismatch in redeclaration of 'fifty(int)'
Error E2344 Q1.cpp 10: Earlier declaration of 'fifty(int)'
Error E2063 Q1.cpp 32: Illegal initialization
Error E2293 Q1.cpp 32: ) expected
Error E2141 Q1.cpp 71: Declaration syntax error
大体上

这句话缺少一个逗号

一定是

printf("50 = %d, 20 = %d, 10 = %d, 5 = %d", money[1], money[2], money[3], money[4]);
fifty(money);

您不能用数字命名参数,您期望的是什么

还有回报;在你的虚空结束时,函数是无用的

大体上,您不初始化money[0],但所有其他函数都测试它的值,行为是未定义的。也许你想这么做

scanf("%d", &money[0]);
我还鼓励您检查通过scanf输入的值,检查它返回1。注意你是在C++中,所以你也可以使用ISTRAM运算符>< /P> 在另一个函数中,您以货币形式递增条目,但这些条目未初始化,可能您希望:

int money[5] = {0}; 
正在将5个元素初始化为0

由于需要在另一个函数中访问数组中的多个元素,因此不必指定特定元素,而是指定所有数组,例如:

void count(int money[], int value, int index)
{
    money[index] = money[0]/value;
    money[0] %= value;
}
一定是

printf("50 = %d, 20 = %d, 10 = %d, 5 = %d", money[1], money[2], money[3], money[4]);
fifty(money);
其他电话也一样

注意:例如,假设每个值只有0或1倍

void fifty(int money)
{
    /*This is getting the whole array from main() and modifying the values of each index*/
    if (money[0] > 50) {
        money[0] = money[0] - 50;
        money[1]++;
    }
}
如果money为100,则必须将money[1]设置为2,而不是1,以便:

void fifty(int money[])
{
    /*This is getting the whole array from main() and modifying the values of each index*/
    money[1] = money[0]/50;
    money[0] %= 50;
}
要做到这一点,每个值都会为所有函数生成一个类似的定义,这是没有用的,您只能定义一个函数来获取要设置的值和索引,例如:

void count(int money[], int value, int index)
{
    money[index] = money[0]/value;
    money[0] %= value;
}
而不是打电话

你能行

count(money, 50, 1);
count(money, 20, 2);
count(money, 10, 3);
count(money, 5, 4);
或者使用任何其他方式,如循环

请注意,您没有指明值1的数目,这似乎很奇怪

一项考虑到我的意见的建议:

#include <stdio.h>

void count(int money[], int index, int value);

int main()
{
  int money[5]; // useless to initialize it because I just assign entries

  printf("Enter cash amount: ");

  if (scanf("%d", &money[0]) != 1) {
    puts("invalid input, abort");
    return -1;
  }

  count(money, 50, 1);
  count(money, 20, 2);
  count(money, 10, 3);
  count(money, 5, 4);

  /*The next two lines should print out how many of each denomination is needed*/
  printf("Change to be given: \n");
  printf("50 = %d, 20 = %d, 10 = %d, 5 = %d\n", money[1], money[2], money[3], money[4]);
  return(0);
}

void count(int money[], int value, int index)
{
  money[index] = money[0]/value;
  money[0] %= value;
}
使用iostream而不是stdio:

现在,count只在代码中的一个位置被调用,因此可以移动其主体以替换其调用:

#include <iostream>

int main()
{
  const int value[] = { 50, 20, 10, 5, 1 }; // list of bills
  int money[sizeof(value)/sizeof(value[0]) + 1];

  std::cout << "Enter cash amount: ";

  if (! (std::cin >> money[0])) {
    std::cout << "invalid input, abort" << std::endl;
    return -1;
  }

  for (size_t i = 0; i != sizeof(value)/sizeof(value[0]); ++i){
    money[i + 1] = money[0]/value[i];
    money[0] %= value[i];
  }

  std::cout << "Change to be given: \n";

  for (size_t i = 0; i != sizeof(value)/sizeof(value[0]); ++i) {
    if (i != 0)
      std::cout << ", ";
    std::cout << value[i] << " = " << money[i+1];
  }
  std::cout << std::endl;

  return(0);
}

例如,如果我想同时考虑100美元的账单,我只需要替换const int value[]={50,20,10,5,1};按常量int值[]={100,50,20,10,5,1};无需对代码进行其他更改。这样做对于代码的可维护性非常重要。

哦,亲爱的。这里面有很多错误

#include <math.h>
#include <stdio.h>
#include <iostream>

void fifty(int money);
输入错误应该是contains,并且您已经定义了一个数组,但尚未初始化它-因此它将包含不确定的值。您需要对其进行初始化,使其包含零。我进一步进行的一些更改实际上意味着您不需要对其进行初始化-因为我设置了变量而不是递增:

    int money[5] = {};
    printf("Enter cash amount: ");

    scanf("%d", &money[1]);
这将把价值储存在货币中[1]。你真的想要钱[0]-所以:

它不会传递数组,而是传递偏移量5处的单个元素,偏移量5不存在。唯一的条目是money[0]、money[1]、money[2]、money[3]、money[4]

字符串后面需要一个逗号:

    printf("50 = %d, 20 = %d, 10 = %d, 5 = %d", money[1], money[2], money[3], money[4]);
    return(0);
}

void fifty(int money, int 5)
这不是声明以数组为参数的函数的方式:

void fifty(int* money)
{
    //This is getting the whole array from main() and modifying the values of each index
    if (money[0] > 50) {
        money[0] = money[0] - 50;
        money[1]++;
    }
    return;
嗯,是的。一旦我们将数组初始化为零,它就可以工作了。但是如果我在提示符下输入440,会发生什么呢?这只会输出一张50美元的钞票——我想要八张

相反,您需要将money[1]增加money[0]/50甚至更好,只需将其设置为该值,然后将money[0]设置为余数。很好的效果是,您不需要if,因为divide向下舍入

你也不需要退货,但是如果你愿意,你可以留下

void fifty(int* money)
{
   money[1] = money[0]/50;
   money[0] = money[0]%50;  // Must do this second.
    return;
}

void twenty(int *money)
{

    if (money[0] > 20) {
        money[0] = money[0] - 20;
        money[2]++;
    }
    return;
}
等一下!我们以前见过这个代码!除了上次使用的偏移量为1和50之外。是时候举行一次活动了。这有点令人讨厌,因为对这两种类型都使用int意味着很容易使参数出错。正确的解决方案是让money成为一个struct,并使用一个指向` offset'的成员变量的指针——但我怀疑目前这有点过于机械化了

void change(int* money, int bill, int offset)
{
   money[offset] = money[0]/bill;
   money[0] = money[0]%bill;  // Must be second.
}

void fifty(int* money)
{
   change(money, 1, 50);
}

void twenty(int* money)
{
   change(money, 2, 20);
}

void ten(int* money)
{
   change(money, 3, 10);
}

void five(int* money)
{
   change(money, 4, 5);
}

最后,作为一个例子,让参数以错误的方式改变是多么容易,上面的代码正是我键入它的方式——我就是这么做的。修正参数留给读者作为练习。

其他使用数组的代码看起来根本不一样;你需要更仔细地阅读,包括。是什么阻止你简单地将工作代码与损坏的代码进行比较?他的意思是无效的fiftyint money[5]或无效的fiftyint*money-这是同一回事,需要称之为fiftymoneySnap!我们得到了非常相似的代码:-@Epic_Lemon我用更多的评论和完整的内容编辑了我的答案proposals@MartinBonner这并不奇怪^^^@Epic_Lemon我再次编辑了我的答案,建议只修改一行代码就可以更改法案列表,请看我写了这么长的答案!提高
#include <math.h>
#include <stdio.h>
#include <iostream>

void fifty(int money);
void fifty(int* money);
void twenty(int* money);
void ten(int* money);
void five(int* money);


int main()
{
    /*this contians all info about change and the money that you want change for*/
    int money[5];
    int money[5] = {};
    printf("Enter cash amount: ");

    scanf("%d", &money[1]);
    scanf("%d", &money[0]);

    fifty(money[5]); /*Passing the array to each module*/
    fifty(money); // Pass the whole array to each function
    twenty(money);
    ten(money);
    five(money);
    /*The next two lines should print out how many of each denomination is needed*/
    printf("Change to be given: \n");
    printf("50 = %d, 20 = %d, 10 = %d, 5 = %d" money[1], money[2], money[3], money[4]);
    printf("50 = %d, 20 = %d, 10 = %d, 5 = %d", money[1], money[2], money[3], money[4]);
    return(0);
}

void fifty(int money, int 5)
void fifty(int* money)
{
    //This is getting the whole array from main() and modifying the values of each index
    if (money[0] > 50) {
        money[0] = money[0] - 50;
        money[1]++;
    }
    return;
void fifty(int* money)
{
   money[1] = money[0]/50;
   money[0] = money[0]%50;  // Must do this second.
    return;
}

void twenty(int *money)
{

    if (money[0] > 20) {
        money[0] = money[0] - 20;
        money[2]++;
    }
    return;
}
void change(int* money, int bill, int offset)
{
   money[offset] = money[0]/bill;
   money[0] = money[0]%bill;  // Must be second.
}

void fifty(int* money)
{
   change(money, 1, 50);
}

void twenty(int* money)
{
   change(money, 2, 20);
}

void ten(int* money)
{
   change(money, 3, 10);
}

void five(int* money)
{
   change(money, 4, 5);
}