簡體   English   中英

cin.get問題

[英]Problems with cin.get

當我的代碼的這一部分運行時,它立即全部彈出到控制台中,並且沒有給我機會像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);
        }

假設您可以使用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

我認為您應該修改代碼,如下所述:

#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++;
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM