为什么我的数组的一部分输出的值与我提取的值不同(c++)

  • 本文关键字:提取 c++ 输出 数组 一部分 c++
  • 更新时间 :
  • 英文 :


data11.txt:

Length(l) Time(t)   Period(T)
200   10.55     0.527
300   22.72     1.136
400   26.16     1.308
500   28.59     1.429
600   31.16     1.558

我将在我的代码中从上面的这个文件中获取数据

#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <iomanip>
/*Using data11.txt as reference, write code to read from the file. In a pendulum experiment certain data was recorded in order to calculate the acceleration due to gravity. All calculations should be in their SI units
Write a function to calculate the acceleration due to gravity as the slope of the graph to this equation.T=2πlg−−√.
What is the percentage error in your calculated value? Take g=9.8ms-2.
If the pendulum weighs 50g and is displaced at an angle of 30, what will it maximum kinetic energy value for each length of the string? Display your answer in table form. */
using namespace std;
double slope_gravity(double T1, double T2, double L1, double L2){//slope function
double T1_inverse = 1/pow(T1,2);
double T2_inverse = 1/pow(T2,2);
double difference_T = T2_inverse - T1_inverse;
double difference_L = (L2 - L1)/100;
double slope = (difference_T/difference_L);
return slope;
}
const int n = 4;
int main (){
ifstream mydata;
string capture;
float Length[n], Time[n], Period[n], acc_gravity;//declaring variable

mydata.open("Data11.txt");
if (mydata.is_open()){
for (int i=0; i<n+1; i++){
getline(mydata, capture);
mydata >> Length[i] >> Time[i] >> Period[i];
}
acc_gravity = slope_gravity(Period[1], Period[0], Length[1], Length[0]);//acceleration due to gravity calc
cout << fixed << showpoint << setprecision(1);
cout << "The Acceleration Due To Gravity Obtained Experimentally "
<< "from the data11 file is "<<abs(acc_gravity)<<"ms^-2"<<endl;
cout << "Calculating for Percentage error.............";
cout <<endl<<endl;
float percent_error = (abs(acc_gravity)- 9.8) * 100/9.8;//error analysis
cout << "Percent Error is "<< setprecision(2) << abs(percent_error)<<"%"<<endl;
cout << endl;
cout << "Calculating Max Kinetic Energy of pendulum weighing 50g being "
<< "displaced at an angle of 30n";

cout << setprecision(4);
float velocity[n], x[n], y;//kinetic energy calc
double max_KE[n];

for (int j=0; j<n+1; j++){
x[j] = 2 * acc_gravity * Length[j]/100;
y = 1 - cos(30);
velocity[j] = sqrt(x[j] * y);

max_KE[j] = 0.5 * 50/1000 * pow(velocity[j],2);
}

cout<<endl<<endl;

cout << setprecision(1);
cout << "Length(m)tMaximum Kinetic Energy(J)n";//tabular form display
for (int k=0; k<n+1; k++){
cout << Length[k] <<"t"<< max_KE[k] <<endl;    
}

}
mydata.close();

return 0;
}

该计划的目标在上面的第一条评论中。

在程序的最后,我应该以表格的形式将长度和最大动能的值打印到控制台。然而,在数组Length[0]中,它打印31.16,但我从data11.txt中提取了数据,因此应该有值200。

问题的根源在于这个for循环。for循环通过从data11.txt中提取值而运行良好。因此Length[0]的值在这里是200。

我试着弄清楚它为什么会这样。问题从这里开始;

mydata.open("Data11.txt");
if (mydata.is_open()){
for (int i=0; i<n+1; i++){
getline(mydata, capture);
mydata >> Length[i] >> Time[i] >> Period[i];
}

但在forloop之外,Length[0]为31.16。我还注意到Time[4]的值与输入值不同,输入值应该是31.16。

请帮忙吗?

您注意到的是所谓的"缓冲区溢出"。

float Length[n], Time[n], Period[n], acc_gravity;
// since 'n' is 4, 
// so the valid range of subscript of Length/Time/Period are [0-3]
// That means, if you try to write to  Length[4], 
// that is invalid, often lead to ruin something else.

mydata.open("data11.txt");
if (!mydata.is_open())
{
fprintf(stderr,"failed to open datafile.n");
return 1;
}

for (int i=0; i<n+1; i++){  // here the rang of 'i' is [0 - 4]
getline(mydata, capture);
mydata >> Length[i] >> Time[i] >> Period[i]; // so, when i==4, 
// 'buffer overun' happens here. 
// 'Time' could be ruined by 'Period'.
}  

如果您想跳过输入文件的"头"行,一种可能的方法是:"'getline()'一次,然后输入循环"。

无论您选择什么,只要不要"缓冲区溢出">。祝你好运。:)

相关内容

  • 没有找到相关文章

最新更新