using 2d arrays.

complete
Last edited on
You do not need to use getline. You can use simply operator >> provided that each name has no more than 499 characters.:)
Otherwise you should read the whole line and then use istringstream to extract each item.
You have an input stream. You need to be able to use spaces as delimiters to parse each row so that you can pull each individual "word" out of it. Then you need to put them inside your array, once they've been completely parsed.

Also, readfile() is an int, and you're not accepting anything back. If you don't need anything back, don't give it a type. int main() needs to return something. I would recommend 0.
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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    // You need to know how many records are in the file as
    // well as a good estimate on how long the names are.
    const int number_of_records = 50, name_length = 35;

    // Declare two 2D arrays to store both names and numbers.
    char names[number_of_records][name_length];
    int numbers[number_of_records][2];

    ifstream file;

    file.open("input.txt");

    if (!file)
    {
        cout << "File does not exist.";
        return 1;
    }

    // Store the names in the names array and
    // the numbers in the numbers array.
    for (int i = 0; i < number_of_records; i++)
    {
        file >> names[i];
        file >> numbers[i][0] >> numbers[i][1];
    }

    file.close();

    // Display the contents of both arrays.
    for (int i = 0; i < number_of_records; i++)
    {
        cout << names[i] << ' ' << numbers[i][0] << ' ' << numbers[i][1] << endl;
    }

    return 0;
}
Can anyone shine some light, I'm still lost
Topic archived. No new replies allowed.