簡體   English   中英

使用關鍵字從文本文件返回數字

[英]Return a number from text file using a keyword

我有兩個文本文件。 這兩個文件的內容如下所示:

文件1的內容:蘋果5芒果10橙15

文件2的內容:蘋果10芒果15橙20

我正在嘗試制作一個使用關鍵字(這里是水果的名稱)並隨機選擇文件之一並返回與該關鍵字對應的數值的程序。 下面是我的代碼。 但是,當我運行該程序時,它僅顯示第一個值,而不顯示相應的值。 我究竟做錯了什么?

    double Fruit::Price(string & sym)
    {
        ifstream inResultFile;
        string file_selected;
        int choice;
        string line;

        /*choice = (rand()%2);

        switch (choice)
        {
        case 0:
            file_selected = "file 1.txt";
            break;

        case 1:
            file_selected = "file 2.txt";
            break;
        }*/

        inResultFile.open("file 1.txt", ios::in);
        if (inResultFile.is_open())
        {
            double value=-1;
string name;

            while (inResultFile >> name >> value)
            {
cout<<name<<value;
if(name==sym)
                return value;
            }

        }
        else
            cout << "Sorry, the file could not be openend." << endl;
return -1;
    }

    int main()
    {
        Fruit Obj;
        string symbol;
        double f_Price;

        cout << "Enter a keyword to get the fruit price" << endl << endl;
        cin >> symbol;

        f_Price = Obj.Price(symbol);
        cout << "The selected price of the input symbol is " << f_Price << endl;
        return 0;
    }

1)您在以下幾行中破壞了sym (請求的水果)的值:

    while (inResultFile >> sym >> value)
    {
        return value;
    }

注意:您必須順序讀取文件,直到達到要求的值,然后才能將其返回。

2)您永遠不會檢查從文件中獲取的值是否是請求的結果,只需返回第一次嘗試即可(也必須在上面的行中發生!)

要獲得正確的值,您必須對水果進行如下比較:

  string fruit = null;
  while(inResultFile >> fruit >> value)
  {
     if( fruit == sym)
         return value;
  }

在方法結尾處,使用以下行

  else
    cout << "Sorry, the file could not be openend." << endl;
  return 0;//no fruit found 

在main中,只需檢查返回值是否為0,這意味着您選擇的水果在文件中不可用。

我只是使用您下面的代碼為我工作。 只需檢查您的txt輸入文件即可。 必須是數據錯誤

class Fruit
{
   public:
     double Price(string & sym);
};

double Fruit::Price(string & sym)
{
    ifstream inResultFile;
    string file_selected;
    int choice;
    string line;

    /*choice = (rand()%2);

    switch (choice)
    {
    case 0:
        file_selected = "file 1.txt";
        break;

    case 1:
        file_selected = "file 2.txt";
        break;
    }*/

    inResultFile.open("file1.txt", ios::in);
    if (inResultFile.is_open())
    {
        double value=-1;
        string name;
        while (inResultFile >> name >> value)
        {
            cout<<name<<value<<endl;
             if(name==sym)
              return value;
        }

    }
    else
        cout << "Sorry, the file could not be openend." << endl;
        return -1;
}

int main()
{
    Fruit Obj;
    string symbol;
    double f_Price;

    cout << "Enter a keyword to get the fruit price" << endl << endl;
    cin >> symbol;

    f_Price = Obj.Price(symbol);
    cout << "The selected price of the input symbol is " << f_Price << endl;
    return 0;
}

而我的輸出

芒果
蘋果5
芒果10
輸入品種的選擇價格為10

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM