所以我在C ++中还很陌生,我一直在尝试制作几个程序,但是我在两个程序中都面临着相同的问题。程序需要打开data.txt文件,读取多少行数字,然后读取第一行数字,将这些数字用于几个公式,然后将其写入单独的文件中。到目前为止,我已经做到了,程序可以正确计算,但是问题是程序只采用第一行数字,然后重复使用它们(第二行不去)。请帮助我弄清楚如何使其正常工作。 这是我到目前为止编写的代码:
Data.txt contains:
2
2 5
3 7
--------------------------------------
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Const chars
const char FVD[] = "Data.txt";
const char FVR[] = "Result.txt";
//Function prototypes
void Input (const char FV[], double &a, double &b, double &n);
void Output (const char FV[], double &a, double &b, double &S, double &P);
double Area (double &a, double &b);
double Perimeter (double &a, double &b);
int main()
{
double a,b,S,P,n;
for (int i=0;i<=n;i++){
Input(FVD,a,b,n,S,P);
S=Area(a,b);
P=Perimeter(a,b);
Output(FVR,a,b,S,P);
}
return 0;
}
//Functions:
//Area:
double Area (double &a, double &b)
{
return a*b;
}
//Perimeter:
double Perimeter(double &a, double &b)
{
double P=(a*2)+(b*2);
return P;
}
// Input:
void Input (const char FV[], double &a, double &b, double &n)
{
ifstream D(FV);
D >> n;
D >> a >> b;
D.close();
}
// Output
void Output (const char FV[],double &a, double &b, double &S, double &P){
ofstream R(FV);
R << a << setw(4) << b << setw(4) << S << setw(4) << P;
R << endl;
R.close();
}
Result in Result.txt
2 5 10 14
编辑:与D.close()的结果相同;和R.close();循环外的功能:
int main()
{
double a,b,S,P,n;
ifstream D(FVD);
ofstream R(FVR);
for (int i=0;i<=n;i++){
Input(FVD,a,b,n);
S=Area(a,b);
P=Perimeter(a,b);
Output(FVR,a,b,S,P);
}
D.close();
R.close();
return 0;
}
输入和输出功能:
void Input (const char FV[], double &a, double &b, double &n)
{
ifstream D(FVD);
D >> n;
D >> a >> b;
}
void Output (const char FV[],double &a, double &b, double &S, double &P){
ofstream R(FVR);
R << a << setw(4) << b << setw(4) << S << setw(4) << P;
R << endl;
}
答案 0 :(得分:0)
您的问题在于输入功能。您每次都在打开和关闭文件,因此导致它一次又一次地读取同一行。您想做这样的事情:
int main()
{
double a,b,S,P,n;
ifstream D(FV);
for (int i=0;i<=n;i++){
Input(D,a,b,n,S,P);
// other stuff
}
D.close();
return 0;
}
您的输入功能如下:
void Input (const ifstream D, double &a, double &b, double &n)
{
D >> n;
D >> a >> b;
}
只是,您可能会在输出函数中遇到同样的问题。希望对您有帮助,祝您好运!
编辑在主循环中声明D,并将其作为参数传递给Input
第二次编辑,这是一个有效的示例(只需复制粘贴):
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Const chars
const char FVD[] = "Data.txt";
const char FVR[] = "Result.txt";
//Function prototypes
void Input (ifstream & D, double &a, double &b);
void Output (ofstream & R, double &a, double &b, double &S, double &P);
double Area (double &a, double &b);
double Perimeter (double &a, double &b);
int main()
{
double a,b,S,P;
ofstream R(FVR);
ifstream D(FVD);
// The value of n was a problem for two reasons
// One it was declared as a double when it was trying to get an int (2)
// Two it was getting read more than once
int n;
D >> n;
for (int i=0;i<=n;i++){
Input(D,a,b);
S=Area(a,b);
P=Perimeter(a,b);
Output(R,a,b,S,P);
}
D.close();
R.close();
return 0;
}
//Functions:
//Area:
double Area (double &a, double &b)
{
return a*b;
}
//Perimeter:
double Perimeter(double &a, double &b)
{
double P=(a*2)+(b*2);
return P;
}
// Input:
void Input (ifstream & D, double &a, double &b)
{
D >> a >> b;
}
// Output
void Output (ofstream & R,double &a, double &b, double &S, double &P){
R << a << setw(4) << b << setw(4) << S << setw(4) << P;
R << endl;
}