0

I am creating a program in C++ that allows a user to input the number of times he or she wants to be prompted for three separate inputs, ie.

how many would you like: 2
enter here: 123.45/N 32.45/W Los Angeles
enter here: 22.22/N 223.4/E Hong Kong

The way I get the three separate inputs is by creating three string variables, and doing this:

cin << input1 << input2;
getline(cin, input3);

I have a parser in a separate file that I created that gets the number input out of the first two strings and does some calculations on it.

The problem I'm having is visualizing how to set up a system only using the std library, where I can have the user enter the number of times they want to input places, and then have the program create 3 unique strings I can reference later for calculations, and have it do cin/getline the amount of times the user enters.

One way I thought of was creating a function that takes an integer (the amount the user entered) and goes through a for loop which calls cin and getline. The problem with that is, how do I save and reference the values the user inputs for calculations later on? ie.

void inputAmount(int n) {
    for(int i = 0; i < n; i++) {
        cin << input1 << input2;
        getline(cin, input3);
    }
}

Where n is the amount of lines the user wants to enter. I tried to create an array of strings and initializing it with (n * 3) elements, but that apparently doesn't work with C++ since the variable must be constant and declared. I'm just confused on how to proceed, or how to achieve this.

4
  • Are there any restrictions on what you can and can't use from the std library? Commented Oct 21, 2014 at 4:15
  • Not many restrictions that I know of, just have to use basic tools though. I've seen stuff like a boost function and some other crazy things, but my class has literally only learned about references and arrays. Commented Oct 21, 2014 at 4:42
  • Can you use a std::vector? Commented Oct 21, 2014 at 4:51
  • I'll go ahead and assume yes Commented Oct 21, 2014 at 4:59

1 Answer 1

1

You can use a std::vector instead of an array. A std::vector does not require a size at compile time. Your code would look something like this:

string input1, input2, input3;
int n; // number of lines
vector<string> v; // vector to hold the lines

// prompt user for number of lines
cout << "how many lines?" << endl;
cin >> n;

for (int i = 0; i < n; i++) {
  cin >> input1 >> input2;
  getline(cin, input3);
  // parse and put back into input1, input2, etc. or some other variable as needed
  v.push_back(input1);
  v.push_back(input2);
  v.push_back(input3);
}

The call to push_back() adds the element to the vector. You can access the elements with an iterator or with the [] operator (same as an array). It would probably be better to create a struct to store the three inputs together, in which case you would parameterize the vector with your struct instead of a string, but this is the basic idea.

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

2 Comments

Sorry I'm replying late.. so I'm trying to figure out how to access each element of the vector. Ie. lets say the user wants to enter 2 versions of input, which would mean 6 input variables, would the vector account for that each time I push an input back into the vector? Ie. push_back(input1) would be accessible at v[0] and then when it runs again, another, different version of input1 is created that I can access at v[0] while the first input1 is at v[5]?
@Alex The vector will continue to add new strings to the end for every call to push_back(). The first run through the for loop will put input1 in v[0], input2 in v[1], etc. The second run through the for loop will put input1 (with the new information from the user for the second triplet) in v[3], input2 in v[4], and input3 in v[5].

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.