1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
#include <string>
#include <sstream>
struct Data { // (You may want to think of a better name)
std::string name;
int numbers[3];
};
int main() {
std::istringstream file (
"tom doe,35, 68, 99,\n"
"Jane doe,24, 55, 96,\n"
"ben doe, 46, 73, 94,\n"
"Sussie doe, 23, 56, 77,\n"
);
const int Size = 4;
Data data[Size];
// get data
for (int i = 0; i < Size; i++)
{
std::string name;
std::getline(file, name, ',');
data[i].name = name;
for (int j = 0; j < 3; j++)
{
std::string num_str;
std::getline(file, num_str, ',');
data[i].numbers[j] = std::stoi(num_str);
}
// discard extra newline
std::string temp;
std::getline(file, temp);
}
// display data
for (int i = 0; i < Size; i++)
{
std::cout << "name = " << data[i].name << std::endl;
std::cout << "numbers = ";
for (int j = 0; j < 3; j++)
{
std::cout << data[i].numbers[j] << " ";
}
std::cout << "\n" << std::endl;
}
return 0;
}
|