array

Hello,
I have a file with numbers and want to display it on the screen.

Any help related to this issue would be very appreciated, thank you very much! :)
Last edited on
1
2
vector<int> stations;
vector<stations> distances; //what do you expect this to do? 
"stations" is a variable, not a type.
Instead of
vector<int>stations;
vector<stations>distances;

you should write

vector<int>stations;
vector<vector<int>>distances;

Or

typedef vector<int> Station;
Station stations;
vector<Station>distances;

Last edited on
Thank you very much! I changed this. Now the build is successful, but it gives warning for 'for loops': signed/unsigned mismatch. The program opens console, but there is just a line 'Press any key to continue...' and no numbers.
Last edited on
stations.size() is 0, the loop never executes.
thank you, how could i fix it? should i define the size before the for loop?
Last edited on
I'm assuming that the CSV file is set so that each line is a row and each comma separates columns?
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
 
int main()
{
    std::vector<std::vector<int> > distances;
    std::ifstream MyFile ("file.csv");
    if(MyFile.is_open())
    {
        std::string line;
        while(std::getline(MyFile, line))
        {
            std::istringstream row (line);
            distances.push_back(std::vector<int>());
            std::string cell;
            while(std::getline(row, cell, ','))
            {
                int dist;
                std::istringstream(cell) >> dist;
                distances.back().push_back(dist);
            }
        }
    }
}
http://ideone.com/hZpq9y
I'm not sure about what your stations vector is for.
Thank you for the code! However, I still got some errors and the screen just flashes and disappears.
Last edited on
Those aren't errors.

The screen flashes and goes away because I have no output in my code ;)
Last edited on
Alright, that's true, sorry! Should I use ostringstream?

Thank you for your help!
Last edited on
No, what I mean is, the program only takes input and stores it in a vector - if you want to see the results you have to std::cout the vector. Here is my original code, modified to display the vector:
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
 
int main()
{
    std::vector<std::vector<int> > distances;
    std::ifstream MyFile ("file.csv");
    if(MyFile.is_open())
    {
        std::string line;
        while(std::getline(MyFile, line))
        {
            std::istringstream row (line);
            distances.push_back(std::vector<int>());
            std::string cell;
            while(std::getline(row, cell, ','))
            {
                int dist;
                std::istringstream(cell) >> dist;
                distances.back().push_back(dist);
            }
        }
    }

    for(std::size_t i = 0; i < distances.size(); ++i)
    {
        for(std::size_t j = 0; j < distances[i].size(); ++j)
        {
            std::cout << distances[i][j] << ' ' << std::flush;
        }
        std::cout << std::endl;
    }
}
http://ideone.com/DxvHtj
Thank you very very much!!
Is there a way to have letters and numbers in one array?
Last edited on
You could use a vector of std::pair<int, std::string>, or you could make a struct and have the vector be a vector of that struct. I recommend the struct because it makes it easier to change your code if you want to add more in the future.
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

struct Node
{
    int numlinks;
    std::string name;

    Node(int num, std::string n) : numlinks(num), name(n)
    {
    }

    friend std::ostream &operator<<(std::ostream &o, Node &node)
    {
        return o << node.name << " has " << node.numlinks << " links";
    }
};
 
int main()
{
    std::vector<std::vector<Node> > distances;
    std::ifstream MyFile ("file.csv");
    if(MyFile.is_open())
    {
        std::string line;
        while(std::getline(MyFile, line))
        {
            std::istringstream row (line);
            distances.push_back(std::vector<Node>());
            std::string cell;
            while(std::getline(row, cell, ','))
            {
                int dist;
                std::istringstream(cell) >> dist;
                distances.back().push_back(Node(dist, "name"));
            }
        }
    }

    for(std::size_t i = 0; i < distances.size(); ++i)
    {
        for(std::size_t j = 0; j < distances[i].size(); ++j)
        {
            std::cout << distances[i][j] << ", " << std::flush;
        }
        std::cout << std::endl;
    }
}
http://ideone.com/EIOTX8
How could I have particular names which are read from the file? Sorry for asking so many questions!
Last edited on
This is not your original question, please create a separate topic for it.
Topic archived. No new replies allowed.