简体   繁体   中英

How do I store multiple values from user input into a vector at once (in c++)?

For a school project I have to use code to decipher a line of numbers separated by commas.

For example, a user might enter this an input:

10,8.5,11,10.5,7.5,5,6

and using the provided key, this would be the output:

sputnik

I need to take the input above, and somehow get each of the numbers (inbetween the commas) into a vector, then I will be about to decipher each value.

I have used strings for the other ciphers (rot13 and rot6) but just strings alone will not work in this case, obviously.

I could easily do it one value at a time using a for loop, but I don't think that's what he wants.

Any help would be appreciated! Thanks.

You could do something as simple as this:

std::vector<double> data;
double value = 0.0;
while (my_data_file >> value)
{
  data.push_back(value);
  char comma;
  my_data_file >> comma;
}

If you must use arrays here's the equivalent:

const size_t MAXIMUM_DATA = 32;
double data[32];
double value;
size_t array_index = 0;
while ((array_index < MAXIMUM_DATA) && (my_data_file >> value))
{
    data[array_index] = value;
    ++array_index;
    char comma;
    my_data_file >> comma;
}

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