2016-05-11 4 views
-1

私は4つの休暇先と価格が書かれているので、ファイルを読み込んで宛先配列に書き込む価格配列に価格。テキスト形式は、これは私が私はファイルを読み込んで宛先配列に宛先を書き込み、価格配列に値段をつけたい

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

//define a constant for the number of lines to read 
#define NUM_READ_LINES 4 
int main() 
{ 

    // array of line numbers each line being less or equal to 100 chars 

    char destination[NUM_READ_LINES][100]; 
    //string price[30]; 
    int counter = 0; 
    //ofstream outfile; 
    ofstream outfile("program.txt"); 
    if(outfile.is_open()) 
    { 
     outfile <<"suncity 250\n "; 
     outfile <<" ushakamarue 300\n"; 
     outfile <<" krugerPark 450\n"; 
     outfile <<" Tablemountain 340\n"; 
    } 

    else 
     cout <<"Unable to open to file"; 

    outfile.close(); 

    //open a file 
     ifstream infile; 
     infile.open("program.txt"); 


     if(infile.good()) 
     { 
     //Read throuh file and load into array 
    while(!infile.eof() && (counter < NUM_READ_LINES)) 
    { 
     infile.getline(destination[counter], 100); 
     counter++; 
    } 
    //loop hrough the array which we just put together 
    for (int i=0; i < counter;i++) 
     { 
      cout << destination[i]<<endl; 
     } 
    } 
    infile.close(); 


    return 0; 
} 

現在、このプログラムは、宛先として、全ラインサンシティ250を読み込む代わりに、先にのみサンシティを読んでいる[1]と250価格に持っているものである

suncity 250 
ushakamarue 300 
krugerPark 450 
Tablemountain 340 

のようなものです[1]。このような

+2

あなたは疑問を持っていますか? –

+0

現在、プログラムは、宛先[1]と250を価格[1]に読み替える代わりに、行全体の行先250を宛先として読み取っています。助けてください。 –

答えて

0

何か:

 string destination[NUM_READ_LINES]; 
    string price[NUM_READ_LINES]; 

    ifstream infile("program.txt"); 
    for (int ii = 0; ii < NUM_READ_LINES; ++ii) { 
     infile >> destination[ii] >> price[ii]; 
    } 
+0

ありがとう、それは働いた。 –

関連する問題