Multi array with different data types

Is there a way of doing something simular to this?

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
//  srand(time(0));
    int amount(5);
    string text("Foo", "Bar", "Baz");
    int arr[amount][text];
}


As it looks now I get error message:
error: invalid conversion from 'const char*' to 'unsigned int'

and:
error: initializing argument 2 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'

and:
error: size of array 'arr' has non-integral type 'std::string'


I want to create an array with 5 seperate containers containing 2 integers and 1 string.

I have no idea on how to make this happen...

I also found this on http://www.cplusplus.com/forum/beginner/10377/

Apr 25, 2009 at 3:10pm

you can use this syntax:
Type array [] = { Type(argument for 1st), Type(argument for 2nd) , ... };


Could someone please explain to me how to use this example?
Last edited on
No, you can't make an array in the way you're thinking. At least not that simply. The example in the post you've given is for giving different arguments for array elements of the same type.

Hashimatsu wrote:
I want to create an array with 5 seperate containers containing 2 integers and 1 string.

I'm not quite sure I understand your needs. Would the following not suffice?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

struct Container
{
   int a, b;
   std::string s;
};

int main( int argc, char* argv[] )
{
   Container arr[5];
}
Does the struct Container allow me to save two int values and one string in each of those five? Can I do the same with public class instead of struct?

And thanks a lot! It makes sense and I think I'm beginning to understand.
Hashimatsu wrote:
Does the struct Container allow me to save two int values and one string in each of those five?

Yes.

Hashimatsu wrote:
Can I do the same with public class instead of struct?

A public class is a struct. The only difference between C++ classes and structs is that classes are inherently private, structs are inherently public.
Guess I went over my head trying to use the information you just gave me...

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
#include <iostream>

using namespace std;

class Container
{
    public:
        int a, b;
        string s;
};

class User
{
    public:
        Container arr[5];

        void create()
        {
            cout << "\nGive value 1: ";
            cin  >> arr.a;

            cout << "\nGive value 2: ";
            cin  >> arr.b;

            cout << "\nGive string:  ";
            cin  >> arr.s;
        }
};

int main()
{
    User foo;

    foo.create;
}


Am I way out there or is it possible to make this work?


EDIT:
I forgot to post the error messages... sorry

error: request for member 'a' in '((User*)this)->User::arr', which is of non-class type 'Container [5]'


and
error: statement cannot resolve address of overloaded function
Last edited on
Yeah, it's possible to make that work. A little context would help, though. What are you trying to do specifically?

I mean, I know you're trying to create an array of objects that contains string and integer data but what do they represent? Names? Ages?

EDIT: The error messages are because you're not treating arr as an array. Also, you're not calling create function correctly - you're missing the parenthesis.

Like I said, though, if you give me a bit of a better idea of what you're creating then I can advise a little more specifically.
Last edited on
Its supposed to hold account number, balance and account holder(name), and there should be five of those in each "Bank" class.

Sorry for all the noob errors in last post, my mistake :)
Now I made this work, but there is still some questionmarks... How do I adress the other four indexes of my array 'arr'?

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
#include <iostream>

using namespace std;

class Container
{
    public:
        int a, b;
        string s;
};

class User
{
    public:
        Container arr[5];

        void create()
        {
            cout << "\nGive value 1: ";
            cin  >> arr[0].a;

            cout << "\nGive value 2: ";
            cin  >> arr[0].b;

            cout << "\nGive string:  ";
            cin  >> arr[0].s;
        }
};

int main()
{
    User foo;

    foo.create();
}
You could pass an index to the create function and operate on the account at that index. All you'd need to do is ask for input in main.

Is the Bank class going to have any other functionality?

Also, have you considered using vectors?
Topic archived. No new replies allowed.