我敢肯定,這是一個常見問題,但我找不到類似於我的例子,所以.. 我有一個名爲input.txt的文本文件,它具有: 0.00.0005434 0.0005678 1.0023423 0.00063452 1.0001546 0.00074321 1.00017654其中。 現在我想編寫一個程序將其讀入數組,然後快速檢查它是否工作。到目前爲止,我得到了:輸入數字從txt文件到數組
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double PA[8];
int n;
ifstream myfile ("input.txt");
if (myfile.is_open())
{
while (! myfile.eof())
{
getline (myfile,line);
cout << PA[line]<< endl;
}
myfile.close();
}
else cout << "Unable to open file";
for (n=0; n<8; n++) // to print the array, to check my work
{
cout << " {" << PA[n] << "} ";
}
system("PAUSE");
return 0;
}
我到目前爲止的問題是,我不斷收到錯誤:行沒有聲明。我想在稍後使用浮點數來計算新數據。我認爲我做錯了..任何幫助?謝謝!
你是什麼意思通過加載數據? – Emil
從輸入文件加載數據到雙精度數組代碼塊之間while(! myfile.eof()){...}) – ziollek
謝謝!這主要工作,除了它似乎只做第一個數字,我無法得到循環工作,似乎我只是乘以錯誤:P – Emil