Redirecting the console output into an array?

Hi!
I have a problem with 2-d arrays, what i cant solve...I have to write a program, that settles if the most frequent birds peculiar to an area were the same. The number of areas is N. We note the number of the birds. The number of the species is M.
I have the maximum values, I can print them on the console, but I should redirect them into an array to make the end of the program. I tried the vector thingm but we haven't learned that and I can't cope with it. Does someone have a better idea? Here's the code, what i have.
(Sorry for my bad english.)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

const int maxn=100;
const int maxm=100;

void input_size(int &n, int &m);
void input_names(string city[maxn], string species[maxm], int n, int m);
void input_array(int x[maxn][maxm], int n, int m, string city[maxn], string species[maxm]);
void max(const int x[maxn][maxm], int n, int m, string city[maxn], string species[maxm]);
void waitforkey();

int main()
{
    int x[maxn][maxm];
    int n, m;
    string city[maxn];
    string species[maxm];
    input_size(n, m);
    input_names(city, species, n, m);
    input_array(x, n, m, city, species);
    max(x, n, m, city, species);
    waitforkey();
}
void input_size(int &n, int &m)
    {
        string tmp;
        bool error;
        do
            {
            cout << "Number of cites? [0.." << maxn <<"] "; cin >> n;
            error=cin.fail() || n<=0 || n>maxn;
            if (error)
                {
                cout << "Error." << endl;
                cin.clear(); getline(cin,tmp,'\n');
                }
            }
        while (error);
        do
            {
            cout << "Number of birdspecies: [0.." << maxm <<"] "; cin >> m;
            error=cin.fail() || m<=0 || m>maxm;
            if (error)
                {
                cout << "Error." << endl;
                cin.clear(); getline(cin,tmp,'\n');
                }
            }
        while (error);
        cout << endl;
    }
void input_names(string city[maxn], string species[maxm], int n, int m)
    {
        string tmp;
        bool error;
        do
            {
            cout << "Enter the name of the cityes! (one name in one row)" << endl;
            for (int i=0; i<n; ++i)
            {
                cin >> city[i];
            }
            error=cin.fail();
            if (error)
                {
                cout << "Error." << endl;
                cin.clear(); getline(cin,tmp,'\n');
                }
            }
        while (error);
        cout << endl;
        do
            {
            cout << "Please enter the name of the species! (one name in one row)" << endl;
            for (int i=0; i<m; ++i)
                {
                cin >> species[i];
                }
            error=cin.fail();
            if (error)
                {
                cout << "Error." << endl;
                cin.clear(); getline(cin,tmp,'\n');
                }
            }
        while (error);
        cout << endl;
    }
void input_array(int x[maxn][maxm], int n, int m, string city[maxn], string species[maxm])
    {
    string tmp;
    bool error;
        cout << "Number of inspected birds: " << endl;
        for(int i = 0;i < n;++i)
            {
            cout << city[i] <<endl;
            for(int j = 0;j < m;++j)
                {
                    do
                    {
                        cout << species[j] << ": ";
                        cin >> x[i][j];
                        error=cin.fail();
                        if (error)
                            {
                            cout << "Error." << endl;
                            cin.clear(); getline(cin,tmp,'\n');
                            }
                        }
                      while (error);
                    }
                    cout << endl;
            }
            cout << "--------------------------------------------------" << endl;
    }
void max(const int x[maxn][maxm], int n, int m, string city[maxn], string species[maxm])
    {
        int bird, maxim;
        cout<< "Maximums: "<<endl;
        for (int i=0; i<n; ++i)
            {
                maxim=x[i][0];
                bird=0;
                for (int j=0; j<m; ++j)
                    {
                        if (x[i][j]>maxim)
                            {
                                maxim=x[i][j];
                                bird=j;
                            }
                    }
                cout << species[bird] << " (" << maxim << " " << city[i] << ")"<< endl;
            }
            cout << endl;
    }
void waitforkey()
    {
      system("pause");
    }
Last edited on
closed account (S6k9GNh0)
Good question:

You simply have to use a buffer large enough to handle whatever your dealing with. You have to assume a maximum size. Well, not always... Keep out for keywords like specific lengths, etc. In an internet protocol, you don't assume size for anything.

Anyways, when you don't know a certain size of the data, you have to just guess a maximum size, allocate enough space for the maximum size, and if you want to, move the data to another buffer that fits the data perfectly to reduce memory in exchange for runtime resources.

In C++, the std::string is often used a data buffer which is a bad idea for a few reasons but the main one is that its misused. You can pass a char* to a std::string but it doesn't handle it well when its 4MB of data. Just don't go overboard with it.

Also, in the case that the assumed maximum size isn't enough, you'll run into a problem called buffer overflow. You can read about it here: http://en.wikipedia.org/wiki/Buffer_overflow

Also, for terminology sake, the act of bounds checking is *compile-time* of whether the given variable(s) will fit inside of the given buffer (array). C and C++ does not have this however, it can be simulated. std::vector does this for instance.

Also, bounds checking does not always (if ever) prevent buffer overflow.
Last edited on
Use the standard library, a possible solution might look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>
using namespace std;


main( blah blah )
{
...
//at this point you know how many elements given by val1 and val2:
vector<vector<bird_t> > my_birdy_array ( val1, vector<bird_t> ( val2 ) );
...
//accesing it like an array and so on...
my_birdy_array[x][y] = amazing_bird ;

}



Other way to deal with this is making a deeper use of its dynamicity, declaring it just with:
 
vector<vector<bird_t> > my_birdy_array ;


using push_back to add rows and elements, just have a look at the vector template documentation:
http://www.cplusplus.com/reference/stl/vector/

You have there other containers that can be useful to you.

Hope I was useful. Cheers.


Btw, if you use the second approach, you may consider having rows of different lengths. I don't know if it makes any sense to your application.

Cheers
Thanks for helping!
Topic archived. No new replies allowed.