【发布时间】:2015-02-09 12:20:37
【问题描述】:
我正在尝试从 2 个不同的文本文件中提取双精度值并将它们放入数组中。这是代码的sn-p:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int p;
cout<<"Enter number of ordered pairs: ";
cin>>p;
cout<<endl;
double x[p];
ifstream myfile("x.txt");
while (myfile.good())
{
myfile>>x[p];
cout<<x[p]<<endl;
}
double testx = x[4]+x[3]+x[2]+x[1]+x[0];
cout<<endl<<"The sum of the values of x are: "<<testx<<endl<<endl;
double y[p];
ifstream myfile2("y.txt");
while (myfile2.good())
{
myfile2>>y[p];
cout<<y[p]<<endl;
}
double testy = y[4]+y[3]+y[2]+y[1]+y[0];
cout<<endl<<"The sum of the values of y are: "<<testy<<endl<<endl; system("PAUSE");
return EXIT_SUCCESS;
}
自从通过testx 和texty 进行检查后,我认为这些值没有被正确存储,这些值的总和不是预期的。
【问题讨论】:
-
您正在打印出这些值,因此您可以基于该值而不是总和来进行假设。什么是输入,输出预期输出(包括打印出来的值)。
-
您无法使用变量
p调整数组x的大小。大小需要在编译时知道,而不是运行时(除非某些编译器扩展)