繁体   English   中英

将 Function 返回值存储到变量中

[英]Store Function Return Value into a Variable

我有这些代码来查找输入的大小,并从输入中提取 integer:

void getInteger(string s) {

    stringstream str_strm;
    str_strm << s;
    string temp_str;
    int temp_int;
    while (!str_strm.eof()) {
        str_strm >> temp_str;
        if (stringstream(temp_str) >> temp_int) {
            cout << temp_int << "";
        }
        temp_str = "";
    }

}


int main()
{
    string myString;
    int totalChar;
    getline(cin, myString);
    int stringLength = myString.length();
    cout << stringLength << endl;;
    getInteger(myString);

}

如何将getInteger值存储到变量中,以便将其值与 integer 进行比较/求和? 例如,输入是:

我爱6

所以代码应该计算输入的总字符数和 integer:

8 + 6 = 14

If you have a function that will extract an integer and include that integer in the output of a sum of the .length() and the integer, form all of your output in the function itself rather than complicating things and losing control of the initial input通过在main()中进行部分输入并在 function 中进行部分输入。

以这种方式处理事物,您可以在单个 function 中完全控制 output。 例如,在您的情况下,如果没有 integer 发现您将如何“取消输出”长度?

将这些更改到位,您的 function 可能类似于:

void getint (std::string s)
{
    int val,                        /* var to hold int value */
        found = 0;                  /* flag indicating if int was found */
    size_t len = s.length();        /* get length of s */
    std::string tmp;                /* temporary string to read into from ss */
    std::stringstream ss (s);       /* create stringstream from string */

    while (ss >> tmp) {             /* read from ss */
        try {                       /* stoi must have exception handler */
            val = stoi (tmp);       /* attempt conversion */
            found = 1;              /* on success set found flag */
            break;                  /* break loop */
        }
        catch (const std::exception & e) {  /* catch exception get next word */
            continue;
        }
    }
    if (found)  /* if integer found, output your sum */
        std::cout << len << " + " << val << " = " << len + val << '\n';
    else        /* otherwise handle error */
        std::cerr << "error: no integer value in input.\n";
}

注意:如果没有发生转换,唯一需要的错误处理是获取下一个单词。任何 output 只会在输入和所需输出之间添加行)

利用 function 的示例程序可以是:

#include <iostream>
#include <sstream>
#include <string>

void getint (std::string s)
{
    int val,                        /* var to hold int value */
        found = 0;                  /* flag indicating if int was found */
    size_t len = s.length();        /* get length of s */
    std::string tmp;                /* temporary string to read into from ss */
    std::stringstream ss (s);       /* create stringstream from string */

    while (ss >> tmp) {             /* read from ss */
        try {                       /* stoi must have exception handler */
            val = stoi (tmp);       /* attempt conversion */
            found = 1;              /* on success set found flag */
            break;                  /* break loop */
        }
        catch (const std::exception & e) {  /* catch exception get next word */
            continue;
        }
    }
    if (found)  /* if integer found, output your sum */
        std::cout << len << " + " << val << " = " << len + val << '\n';
    else        /* otherwise handle error */
        std::cerr << "error: no integer value in input.\n";
}

int main (void) {

    std::string s;

    if (getline (std::cin, s))
        getint (s);
    else
        std::cerr << "stream error or user canceled.\n";
}

示例使用/输出

$ ./bin/lengthaddition
I love 6
8 + 6 = 14

输入中没有 integer

$ ./bin/lengthaddition
I love B
error: no integer value in input.

如果您还有其他问题,请仔细查看并告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM