简体   繁体   中英

Problems with cin.get

When this part of my code is run it pops up into the console all at once and does not give me a chance to enter in the data for cin.get like it would cin. What do I need to change the cin.get into so that it will stop after every cout and let me input data. The data needs to be able to have spaces, letters, and numbers in it, if it helps I only need to be able to have 1 space. Thanks for all help

Edit: Ive changed all the cin.get to cin and all the char qx[10] to string qx. If anyone knows how to prevent spaces being entered then it too would fix my problem.

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);
        }

Assuming you can use std::string (as the tag/edit suggests):

If you replace

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

With

string q1;
//...
string q10;

Then you can use std::getline to get user input reliably:

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

I think you should modify the code as mentioned below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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