2017-05-09 38 views
0

確定我有一個看起來像這樣我怎麼兩個字符串和整數匹配到一個文件

塔科馬塔科馬0
塔科馬西雅圖15
塔科馬斯波坎24

我把它宣佈一個txt文件這樣

INFILE >> city1 >>城2 >> DIST

我的問題是什麼,我需要做的有我的節目我的兩個CIN位置匹配到txt文件到GE t第三列的正確整數。作爲一個例子,我的第一個cin位置是塔科馬,我的第二個cin位置是斯波坎,我的結果應該是24,當我編碼outFile < < dist,但我一直得到0。

有沒有這樣的例子。我不需要有人爲我編寫代碼,但是如何讓這個工作起作用的例子將幫助我理解我一直在做錯的事情。

int client() 
{ 
string city1; 
string city2; 
int distance; 
bool city1found = false; 
bool city2found = false; 
ifstream inClient; 
ifstream inDist; 
ifstream inroute; 
ofstream outClient; 
ofstream outDist; 
inClient.open("client.txt", ios::app); 
inDist.open("post.txt", ios::app); 
outClient.open("client.txt", ios::app); 
outDist.open("post.txt", ios::app); 
inroute.open("routes.txt"); 

inroute >> city1; 
inroute >> city2; 
inroute >> distance; 

cout << "Business Name (First & Last Name): "; 
cin >> cFirst >> cLast; 
outClient << cFirst << " " << cLast; 
outDist << cFirst << " " << cLast; 

cout << "Origin of delivery: "; 
cin >> clocation; 
    if (clocation == city1) //if the current city is equal to the inputted from city 
    { 
     city1found = true; 
    } 
    else if (city1found == false) 
    { 
     cout << endl << "Origin city not found." << endl; 
     cin >> clocation; 
    } 

outClient << "\t\t" << clocation; 
outDist << setw(15) << clocation; 

cout << "Seven digit phone number (EX. 671-689-5134): "; 
cin >> cphone; 
outClient << "\t\t" << cphone; 

cout << "Email address: "; 
cin >> cemail; 
outClient << "\t\t" << cemail; 

cout << "Destination of delivery: "; 
cin >> cdestination; 
    if (cdestination == city2) //if the current city is equal to the inputted from city 
    { 
     city2found = true; 
    } 
    else if (city2found == false) 
    { 
     cout << endl << "Detination not found." << endl; 
     cin >> cdestination; 
    } 
outClient << "\t\t" << cdestination; 
outDist << setw(15) << cdestination; 

cout << "Weight of Shipment(Tons): "; 
cin >> weight; 
outClient << "\t\t" << weight; 
outDist << setw(15) << weight; 

//Distance established 
if (inroute.is_open()) 
{ 
    while (!inroute.eof()) 
    { 
     if (clocation && cdestination == city1 && city2) 
     { 
      distance; 
     } 

    } 
} 
outClient << "\t\t" << distance; 
outDist << setw(15) << distance; 
outClient << endl; 
outDist << endl; 

cout << "Order confirmed" << endl; 

cout << "Enter '0' to return to the Main Menu or '1' to exit out of program" << endl; 
cin >> ans; 
if (ans == 0) 
{ 
    system("CLS"); 
    main(); 
} 
else (ans == 1); 
{ 
    exit(0); 
} 

}

+0

歡迎來到StackOverflow。我們更喜歡你向我們展示你的代碼和一個帶有如[這裏](http://stackoverflow.com/help/mcve)所述的期望輸出的例子,因爲我們更容易理解和幫助你。即使你不希望我們爲你寫一個解決方案。 – Badda

回答

-1

我會使用功能getline()它允許在一個字符串來存儲文件裏逐行。然後,您會將每行與「cin location」進行比較,如果匹配,則輸出該行的最後一個單詞。
告訴我,如果你想要一個示例代碼來說明這一點。

編輯

後你顯示你的代碼,我有一些建議豆蔻給你。首先,你只需要在你需要它們之前聲明和初始化變量。對每個人,甚至是你自己來說,都會更容易理解。至於流,您只能在讀取或寫入文件之前打開它們,然後關閉它們。它也將避免一些錯誤。

此外,檢查這樣一個布爾值:

if (city2found == false) 

是同樣的事情,做這樣的:

if (!city2found) // Which really means (city2found != true) 

這是題外話,但我建議這樣做,因爲這是主要爲什麼布爾值使用起來非常酷。

怎麼用流和函數getline():

ifstream file; 
file.open("file.txt"); 
if (file.is_open()) 
{ 
    string line; 
    while (getline(inFile, line)) // This will iterate through every line of the file until the end 
    { 
     if (line == "Tacoma Tacoma 0") // If you could write a function to extract only the destination from the line, it'd be easier 
      return 0; 
     if (line == "Tacoma Seattle 15") 
      return 15; 
    } 
} 
file.close(); 

我還沒有檢查這個閹工作確實與否的時間,但它不應該被使用的很遠。如果您需要更多關於getline的信息,您可以檢查鏈接。

+0

這是我不斷收到錯誤的地方。我想我不清楚如何正確使用getline()。我在我以前的文章中附加了我的代碼的一部分 –

+0

@DanielPhillip我編輯了我的答案,希望這能幫助你。 另外,我不知道爲什麼這個投票和誰投了票,但我很樂意知道如何改進我的答案。 – Badda

+0

有沒有一種方法可以使用'getline()'來匹配inFile和'cin'中的行作爲結果來拉第三列的整數?因爲你提供的代碼有效,但只適用於小列表,所以效率很高,但如果我必須這樣做,我可以列出幾乎100行的列表。 –

相關問題