简体   繁体   中英

Is there a way to have user input multiple char arrays at once c++

I currently have a function that takes a array of 4 characters and returns another value based on that sequence of characters.

What I want is to have the user input a whole line of characters and then create a loop to go over each "sub- group of characters" and then return the result for all of them.

My initial thinking is to somehow use push_back to keep adding the arrays to a vector.

I don't know how long the entire array will be, but it should be a product of 3.

As an example, right now I am able to do :

char input [4] ;
cin >> input;  

int i = name_index[name_number(input)];
cout << name[i].fullName;

But what I would like is the user ti input multiple name abbreviations at once

I would change your sample from this:

char input [4] ;
cin >> input;  

int i = name_index[name_number(input)];
cout << name[i].fullName;

To this:

string input;
cin >> input;  

const int i = name_index[name_number(input)];
cout << name[i].fullName;

Then you can start using a vector to track multiple inputs:

vector<string> inputs;
string line;
while (cin >> line)
{
    if (line == "done")
        break;
    inputs.push_back(line);
}

for (unsigned int i = 0; i < inputs.size(); ++i)
{
    cout << "inputs[" << i << "]: " << inputs[i] << endl;
    //const int index = name_index[name_number(inputs[i])];
    //cout << name[index].fullName;
}

You asked for an explanation of line . The line while (cin >> line) tries to take text from the standard input and put it into line . By default, this will stop when it encounters whitespace (space, tab, return, etc.) If it succeeds, then the body of the while loop is executed and we add what was input to the vector . If not, then we assume we're at the end of input and stop. We can then process the vector . (In the code linked below, I just output it since I don't know what name_index or name_number are.

( Working code here )

The way cin works, is it will accept any amount of input and separate them each by spaces, when you ask for specific input it prompts the user to give input, and then only takes the very first string (up until the space). If there are any more input after that, another cin >> input will just retrieve that value without prompting the user again. You can tell when the actual end of the input is reached when there is only a newline character left. This code should allow you to type in multiple strings separated by spaces and then process them all at once after the user enters the text:

char input[4];
do // Start our loop here.
{
    // Our very first time entering here, it will prompt the user for input.
    // Here the user can enter multiple 4 character strings separated by spaces.

    // On each loop after the first, it will pull the next set of 4 characters that
    // are still remaining from our last input and use it without prompting the user
    // again for more input.

    // Once we run out of input, calling this again will prompt the user for more
    // input again. To prevent this, at the end of this loop we bail out if we
    // have come accros the end of our input stream.
    cin >> input;

    // input will be filled with each 4 character string each time we get here.
    int i = name_index[name_number(input)];
    cout << name[i].fullName;
} while (cin.peek() != '\n'); // We infinitely loop until we reach the newline character.

Also, keep in mind that allocating only 4 characters to input does not account for the end of string character '\\0' that will be tacked on the end. 另外,请记住,仅分配4个字符来input并不能说明将在结尾添加的字符串字符'\\0'的末尾。 If the user inputs 4 characters then it will actually access bad memory when it assigns the string to your input . There are 4 characters + 1 end character which means you need a minimum of 5 characters allocated for your input . The best way is to just use std::string as it will size itself properly even if the user enters more than 4 characters.

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