0

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

6
  • 1
    Can you give an example? Commented Jul 31, 2013 at 17:27
  • "Multiple char arrays" translates to "multiple strings". So read strings (std::string), not characters. Given an std::string input;, when you do std::cin >> input, you'll be reading-in a char array. Commented Jul 31, 2013 at 17:29
  • Vector of vectors (or vector of strings) should be fine for this. Just keep extracting from input, count the values as you go and add them to a vector. When you've had enough push the vector to the "major" vector. Repeat as long as there's input waiting or an error occurs. Commented Jul 31, 2013 at 17:29
  • @NikosC. I don't understand how to avoid the same problem with strings. Commented Jul 31, 2013 at 17:32
  • 1
    Call me simple-minded, but why not just jam them into a single vector and iterate on them in subsets of three? Commented Jul 31, 2013 at 17:37

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

7 Comments

Can you explain how line works. I understand its just a variable name for the string, but how does the while statement work. Is it saying to read an entire line of input to "string" . If so, how do you get to the next line?
@Marla: I added some explanation and a link to some code that works.
I'm just a little confused about the user stops input when they're done. For example, if they enter -1, then stop.
This will never stop, while (cin >> line) will pull all values you enter separated by spaces as separate line's, but when you reach the very end it will prompt the user for more input and never end.
@Lochemage, is there a way to avoid that. Such as indicating with a certain character that they are done? Do I have to use a break statement?
|
-1

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.

EDIT: 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. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.