在txt文件中查找字符串并获取以下数字

时间:2015-12-09 04:18:49

标签: c++ string parsing c++11

我的代码有问题,它找到了我想要的字符串,但它返回整行。我希望它只返回我输入的字符串部分并返回以下数量。截至目前,它正在寻找“坦克7000.99”而不仅仅是“坦克”并且它没有返回以下数量的7000.99。信息位于文本文件中。

文字档案:

Tank 7000.99
Cart 900.99

问题区域

double getCost(string item) {      //string item = "Tank"
    string str;
    double cost;
    while (getline(file, item)) 
    {
        if (std::size_t pos = str.find(item))
        {
            cout << "Found " << item << endl;
            file >> cost;                      //file is the txt file
        }
        else
        {
            cout << "Item not found in file" << endl;
            cost = 0;
        }
    }
    return cost;
}

1 个答案:

答案 0 :(得分:3)

  1. 请勿使用<?php if ( !isset ( $num_tries ) ) { $num_to_guess = rand(1, 100); } $num_tries = ( isset ( $_POST['num_tries'] ) ) ? $num_tries + 1 : 0; if ( !isset ( $_POST['guess'] ) ) { $message = "Welcome to the guessing machine!"; } elseif ( !is_numeric ( $_POST['guess'] ) ) { $message = "I don't understand that response..."; } elseif ( $_POST['guess'] == $num_to_guess ) { $message = "Well done! That is the number!"; } elseif ( $_POST['guess'] < $num_to_guess ) { $message = $_POST['guess'] . " is too small!"; } elseif ( $_POST['guess'] > $num_to_guess ) { $message = $_POST['guess'] . " is too big!"; } else { $message = "I am terribly confused..."; } ?> <!DOCTYPE html> <html> <head> <title>A PHP Number Guessing Script</title> </head> <body> <h2>The Guessing Game</h2> <p>1-100<br/> Guess number: <?php echo $num_tries ?></p> <h3><?php echo $message; echo $num_to_guess?></h3> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <p><label for="guess">Type your guess here:</label><br/> <input type="text" id="guess" name="guess" /></p> <input type="hidden" name="num_tries" value="<?php echo $num_tries; ?>"/> <button type="submit" name="submit" value="submit">Submit</button> </form> </body> </html> 。只需使用getline即可阅读该项目。 operator>>读取整行。 getline只会读取令牌。
  2. 请勿阅读operator>>。阅读item。然后,你可以比较两者。
  3. str

    建议改进

    我假设您要在该计划中多次致电double getCost(string item) { //string item = "Tank" string str; double cost; while (file >> str) { if ( str == item ) { cout << "Found " << item << endl; file >> cost; //file is the txt file return cost; } } cout << "Item not found in file" << endl; return 0; } 。如果这个假设是正确的,那么当前的策略将是低效的。

    我建议创建几个函数。在第一个函数中,读取所有数据并将它们收集到地图中。在第二个函数中,获取与地图相对应的成本作为输入。

    getCost

    并将它们用作:

    void readData(std::string& file,
                  std::map<std::string, double>& data)
    {
       data.clear();
       std::ifstream file(filename);
       if (!file)
       {
          return;
       }
    
       std::string line;
       while ( getline(file, line) )
       {
          std::istringstream istr(line);
          std::string item;
          double cost;
          if ( istr >> item >> cost )
          {
             data[item] = cost;
          }
          else
          {
             std::cerr << "Unable to extract the data from line: " << line << std::endl;
          }
       }
    }
    
    double getCost(string item, // item = "Tank"
                   std::map<std::string, double>& data)
    {
       auto iter = data.find(item);
       if ( iter == data.end() )
       {
          cout << "Item not found in file" << endl;
          return 0;
       }
    
       return iter->second;
    }