如何在C ++中将用户输入存储到数组中

时间:2018-09-20 16:09:05

标签: c++

我不熟悉C ++,想问这个基本问题

我想要什么:用户输入的数据如2:3American,4:2China(这意味着我的国家队赢得2分,美国队再输3分。我的国家队获得4分,中国队获得2分)

在控制台中: 请输入您的团队对其他团队的结果,输入负数退出 2:3美国 4:2中国 -1

结果获胜:1        输:1        抽奖:0

3 个答案:

答案 0 :(得分:0)

如果某些权威机构没有为您提供特定的编码,请使用尽可能简单的编码。更好的是“ 2 3 American 4 2 China”。这样您就只需要处理一个简单的for循环。

未计算结果行。将每个字符串转换为整数以进行计算。

int main( int argc, char* argv[]) {

    std::vector<std::string> arguments(argv + 1, argv + argc);

    std::cout << "arguments contains \n";

    for (std::vector<std::string>::iterator it = arguments.begin() ; it != arguments.end(); ++it) {

        int firstPos = it->find_first_of(":");
        int secPos = 0;

        std::string firstInteger = it->substr(0,firstPos);

        std::string secondInteger;

        if ( firstInteger.compare("-1") == 0 ) {

            std::cout << "breaking \n";
            return 0;
        } else {
            std::cout << " f=<" << firstInteger << ">";
            secPos = it->find_first_not_of( "012345678:", firstPos);
            if ( secPos == std::string::npos )
                std::cout << "not found";

            std::cout << " s=<" << it->substr(firstPos+1 ,secPos-firstPos-1 )  << "> "; 
            std::string  teamName = it->substr(secPos);
            std::cout << teamName ;
            std::cout << std::endl;
        } 
    }
    std::cout << '\n';
    return 0;
}

答案 1 :(得分:0)

很久以前,我已经编写了类似此问题的代码。进行了一些小的更改即可解决您的问题。
所以我认为这就是您想要的

输入“ 2:3美国4:2中国-1”(单行)

预期的输出

#include <iostream>
using namespace std;
size_t non_int(int index,string* s)
{
int i=index;
for( i=index;i<s->length();i++){    
if(s->at(i) >= '0' && s->at(i) <= '9')
    {
       // cout << s->at(i) << " is a Digit";
    }
    else{
        return (i-1)<index?(std::string::npos):(i-1);
    }
}
 return (i-1)<index?(std::string::npos):(i-1);;
}
int main()
{
    cout << "Please input the match result such as 2:3American " << endl;
    string str;
    std::getline (std::cin,str);
    //cout<<str;// i want to see did the first user input stored in array. But seems the console..does not print out temp[0] and just skipt it 
    int win,lose,draw=0;
    std::size_t found = 0;
    string s1,s2;
    int i1,i2;
    std::size_t f1,f2;
    while( found  !=std::string::npos)
    {
         f1 = str.find(":",found);
         if (f1!=std::string::npos){
            i1 = stoi(str.substr(found,f1-found));
            f2 = non_int(f1+1,&str);
             if (f2!=std::string::npos){
                i2 = stoi(str.substr(f1+1,f2-f1));
                if(i1>i2) win++;
                else if(i1<i2)lose++;
                else draw++;
             }
             else {
               cout<<"ERROR :invalid input ";   
             }  
         }
         else {
        //exit on -ve input 
    //  cout<<"ERROR 45 ";
         }  
    found = str.find(" ",found+1);      
    }
   cout<<"win:"<<win<<"lose:"<<lose<<"draw:"<<draw<<endl;
    return 0;
}

答案 2 :(得分:0)

步骤1:

定义一个代表输入令牌的类。

struct Segment
{
    int         myCountryScore;
    int         otherCountryScore;
    std::string otherCountry;
};

步骤2

定义一个从流中读取Segment的输入函数。

    std::istream& operator>>(std::istream& s, Segment& data)
    {
        Segment tmp;
        char    sep;
        int     firstNumber;
        bool    good = false;
        if (s >> firstNumber && firstNumber >= 0)
        {
            tmp.myCountryScore = firstNumber;
            if (s >> std::noskipws >> sep >> tmp.otherCountryScore >> tmp.otherCountry >> std::skipws) && (sep == ':'))
            {
                // The read worked. Copy it to the output object.
                data = tmp;
                good = true;
            }
        }
        if (!good) {
            // If there was an error reading.
            // Or we reached the end (negative number read)
            // Then set the state of the stream to failure mode.
            s.setstate(std::ios::failbit);
        }
        return s;
    }

步骤3

编写一个循环,该循环从循环中的流中读取Segments

Segment object;
while(std::cin >> object) {
    // You have correctly read an object
    // Add your code to handle it here.
}

第3步替代方法。

您可以简单地使用流迭代器将它们Segment逐个复制到向量中。

std::vector<Segment>  data(std::istream_iterator<Segment>(std::cin),
                           std::istream_iterator<Segment>());