Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;(使用文件数据通过函数进行计算)_C++ - Fatal编程技术网

C++ C++;(使用文件数据通过函数进行计算)

C++ C++;(使用文件数据通过函数进行计算),c++,C++,在一个函数中读取文件后,如何使用该文件,并在另一个函数中进行计算 这是我试图编写的代码 void readFile(ifstream&); //file reading void DisplayAdd(ifstream& somefile); //here what i don't understand do i use void or int ! //and how is it going to use the data from the file !! int

在一个函数中读取文件后,如何使用该文件,并在另一个函数中进行计算

这是我试图编写的代码

void readFile(ifstream&); //file reading 

void DisplayAdd(ifstream& somefile);  //here what i don't understand do i use void or int !  
//and how is it going to use the data from the file !!


int main()
{
ifstream inputfile;
inputfile.open("numbers.txt");
readFile(inputfile);         //calling function to make reading file
DisplayAdd(inputfile);      //calling function to make addition
}
inputfile.close(); //closing file
return 0;
}

void readFile(ifstream& somefile) //reading data from file ..
{
double num1,num2,num3,num4,num5;
while(somefile>>num1>>num2>>num3>>num4>>num5)
    cout<<num1<<endl<<num2<<endl<<num3<<endl<<num4<<endl<<num5<<endl;
}

void DisplayAdd(ifstream& somefile) //how to make the calculation here is it like this !
{cout<<num1+num2+num3+num4+num5;
}
void readFile(ifstream&)//文件读取
void DisplayAdd(ifstream&somefile)//这里我不明白的是我用void还是int!
//它将如何使用文件中的数据!!
int main()
{
ifstream输入文件;
打开(“numbers.txt”);
readFile(inputfile);//调用函数使读取文件
DisplayAdd(inputfile);//调用函数进行添加
}
inputfile.close()//关闭文件
返回0;
}
void readFile(ifstream&somefile)//从文件中读取数据。。
{
双num1、num2、num3、num4、num5;
而(somefile>>num1>>num2>>num3>>num4>>num5)
coutNo

#包括
#include//使用包含文件所需的头
使用名称空间std;
ifstream f(“date.txt”);//声明文件数据
int returnNumber();//您可以对函数进行原型化(我想您知道这一点)
int nr,a=10,b=10;//声明全局变量
int main()
{

看不见。你说的“澄清文件”是什么意思?到目前为止你的工作样本会很有帮助。你太棒了,非常感谢…,,现在开始写代码,看看会发生什么。。。
#include <iostream>
#include <fstream> // you include HEADER needed for files using

using namespace std;

ifstream f("date.txt"); // you DECLARE the FILE data

int returnNumber(); // you PROTOTYPE your function (I guess you know about this)
int nr, a = 10, b = 10; // you declare your GLOBAL variables



int main()
{
    cout << returnNumber(); // you SHOW the number you got after calculation on screen
    return 0;
}


int returnNumber(){ // here's the function; int type because you return a integer value
    f >> nr; // you READ the VALUE from the file
    nr = nr + a + b; // you make a CALCULATION inside function
    return nr; // you RETURN the number
}