I have a bug in a array i cant find.


what have i done wrong ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<string>
using namespace std;
main(){

string personlist[10][2] = {

{"person1",1},
{"person2",2},
{"person3",3},
{"person4",4},
{"person5",5},
{"person6",6},
{"person7",7},
{"person8",8},
{"person9",9},
{"person10",10}};

	return 0;
}
you have an array with the type string. The second value is not a string
You are defining personlist to be an array of 10 arrays of 2 strings. 1, 2, ..., 10 are not strings.
string personlist[10][2]; means a multidimensional array of strings.

You can store 20 strings in it. Where exactly do you think you can store the integers 1, 2 ... 10? There's no place for them.

What you appear to want to do is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <utility>

int main()
{
    std::pair<std::string, int> personlist[10] {
        {"person1", 1},
        {"person2", 2},
        {"person3", 3},
        {"person4", 4},
        {"person5", 5},
        {"person6", 6},
        {"person7", 7},
        {"person8", 8},
        {"person9", 9},
        {"person10", 10}
    };

    for (const auto &p: personlist)
        std::clog << p.first << ' ' << p.second << '\n';
}
person1 1
person2 2
person3 3
person4 4
person5 5
person6 6
person7 7
person8 8
person9 9
person10 10


Your compiler needs to be C++11 compliant to compile the above code.
Maybe it will be better to define the following array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<string>
#include <utility>

using namespace std;
int main()
{
   const int N = 10;
   std::pair<std::string, int> personlist[N] = 
   {
      { "person1", 1 },
      { "person2", 2 },
      { "person3", 3 },
      { "person4", 4 },
      { "person5", 5 },
      { "person6", 6 },
      { "person7", 7 },
      { "person8", 8 },
      { "person9", 9 },
      { "person10", 10 }
   };

   return 0;
}
He he Vlad! Are you copying my reply again? ;)
Topic archived. No new replies allowed.