Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;可能同时读取int和字符串 这里的C++相当新颖,但对编程来说并不新鲜。我想知道是否有任何简单的方法可以获得用户输入,比如“20公斤”,“20”是用户输入的任何东西,然后kg/lb/etc是用户输入的东西。 问题是,我需要在计算中使用输入的整数部分。 我想必须要做的是将其作为字符串读入,然后将int和String分离成单独的变量。(我必须在方程式中使用数字和测量类型) 任何帮助都会很好_C++_String_Int - Fatal编程技术网

C++;可能同时读取int和字符串 这里的C++相当新颖,但对编程来说并不新鲜。我想知道是否有任何简单的方法可以获得用户输入,比如“20公斤”,“20”是用户输入的任何东西,然后kg/lb/etc是用户输入的东西。 问题是,我需要在计算中使用输入的整数部分。 我想必须要做的是将其作为字符串读入,然后将int和String分离成单独的变量。(我必须在方程式中使用数字和测量类型) 任何帮助都会很好

C++;可能同时读取int和字符串 这里的C++相当新颖,但对编程来说并不新鲜。我想知道是否有任何简单的方法可以获得用户输入,比如“20公斤”,“20”是用户输入的任何东西,然后kg/lb/etc是用户输入的东西。 问题是,我需要在计算中使用输入的整数部分。 我想必须要做的是将其作为字符串读入,然后将int和String分离成单独的变量。(我必须在方程式中使用数字和测量类型) 任何帮助都会很好,c++,string,int,C++,String,Int,我不是在寻找任何代码块,我只是想了解我应该做什么,以及我可能需要使用的任何关键代码片段。 提前谢谢你 首先,您必须确保输入在整数部分和公制部分之间有一个空格。那你应该 把它分成两部分,然后 将第一部分转换为整数 如果您不想自己做这样乏味的工作,可以使用ssstream。下面是一个简短的示例 #include<string.h> #include<iostream> #include<sstream> using namespace std; int mai

我不是在寻找任何代码块,我只是想了解我应该做什么,以及我可能需要使用的任何关键代码片段。
提前谢谢你

