简体   繁体   English

使用C ++包含多个文件

[英]Including multiple files with C++

This week my class took a spin and is teaching material not found in the book. 这周我的课开了一小会儿,正在上书所没有的教学材料。 I'm using Visual Studio 2010, the project is to take 5 numbers from the keyboard and get the average, but I have to use functions with a .h header file and corresponding .cpp file to receive credit. 我使用的是Visual Studio 2010,该项目是从键盘上提取5个数字并获得平均值,但是我必须使用带有.h头文件和相应的.cpp文件的函数来接收积分。 This is what I have so far 这就是我到目前为止

Main.cpp Main.cpp

#include <iostream>
#include "average.h"

using namespace std;

const int numbersinput=5;

int main ()
{
  int numbers,sum,avg;

  cout << "Hello, please enter 5 numbers you would like to see the average for." << endl;

  for (int i = 0; i != numbersinput; ++i)
  {
    cin >> numbers;

    sum += numbers;
  }

  int average(int sum);
  cout << avg;

  system ("PAUSE");
  return 0;
}

The .h headerfile named average.h .h头文件,名为average.h

#include <iostream>

using namespace std;

int average(int);

and the other .cpp file named average.cpp 另一个.cpp文件名为average.cpp

#include <iostream>
#include "average.h"

using namespace std;

const int numbersinput=5;
int avg;

int average(int sum)
{
  avg = sum /numbersinput;
  return avg;
}

I can get a successful build, but I get this error after I enter the first number and press enter. 我可以获得成功的构建,但是在输入第一个数字并按Enter后出现此错误。

Run-Time Check Failure #3 - The variable 'sum' is being used without being initialized. 运行时检查失败#3-变量'sum'未被使用而被初始化。

What am I not getting here? 我什么不来?

You might want to initialize sum to 0 before you start adding to it with += : 您可能想将sum初始化为0然后再开始使用+=对其进行添加:

So instead of int numbers,sum,avg; 因此int numbers,sum,avg;而不是int numbers,sum,avg;而是求int numbers,sum,avg; You will have int sum = 0; 您将得到int sum = 0; etc. 等等

Change this line: 更改此行:

int numbers,sum,avg;

To: 至:

int numbers=0;
int sum=0;
int avg=0;

This gives the variables values- before they are initialized (given a value) they are undefined, which means that they could equal any value. 这将为变量赋值-在它们被初始化(给定值)之前,它们是不确定的,这意味着它们可以等于任何值。 By initializing them, you are giving them a number to add to when you make the sum. 通过初始化它们,您可以给它们一个加和的数字。

Edit these lines: 编辑这些行:

int average(int sum);

cout << avg;

To: 至:

cout << average(sum);

The function declaration for int average(int sum) is not called in the same way as it is declared. int average(int sum)的函数声明的调用方式与声明方式不同。 The ints are unnecessary. 整数是不必要的。 In my edited code, you can see that the returned value ( average )is printed out, instead of being left without being used. 在我编辑的代码中,您可以看到打印出了返回值( average ),而不是不使用它而被留下。

Also, as a general tip, try not to give variables the same name. 另外,作为一般提示,请尽量不要给变量起相同的名称。 Try to make the function variable sum be called sumToAverage or instead make the sum in main be called total . 尝试使函数变量的sum称为sumToAverage或者使mainsum称为total It is a good idea to pick variable names that are different, so you won't get confused. 选择不同的变量名是一个好主意,因此您不会感到困惑。

You're saying : 你是说:

sum += numbers

But you haven't initialized sum , so it will have some random value that's on the stack. 但是您尚未初始化sum ,因此它将具有一些在堆栈上的随机值。 Change your declaration of sum to: sum声明更改为:

int sum = 0;

Also, you're using global variables to pass information to functions, which isn't a good idea. 另外,您正在使用全局变量将信息传递给函数,这不是一个好主意。 Get rid of the avg variable and change average to: 摆脱avg变量,将average更改为:

int average(int sum, int numberofvalues)
{
  int avg = sum / numberofvalues;
  return avg;
}

You're also re-declared average in the body of main, which you don't need to do as it's in the header. 您还将主体中的average重新声明为average ,您无需这样做,因为它位于标题中。 Then, you can get the average in main by saying: 然后,您可以通过说出主要平均值:

int avg = average(sum, numbersinput);

Now, main will look like this: 现在, main将如下所示:

int main ()
{
 int sum=0;

 cout << "Hello, please enter 5 numbers you would like to see the average for." << endl;

 for (int i = 0; i != numbersinput; ++i)
 {
   int number;
   cin >> number;

   sum += number;
 }


 int avg = average(sum, numbersinput);

 cout << avg;    

 system ("PAUSE");

 return 0;
}

Oh, and don't put using namespace std in header files! 哦,不要在头文件中using namespace std

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM