2016-12-11 3 views
0

私のC++クラスの課題を終了しようとしています。私は授業が私のために完了するのを探していませんが、私は難局に達しています。私の教授は電子メールに返信するのが最善ではありません。彼女は多くのことが続いているので理解できる。2つの並列配列を使用するC++

入力ファイルからデータを読み取るプログラムを作成しています。データには、従業員の姓、勤務時間数、1時間当たりの賃金率が含まれます。私の問題は、この情報を2つの並列配列に配置する必要があることです。私は自分自身で課題を終えることができる方法を見つけ出すことができれば、文字通りGoogleで検索し、見つけることができたが役に立たないすべての動画を見ました。

Here is the input data: 
Smith  40 10.00 
Jackson 25 8.00 
Hill  35 10.00 
Withers 28 7.25 
Mills  32 7.55 
Myers  50 10.25 
Johnson 45 10.50 
Mcclure 38 9.50 
Miller 42 8.75 
Mullins 40 10.75 

更新:私のコードを編集し、私のプロジェクトの次のステップに進むために必要な結果に近づいています。私が持っているファイルからデータを読み込んで初期化する方法を編集しました。しかし、私の列の間には、奇妙な数列があります。

Smith 
-9.25596e+61 
-9.25596e+61 
-9.25596e+61 
40 
-9.25596e+61 
-9.25596e+61 
-9.25596e+61 
10.00 
-9.25596e+61 
-9.25596e+61 
-9.25596e+61 
Jackson 
-9.25596e+61 
    7.25 
-9.25596e+61 
25 
-9.25596e+61 
-9.25596e+61 
-9.25596e+61 
8.00 
-9.25596e+61 
-9.25596e+61 
-9.25596e+61 
Hill 
-9.25596e+61 
-9.25596e+61 
-9.25596e+61 
35 
-9.25596e+61 
-9.25596e+61 
-9.25596e+61 
10.00 
-9.25596e+61 
-9.25596e+61 
-9.25596e+61 

私の改訂コードは以下のとおりです。

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

const int NOFROWS = 10; 
const int NOFCOLS = 3; 

void readFile(ifstream& infile, string X[], double y[][NOFCOLS]); 
void print(ifstream&infile,ofstream& outfile, string x[], double y[] [NOFCOLS]); 

int main() 
{ 

//variables 

string names[20]; 
double wages[NOFROWS][NOFCOLS]; 
ifstream incode; 
ofstream outcode; 
incode.open("employeeinformation.txt"); 
outcode.open("results.txt"); 
if (!incode) 
{ 
    cout << "No data" << endl; 
    system("pause"); 
    return 1;   //if loop to terminate program if unable to open file 
} 
cout << fixed << showpoint << setprecision(2) << endl; 
readFile(incode, names, wages); //calls function readFile 
print(incode,outcode, names, wages); 

incode.close(); 
outcode.close(); 
system("pause"); 
return 0; 
} 


//Function to read file and input information into array. 
void readFile(ifstream& infile, string x[], double y[][NOFCOLS]) 
{ 
//local variables, used as counters for while loop. 
int r = 0; 
int c = 0; 

for (r = 0; r < NOFROWS; r++) 
    infile >> x[r]; // gets information from file for position 
infile >> y[r][c]; 
//r++; //counter to increase position value 
for (c = 0; c < NOFCOLS; c++) 
{ 
    infile >> y[c][r]; 
} 

}

void print(ifstream&infile, ofstream& outfile, string x[], double y[]   [NOFCOLS]) 
{ 
cout << setw(10) << "Names" << setw(10) << "hours" << setw(10) <<  "wages"; 
for (int r = 0; r < NOFROWS; r++) { 
    outfile << x[r] << " " << endl; 
    for (int c = 0; c < NOFCOLS; c++) 
    { 
     outfile << setw(10) << y[r][c] << " " << endl; 



     } 
    } 
} 

答えて

1

あなたが一度ファイルを開くようだが、二回その内容を読み取ろうとします。

readFile(incode, names); // reads incode until all data is read 
readFile2(incode, wages); // reads from the same incode, that has all data already expended 

closeopenのためにファイルを試してみてくださいまたは第二の呼び出しの前に初めに読み出しポインタを移動するか、いっそのRAIIファイル管理のためのコードをリファクタリング。

+0

私はそれを撃つでしょう。フィードバックいただきありがとうございます。 – Dustin

1

私は理想的には、一度に1行を読み込んで、名前、時間、賃金を入力するという単一の機能を持つことが理想的です。 (readFile2()続いreadFile())あなたの現在の設定では

while (inFile) { 
    inFile >> names[i] >> hours[i] >> wages[i]; 
    i++; 
} 

は、あなたのreadFile()機能は、ファイルの最後に読んでいます。 readFile2()を呼び出す前に、ファイルの先頭に戻って検索する必要があります。そうしないと、ファイルの最後から読み込みを試みます。

inFile.clear(); 
inFile.seekg(0, ios::beg); 

譲渡の際には十分です。

+0

フィードバックいただきありがとうございます。私は単一の(infile)を使用してみましたが、問題は、名前と時間と賃金の2つの配列しか使用できないということです。 – Dustin

関連する問題