首先,您必须确保输入在整数部分和公制部分之间有一个空格。那你应该

  • 把它分成两部分,然后

  • 将第一部分转换为整数

  • 如果您不想自己做这样乏味的工作,可以使用
    ssstream
    。下面是一个简短的示例

    #include<string.h>
    #include<iostream>
    #include<sstream>
    
    using namespace std;
    
    int main()
    {
        string input("20 kg");
    
        istringstream stream(input);
    
        int n;
        string metric;
    
        stream >> n;
        stream >> metric;
    
        //do something you want here
    
        cout<<n<<" "<<metric;
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    使用名称空间std;
    int main()
    {
    字符串输入(“20千克”);
    istringstream(输入);
    int n;
    字符串度量;
    流>>n;
    流>>公制;
    //在这里做你想做的事
    
    cout首先您必须确保输入在整数部分和度量部分之间有一个空格,然后您应该

  • 把它分成两部分,然后

  • 将第一部分转换为整数

  • 如果您不想自己做这样乏味的工作,可以使用
    ssstream
    。下面是一个简短的示例

    #include<string.h>
    #include<iostream>
    #include<sstream>
    
    using namespace std;
    
    int main()
    {
        string input("20 kg");
    
        istringstream stream(input);
    
        int n;
        string metric;
    
        stream >> n;
        stream >> metric;
    
        //do something you want here
    
        cout<<n<<" "<<metric;
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    使用名称空间std;
    int main()
    {
    字符串输入(“20千克”);
    istringstream(输入);
    int n;
    字符串度量;
    流>>n;
    流>>公制;
    //在这里做你想做的事
    
    cout首先您必须确保输入在整数部分和度量部分之间有一个空格,然后您应该

  • 把它分成两部分,然后

  • 将第一部分转换为整数

  • 如果您不想自己做这样乏味的工作,可以使用
    ssstream
    。下面是一个简短的示例

    #include<string.h>
    #include<iostream>
    #include<sstream>
    
    using namespace std;
    
    int main()
    {
        string input("20 kg");
    
        istringstream stream(input);
    
        int n;
        string metric;
    
        stream >> n;
        stream >> metric;
    
        //do something you want here
    
        cout<<n<<" "<<metric;
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    使用名称空间std;
    int main()
    {
    字符串输入(“20千克”);
    istringstream(输入);
    int n;
    字符串度量;
    流>>n;
    流>>公制;
    //在这里做你想做的事
    
    cout首先您必须确保输入在整数部分和度量部分之间有一个空格,然后您应该

  • 把它分成两部分,然后

  • 将第一部分转换为整数

  • 如果您不想自己做这样乏味的工作,可以使用
    ssstream
    。下面是一个简短的示例

    #include<string.h>
    #include<iostream>
    #include<sstream>
    
    using namespace std;
    
    int main()
    {
        string input("20 kg");
    
        istringstream stream(input);
    
        int n;
        string metric;
    
        stream >> n;
        stream >> metric;
    
        //do something you want here
    
        cout<<n<<" "<<metric;
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    使用名称空间std;
    int main()
    {
    字符串输入(“20千克”);
    istringstream(输入);
    int n;
    字符串度量;
    流>>n;
    流>>公制;
    //在这里做你想做的事
    cout(特别是s)可以很容易地处理这种情况:

    int weight;
    std::string units;
    std::cout << "Guess the weight of the cake: ";
    if (std::cin >> weight >> units)
    {
        std::cout << weight << units << "? Spot on!" << std::endl;
    }
    else
    {
        std::cerr << "Expected a numeric weight and alphabetic units (e.g: 42 kg)."
                  << std::endl;
    }
    
    int权重;
    std::字符串单位;
    标准::库特>重量>>单位)
    {
    std::cout(特别是s)可以很容易地处理这种情况:

    int weight;
    std::string units;
    std::cout << "Guess the weight of the cake: ";
    if (std::cin >> weight >> units)
    {
        std::cout << weight << units << "? Spot on!" << std::endl;
    }
    else
    {
        std::cerr << "Expected a numeric weight and alphabetic units (e.g: 42 kg)."
                  << std::endl;
    }
    
    int权重;
    std::字符串单位;
    标准::库特>重量>>单位)
    {
    std::cout(特别是s)可以很容易地处理这种情况:

    int weight;
    std::string units;
    std::cout << "Guess the weight of the cake: ";
    if (std::cin >> weight >> units)
    {
        std::cout << weight << units << "? Spot on!" << std::endl;
    }
    else
    {
        std::cerr << "Expected a numeric weight and alphabetic units (e.g: 42 kg)."
                  << std::endl;
    }
    
    int权重;
    std::字符串单位;
    标准::库特>重量>>单位)
    {
    std::cout(特别是s)可以很容易地处理这种情况:

    int weight;
    std::string units;
    std::cout << "Guess the weight of the cake: ";
    if (std::cin >> weight >> units)
    {
        std::cout << weight << units << "? Spot on!" << std::endl;
    }
    else
    {
        std::cerr << "Expected a numeric weight and alphabetic units (e.g: 42 kg)."
                  << std::endl;
    }
    
    int权重;
    std::字符串单位;
    标准::库特>重量>>单位)
    {
    std::cout通过将它们作为一个整体来使用
    pair
    ,之后易于处理

    pair<int, string> val;
    if (cin >> val.first >> val.second) 
        // read input sucessfully, e.g. val will be {20, "kg"}
    else 
        cerr << "unable to input weight and units\n"
    
    pairval;
    如果(cin>>val.first>>val.second)
    //读取输入成功,例如val将为{20,“kg”}
    其他的
    cerr使用
    配对
    ,将它们作为一个整体考虑,便于事后处理

    pair<int, string> val;
    if (cin >> val.first >> val.second) 
        // read input sucessfully, e.g. val will be {20, "kg"}
    else 
        cerr << "unable to input weight and units\n"
    
    pairval;
    如果(cin>>val.first>>val.second)
    //读取输入成功,例如val将为{20,“kg”}
    其他的
    cerr使用
    配对
    ,将它们作为一个整体考虑,便于事后处理

    pair<int, string> val;
    if (cin >> val.first >> val.second) 
        // read input sucessfully, e.g. val will be {20, "kg"}
    else 
        cerr << "unable to input weight and units\n"
    
    pairval;
    如果(cin>>val.first>>val.second)
    //读取输入成功,例如val将为{20,“kg”}
    其他的
    cerr使用
    配对
    ,将它们作为一个整体考虑,便于事后处理

    pair<int, string> val;
    if (cin >> val.first >> val.second) 
        // read input sucessfully, e.g. val will be {20, "kg"}
    else 
        cerr << "unable to input weight and units\n"
    
    pairval;
    如果(cin>>val.first>>val.second)
    //读取输入成功,例如val将为{20,“kg”}
    其他的
    
    cerr我的想法是让用户将整个内容作为字符串输入,然后您可以使用substr方法将字符串分成数字部分,然后是测量部分。 然后必须将数字部分转换为整数

    范例

    string str = "20 lb";
    string delimiter = " ";  //space
    string number = str.substr(0, str.find(delimiter)); // this will get you the number
    string measurement = str.substring(str.find(delimiter)+1, str.length()) //this will get you the               measurement
    //convert the number string now
    

    这应该对你有用

    我的想法是让用户将整个内容作为字符串输入,然后你可以使用substr方法将字符串分成数字部分和测量部分。 然后必须将数字部分转换为整数

    范例

    string str = "20 lb";
    string delimiter = " ";  //space
    string number = str.substr(0, str.find(delimiter)); // this will get you the number
    string measurement = str.substring(str.find(delimiter)+1, str.length()) //this will get you the               measurement
    //convert the number string now
    

    这应该对你有用

    我的想法是让用户将整个内容作为字符串输入,然后你可以使用substr方法将字符串分成数字部分和测量部分。 然后必须将数字部分转换为整数

    范例

    string str = "20 lb";
    string delimiter = " ";  //space
    string number = str.substr(0, str.find(delimiter)); // this will get you the number
    string measurement = str.substring(str.find(delimiter)+1, str.length()) //this will get you the               measurement
    //convert the number string now
    

    这应该对你有用

    我的想法是让用户将整个内容作为字符串输入,然后你可以使用substr方法将字符串分成数字部分和测量部分。 然后必须将数字部分转换为整数

    范例

    string str = "20 lb";
    string delimiter = " ";  //space
    string number = str.substr(0, str.find(delimiter)); // this will get you the number
    string measurement = str.substring(str.find(delimiter)+1, str.length()) //this will get you the               measurement
    //convert the number string now
    

    这应该对你有用,你是说后缀,对吗?@Robert
    kilo
    是前缀。但是
    kikg
    后面是后缀,所以我想那是后缀。我不是很确定。我想是SI前缀。