2017-04-07 8 views
-2

現在、私のクラスのプロジェクトで作業中です。ポインタやベクターを使用することはできません(残念ながら) 、私は配列の出力を得ることができません。 私は、81行2列のファイル (列1はX値、列2はY値を保持しています)を含んでいます。最初の行は列タイトルなので無視されます。 データをパラレルな1次元配列(X値用とY値用)に読み込む関数を作成する必要があります。私は正常に動作する関数を持っていて、ファイルからデータを読み込む同じwhileループで配列を出力すると、すべてがうまくいきます。しかし、私が戻ってメインでそれらを出力しようとすると、ちょっとナンセンスになるだけです。ここ は、これまでに私のコードです:Void関数は配列をmainに送り返しますが、値を正しく出力するための配列を取得できません

#include<iostream> 
    #include<fstream> 
    #include<iomanip> 
    using namespace std; 

    //Function Prototype for readFile 
    void readFile(double oneDforX[], double oneDforY[]); 

    //Declare named constant for max number of rows 
    const int MAX_ROWS = 100; 

    int main() 
    { 
     //Declare two 1D array to hold data for X and Y 
     double oneD_ForXValues[MAX_ROWS]; 
     double oneD_ForYValues[MAX_ROWS]; 

     //Call function readFile to fill arrays 
     readFile(oneD_ForXValues, oneD_ForYValues); 

     /* 
      This is where I'm having the problem, when the arrays are 
      sent back to main I can't get the data to output correctly. 

      I tried this for the X array: 

      for (int i = 0; i < 80; i++) 
      { 
       cout << oneD_ForXValues[i] << end; 
      } 
       **This did not work, my output was something like this: 
       3.5 //The last number in the array 
       0 
       0  //Then a bunch of zeros all the way to the end 
       0 

       Any and all help is greatly appreciated! Thanks! 
     */ 

     return 0; 
    } 

    //Function Header for readFile function 
    void readFile(double oneDforX[], double oneDforY[]) 
    { 
     //Declare file stream object and open file 
     ifstream dataIn; 
     dataIn.open("dataFile.txt"); 

     //Loop counter 
     int count = 0; 

     //If opening file does not fail, execute 
     if (!dataIn.fail()) 
     { 
      //Ignore first line, column titles 
      dataIn.ignore(80, '\n'); 

      //While loop to read in data 
      while (!dataIn.fail()) 
      { 
       dataIn >> oneDforX[count] >> oneDforY[count]; 
      } 
     } 
     //If the file failed to open 
     else 
     { 
      cout << "An error occurred opening the file." << endl; 
     } 

     //Close the file 
     dataIn.close(); 
    } 
    //Back to main 

答えて

0

あなただけ何度もあなたの配列の最初の要素に書き込むようにするには、readFilecountをインクリメントすることはありません。

+0

ありがとうございました!私はその笑いを逃したとは信じられません。 –

関連する問題