2017-04-07 144 views
0

我已經寫了下面的代碼來讀取文件(area.inp)中列中的數據到數組。當在屏幕上顯示數據時,第一個「for」循環顯示正確的數字(所以代碼正確讀取文件中的數字),但第二個「for」循環顯示不正確的數字組。我無法解決問題。我將不勝感激關於這個問題的任何指導。將文件中的數據存儲到數組中

area.inp

001.000 003.000 
002.000 004.000 
006.000 005.000 
004.000 002.000 
002.000 001.000 

代碼

#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <algorithm> 
#include <iterator> 


using namespace std; 


int main(int argc, char **argv) 
{ 
    int j=0; 
    double X[j]; 
    double Y[j]; 
    static double *p; 
    double *q; 
    p=X; 
    q=Y; 


//**** Counting the number of lines 

    ifstream myfile("area.inp"); 
    myfile.unsetf(ios_base::skipws); 

    int points = count(
     istream_iterator<char>(myfile), 
     istream_iterator<char>(), 
     '\n'); 

    cout << "Number of data in file: " << points << "\n"; 

//**** Open data file and result 
    cout <<"file is open"<< endl; 
    ifstream infile; 
    infile.open("area.inp", ios::in); 


    cout <<"Reading data from file"<< endl; 
    for (j=0; j<points; j++) 
     { 
      cout << std::setprecision(3) << std::fixed;; 

      infile >> X[j] >> Y[j]; 

      cout << "Value of X["<<j<<"]: " << X[j] << endl; 

      cout << "Value of Y["<<j<<"]: " << Y[j] << endl; 
     } 

    cout <<"Showing numbers stored in array"<< endl; 


      for (j=0; j<points; j++) 
      { 

       cout << "Value of X["<<j<<"]: " << X[j] << endl; 

       cout << "Value of Y["<<j<<"]: " << Y[j] << endl; 
      } 

    infile.close(); 

    return 0; 
} 

Number of data in file: 5 
file is open 
Reading data from file 
Value of X[0]: 1.000 
Value of Y[0]: 3.000 
Value of X[1]: 2.000 
Value of Y[1]: 4.000 
Value of X[2]: 6.000 
Value of Y[2]: 5.000 
Value of X[3]: 4.000 
Value of Y[3]: 2.000 
Value of X[4]: 2.000 
Value of Y[4]: 2.000 
Showing numbers stored in array 
Value of X[0]: 5.000 
Value of Y[0]: 3.000 
Value of X[1]: 2.000 
Value of Y[1]: 4.000 
Value of X[2]: 4.000 
Value of Y[2]: 5.000 
Value of X[3]: 4.000 
Value of Y[3]: 2.000 
Value of X[4]: 2.000 
Value of Y[4]: 2.000 


------------------ 
(program exited with code: 0) 
Press return to continue 
+1

看看'INT J = 0;雙X [j];'。 X可以存儲多少個元素? – NathanOliver

+1

除了'double X [j];'不是標準的C++時'j'不是一個常量表達式的事實。 – crashmstr

回答

0

作爲評價所提到的,要創建一個0長度的數組。要做到的事情你的方式,你必須跟你算分的文件數new後分配數組:

double *X = new double[points]; 
double *Y = new double[points]; 

,然後在程序結束時,釋放內存與delete

我會建議你避免陣列,而是使用std::vector。在這個例子中,我使用std::pair來存儲點數,但是你可以改變它。

// Using a vector of pairs 
std::vector<std::pair<double, double>> data; 

ifstream infile; 
infile.open("area.inp"); 
// You should check here to make sure the file opened ok 

// Read the file. No need to know size ahead of time 
std::pair<double, double> tmp; 
while (infile >> tmp.first >> tmp.second) { 
    data.push_back(tmp); 
} 

// Print the results 
std::cout << "Number of pairs: " << data.size() << std::endl; 
for (auto p : data) { 
    std::cout << p.first << ", " << p.second << std::endl; 
} 
0

You'r數組X []和Y []的大小爲零。 好奇,爲什麼沒有賽格故障

嘗試改變如下,

#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <algorithm> 
#include <iterator> 


using namespace std; 


int main(int argc, char **argv) 
{ 
int j=0; 
static double *p; 
double *q; 


//**** Counting the number of lines 

ifstream myfile("area.inp"); 
myfile.unsetf(ios_base::skipws); 

const int points = count(
    istream_iterator<char>(myfile), 
    istream_iterator<char>(), 
    '\n'); 

double X[points]; 
double Y[points]; 
p=X; 
q=Y; 

cout << "Number of data in file: " << points << "\n"; 

//**** Open data file and result 
cout <<"file is open"<< endl; 
ifstream infile; 
infile.open("area.inp", ios::in); 


cout <<"Reading data from file"<< endl; 
for (j=0; j<points; j++) 
    { 
     cout << std::setprecision(3) << std::fixed;; 

     infile >> X[j] >> Y[j]; 

     cout << "Value of X["<<j<<"]: " << X[j] << endl; 

     cout << "Value of Y["<<j<<"]: " << Y[j] << endl; 
    } 

cout <<"Showing numbers stored in array"<< endl; 


     for (j=0; j<points; j++) 
     { 

      cout << "Value of X["<<j<<"]: " << X[j] << endl; 

      cout << "Value of Y["<<j<<"]: " << Y[j] << endl; 
     } 

infile.close(); 

return 0; 
} 
相關問題