【问题标题】:Problems with cin.getcin.get 的问题
【发布时间】:2016-03-10 19:11:12
【问题描述】:

当我的这部分代码运行时,它会立即弹出到控制台中,并且不会让我有机会像 cin 一样输入 cin.get 的数据。 我需要将 cin.get 更改为什么,以便它在每次 cout 后停止并让我输入数据。数据需要能够在其中包含空格、字母和数字,如果有帮助,我只需要能够有 1 个空格。 感谢大家的帮助

编辑:我已将所有 cin.get 更改为 cin,将所有 char qx[10] 更改为字符串 qx。如果有人知道如何防止输入空格,那么它也可以解决我的问题。

int max = 10;
        int randomnumber;

        srand(time(0));
        randomnumber = (rand() % max) + 1;
        cout << randomnumber;

        if (randomnumber == 1)
        {
            char q1[10];
            char q2[10];
            char q3[10];
            char q4[10];
            char q5[10];
            char q6[10];
            char q7[10];
            char q8[10];
            char q9[10];
            char q10[10];

            system("CLS");
            cout << "Quiz Version : A" << endl;
            cout << "sdsdfg:";
            cin.get(q1, 10);
            cout << endl <<"sdfg:";
            cin.get(q2, 10);
            cout << endl << "asdf:";
            cin.get(q3, 10);
            cout << endl << "sdsdfg:";
            cin.get(q4, 10);
            cout << endl << "sdfgsdfg:";
            cin.get(q5, 10);
            cout << endl << "dfgdf:";
            cin.get(q6, 10);
            cout << endl << "zxcvzxc:";
            cin.get(q7, 10);
            cout << endl << "erteetrre:";
            cin.get(q8, 10);
            cout << endl << "dsfgasdgf:";
            cin.get(q9, 10);
            cout << endl << "xvcbyht:";
            cin.get(q10, 10);
        }
        else if (randomnumber == 2)
        {
            char q1[10];
            char q2[10];
            char q3[10];
            char q4[10];
            char q5[10];
            char q6[10];
            char q7[10];
            char q8[10];
            char q9[10];
            char q10[10];

            system("CLS");
            cout << "Quiz Version : B" << endl;
            cout << "sdsdfg:";
            cin.get(q1, 10);
            cout << endl << "sdfg:";
            cin.get(q2, 10);
            cout << endl << "asdf:";
            cin.get(q3, 10);
            cout << endl << "sdsdfg:";
            cin.get(q4, 10);
            cout << endl << "sdfgsdfg:";
            cin.get(q5, 10);
            cout << endl << "dfgdf:";
            cin.get(q6, 10);
            cout << endl << "zxcvzxc:";
            cin.get(q7, 10);
            cout << endl << "erteetrre:";
            cin.get(q8, 10);
            cout << endl << "dsfgasdgf:";
            cin.get(q9, 10);
            cout << endl << "xvcbyht:";
            cin.get(q10, 10);
        }

【问题讨论】:

    标签: c++ string cin


    【解决方案1】:

    假设您可以使用std::string(正如标签/编辑所建议的那样):

    如果你替换

    char q1[10];
    //...
    char q10[10];
    

    string q1;
    //...
    string q10;
    

    然后您可以使用std::getline 可靠地获取用户输入:

    cout << "Quiz Version : A" << endl;
    cout << "sdsdfg:";
    getline(cin, q1); //Gets user input from cin and places it in q1
    

    【讨论】:

      【解决方案2】:

      我认为你应该如下修改代码:

      #include <iostream>
      using namespace std;
      
      #define NUM_ELEMS 3
      #define BUF_LEN 10
      
      main()
      {
        char s[NUM_ELEMS][BUF_LEN];
      
        int i = 0;
        while (i < NUM_ELEMS && !cin.fail()) {
          cout << "Enter some text: ";
          cin.getline(s[i], BUF_LEN);
          cout << "You entered '"<< s[i] << "'" << endl;  
          i++;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2022-08-12
        • 2014-07-13
        • 1970-01-01
        • 1970-01-01
        • 2014-03-10
        • 2011-12-30
        • 1970-01-01
        • 2011-10-24
        • 1970-01-01
        相关资源
        最近更新 更多