C++(使用文件数据通过函数进行计算)
C++ ( using file data to make calculation through functions)
在一个
函数中读取文件后如何使用该文件,并在另一个函数中进行计算?
这是我尝试制作的代码..
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;
}
No.
#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
}
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 函数计算用户按下按钮的频率
- C++ 在编译时具有函数计算全局变量
- C++ - 使用特定函数计算 x 和 y 的最终值
- 尝试使用函数计算有剩余球员的球队数量
- 我正在尝试创建一个布尔函数,该函数计算字符并在字符为"()*-+"时返回 true
- 哈希函数计算
- 为什么 if 语句对于运算符重载函数计算 false
- 使用C 中的内在函数计算相交
- 如何使用 2 个迭代器进行求和函数计算映射<字符串、双精度>
- vector.size() 函数计算量大吗?
- 如何使用opencv函数计算图像的孔数(二进制)
- 斐波那契函数计算不正确
- 如何用3个用户定义的函数计算一个数字的真平方根
- 元函数计算 x^n 并返回整数限制而不溢出(如果不可能)
- 使用递归函数计算字符串中的元音
- 如何使用 pow() 函数计算 C++ 中超过 2^32 的幂
- 返回 void 的函数的无序函数计算
- 在 C++ 中使用尾递归函数计算列表的总和
- 使用结构和函数计算线的长度