C ++:使用stringstream从字符串中提取所需的整数后,获取两个字符串定界符和值之间的数字

问题描述 投票:0回答:1
    ```
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <fstream>

    using namespace std;


    bool findCurrentNumber (int currentNumber, int foundNumber) 
    {
        if (currentNumber == foundNumber) 
        {
            return true;
        }
        return false;
    }


    int main()
    {

        ifstream inputFile;
        inputFile.open("C:\\Projects\\data.txt");
        if (!inputFile) 
        {
            cerr << "Error reading file "; 
        }
        else 
        {
        int searchCurrentNumber;
        cout << "Please type in the number you are looking for: ";
        cin >> searchCurrentNumber;

        bool foundRelevantSection = false;
        string delimiter = "energy";
        size_t pos = 0;
        string token;
        string line;


        while (getline(inputFile, line)) 
        {
             while ((pos = line.find(delimiter)) != string::npos) 
             {
                    token = line.substr(0, pos);
                    //check the found token
                    //cout << token << endl;
                    line.erase(0, pos + delimiter.length());
                    stringstream ss;     
                    //store the whole string into stringstream
                    ss << token; 
                    string temp; 
                    int found; 
                    while (!ss.eof()) 
                    { 
                    //extract the word by word from stream
                    ss >> temp; 
                    //Check the given word is integer or not
                    if (stringstream(temp) >> found) 
                    cout << "Number found: " << found << " " << endl;; 
                    //no space at the end of string
                    temp = ""; 
                    }
                    if (findCurrentNumber (searchCurrentNumber, found) == true)
                    {
                        while (getline (inputFile, line)) 
                        {
                        if (foundRelevantSection) 
                            {
                                //if no matches were found, the function returns "string::npos"
                                if(line.find("total") != string::npos) 
                                {
                                    //relevant section ends now
                                    foundRelevantSection = false;   
                                }
                                else    
                                {
                                cout << line << endl;
                                }
                            }
                            else 
                            {
                                if (line.find("point") != string::npos )
                                {
                                    foundRelevantSection = true;
                                }
                            }

                        }
                    }               
                    else 
                    {
                    cout << "The number is not equal on this line compared to what is typed in!" << endl;
                    }
              }                     
        } //closes the while-loop
    } //closes the if/else-statement

    inputFile.close();

        return 0;
    }
```

大家好,

我想解析具有以下格式的输入文件:

point     152 # energy  # 0.5
152 152 152 152 152 152
152 152 152 152 152 152
total  0.011  0.049  0.035 

point     153 # energy  # 1.5
153 153 153 153 153 153
153 153 153 153 153 153
total  0.015  0.050  0.040

该代码接受用户提供的整数,并将其与从例如字符串“点152#能量”。如果用户输入数字“ 152”,则代码应提供以下数字:

output: 
152 152 152 152 152 152
152 152 152 152 152 152

[不幸的是,如果输入数字152或输入数字153,则我的代码返回的结果完全相反。

有人可以帮助我,告诉我我做错了什么吗?感谢您提供任何提示!

提前感谢!

最诚挚的祝福,

DaveS

getline stringstream
1个回答
0
投票

修正了第二个错误的结尾。

您应该努力使调试器变得更好,我发现了其中一个问题:

while (!ss.eof()) 
    { 
    //extract the word by word from stream
    ss >> temp; 
    //Check the given word is integer or not
    if (stringstream(temp) >> found) 
         cout << "Number found: " << found << " " << endl;; 
    //no space at the end of string
    temp = ""; 
    }

不会在“点152#”中找到152,而是继续处理#,将其发现为0。

此代码带有中断以修复该部分:

while (!ss.eof()) 
    { 
    //extract the word by word from stream
    ss >> temp; 
    //Check the given word is integer or not
    if (stringstream(temp) >> found) 
         {
         cout << "Number found: " << found << " " << endl;
         foundRelevantSection = true;
         break; /* exits the while now */
         }
    //no space at the end of string
    temp = ""; 
    }

或者您可以先将其设置为0,然后使用&& found == 0进行测试,以测试一段时间内发现的内容>

然后,调用findCurrentNumber (int currentNumber, int foundNumber)的部分是垃圾(或占位符,代表更复杂的方式?),因为if (findCurrentNumber (searchCurrentNumber, found) == true)只是if (searchCurrentNumber == found),这更容易阅读!

我没有检查代码后面是否还有更多错误,但是通过中断,您肯定可以找到正确的值。

第2部分

已经

找到了“点”,因此您不必再寻找它了!我在中断之前在上面的代码中添加了foundRelevantSection。将下一部分更改为(如果不相关,则找到点):
while (getline (inputFile, line) && foundRelevantSection) 
    {
    //if no matches were found, the function returns "string::npos"
    if(line.find("total") != string::npos) 
        {
        //relevant section ends now
        foundRelevantSection = false;   
        }
    else    
        {
        cout << line << endl;
        }
    }

希望这是最后一个错误...

© www.soinside.com 2019 - 2024. All rights reserved.