2

I have a file that consists of lines that look like this:

Alice 60 30 75
Bob 20 250 12

Where the name and integer lengths are variable. What's the shortest way to put the name into a string and the integers into an array of size 3? I did a getline() and then pushed the first char up to the first space into a char vector, transferred to string, then did the next char to space, converted using atoi() then sent to array, etc. I feel like there's probably a much better way?

I tried the suggestion line this:

int main() {
    ifstream infile("wheelgame.txt");
    string s;
    vector<int> a(3); 

    while (cin >> s >> a[0] >> a[1] >> a[2])
    {
        cout << "test";
    }

    }

But I think I'm misunderstanding? It runs forever this way.

Austin
  • 6,921
  • 12
  • 73
  • 138

2 Answers2

10

A shorter way

std::string s;
std::vector<int> a(3);    // or int a[3]; or std::array<int, 3> a;
std::cin >> s >> a[0] >> a[1] >> a[2];

EDIT: Change the while loop to read from file instead of stdin (i.e. cin)

while (infile >> s >> a[0] >> a[1] >> a[2]) {
    ...
}

This loop won't run forever.

Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • You get a +1 for using a vector instead of an array. Better to use a [std::array](http://en.cppreference.com/w/cpp/container/array) though if you don't need the re-sizing. – NathanOliver Jul 09 '15 at 14:28
  • Thanks that looks a whole lot better. I'm forced to use an array because I need to pass it to a function that takes list[], but this should still work well. Can you still use getline() with this? – Austin Jul 09 '15 at 14:28
  • 1
    @AustinMW You can always just copy the vector contents to an array if you really need to. – Hatted Rooster Jul 09 '15 at 14:32
  • 3
    @AustinMW The `list[]` is not a problem, just pass in `a.data()` (or `&a[0]` if you are still pre-C++11). – Baum mit Augen Jul 09 '15 at 14:33
  • Or should I do `while(!file.eof())`? How do I move to the next line? – Austin Jul 09 '15 at 14:34
  • @AustinMW [Most certainly not](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Also: One question per question. If you have another question, just ask it in another question, not in the comments to an answer of your old question. – Baum mit Augen Jul 09 '15 at 14:35
  • 2
    @AustinMW No, use `while (cin >> s >> a[0] >> a[1] >> a[2]) {}` – Shreevardhan Jul 09 '15 at 14:35
  • Sorry for my ignorance, but are you saying to do that without the while(getline()) at all? I tried it both ways and it seems to run forever, I think I'm still misunderstanding something. – Austin Jul 09 '15 at 14:54
  • @AustinMW Update your question with the problem you're facing or write a new one. – Shreevardhan Jul 09 '15 at 14:56
  • Ahh thanks! I'm a beginner to file-io, didn't know you could do that. – Austin Jul 09 '15 at 15:15
2

If your string is a char array and you don't want to use STL:

char str[MAX];
int a[3];
fscanf(file, "%s %d %d %d", str, &a[0], &a[1], &a[2]);
Nghia Bui
  • 3,694
  • 14
  • 21