-1

So I'm trying to figure out how to assign a number of indexes based on the input. Let's say the input is:

9 10 11 12 13

I want the computer to create an array with the number of numbers in the list; in this case here are 5 numbers. I want the code to create an array and make it have 5 total elements/indexes.

How can I do this?

[NOTE: there will not always be 5 numbers, there may be any amount of numbers]

cuay
  • 15
  • 1
  • `int indexes[5] = {9 10 11 12 13}`; or `int indexes[5]; cin >> indexes[0]; cin >> indexes[1]; cin >> indexes[2]; cin >> indexes[3]; cin >> indexes[4]; ` – Raindrop7 Nov 20 '17 at 22:13
  • 1
    [Best to start with the right way: `std::vector::push_back`.](http://en.cppreference.com/w/cpp/container/vector) If you are not allowed to use `std::vector` this gets more complicated and you'll have to state additional assignment constraints and requirements. – user4581301 Nov 20 '17 at 22:13
  • Do you want to have an array with 5 elements {9, 10, 11, 12, 13} or do you want to work with the *indices* 9, 10, 11, 12, 13 (i.e. your array must have 14 elements)? And if you want to work with the indices, what other value do you want to assign to them? First part of your question to me suggests you want to assign some (magic) value to these indices, the second part sounds like you want to generate an array with 5 elements with these five values. – aufziehvogel Nov 20 '17 at 22:13
  • @Aufziehvogel He wants to create an array based on a variable size. In his example, he used 9, 10, 11, 12, 13 as the array content so the array size is 5. In general, he wants to create an array of size n where n is the input size which isnt known beforehand. That's my interpretation of the question. – Javia1492 Nov 20 '17 at 22:15
  • This feels like a homework question. –  Nov 20 '17 at 22:16
  • I think this might of be use https://stackoverflow.com/questions/4029870/how-to-create-a-dynamic-array-of-integers . If you HAVE to use an array, so be it, but you could just use a `std::vector` and not worry about input size. – Javia1492 Nov 20 '17 at 22:16
  • You can get input as a string, count the number of element, create an array with the previous information, store the input using the first string. – Hollyol Nov 20 '17 at 22:17
  • Note: There is nothing wrong with a homework question if it is presented correctly and shows that the asker has put forth effort. This question doesn't do either particularly well. A discussion on this topic: https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems – user4581301 Nov 20 '17 at 22:32

1 Answers1

1

You can use std::getline to read from your programs input stream, and then create a new stream and split the string at ' ', you can the use std::stoi to convert the input to int s :

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

int main()
{
    std::string input;
    std::getline(std::cin, input);
    std::stringstream  inputStream(input);

    std::string line;
    std::vector<int> numericResults;
    while(std::getline(inputStream,line,' ')) {
        numericResults.push_back(std::stoi(line));
    }
}

Or since your deliminator is a space you could simplify the loop as suggest by @user4581301:

int numericResult; 
std::vector<int> numericResults;
while(inputStream >> numericResult) {
    numericResults.push_back(numericResult);
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
George
  • 2,101
  • 1
  • 13
  • 27