如何将两个字符串和一个整数与文件匹配

时间:2017-05-09 13:01:52

标签: c++ visual-c++

好的我有一个如此显示的txt文件

Tacoma Tacoma 0
塔科马西雅图15
Tacoma Spokane 24

我已将其宣布为

inFile>> city1>> city2>> DIST

我的问题是我需要做什么才能让我的程序将我的两个cin位置与txt文件匹配,以便在第三列上获得正确的整数。作为一个例子,我的第一个cin位置是Tacoma,我的第二个cin位置是Spokane,当我在outFile<<中编码时,我的结果应该是24。但是我的结果仍然是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);
}

}

1 个答案:

答案 0 :(得分:0)

我会使用函数getline(),它允许逐行存储一个文件。然后,您将每行与您的&#34; cin位置进行比较&#34;如果匹配,则输出该行的最后一个单词 如果你想要一个示例代码来说明这一点,请告诉我。

编辑

在您展示代码后,我会给您一些小小的建议。首先,您应该在需要之前声明和初始化变量。它会让每个人都更容易理解,甚至是你自己。对于流,您应该只在读取或写入文件之前打开它们,然后关闭它们。它还可以避免一些错误。

另外,检查一个类似的布尔值:

if (city2found == false)

就像那样做:

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

这是偏离主题的,但我建议这样做,因为这主要是为什么布尔值太酷了。

如何使用stream和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的更多信息,可以查看this链接。