Hello everyone.

I am working on my final project at university for C++, and I got stuck a little.
I have an array inside an array. And I want to access that inner array's member and print it with cout.

Here is the code:

void standings(userClub uc){
	cout << endl;
	cout << "First Division" << endl;

	int table[10];
	
	for(int i=0; i<=9; i++){
		table[i] = coms[i].pts;
	}

	table[9]=uc.pts;
	sort(table, table+10);

	int y=9;
	while(y>=0){
		cout << table[coms[y].name] << "\t"; // HERE I WANT TO ACCESS
		cout << table[y] << endl;            // coms array
		y--;
	}
}

Please have in mind that I'm not a pro... Thanks x)

Recommended Answers

All 3 Replies

coms[y].name is a string. How can you give cout<<table[coms[y].name] ?
It would be right if you give an integer value present in coms.
Please post your full code.

You need a way to point back to the coms index after sorting table . Rather than a sorted array, I'd go with a map:

void standings(userClub uc){
    map<int, int> table;
	
    for(int i=0; i<9; i++){
        table[coms[i].pts] = i;
    }

    table[uc.pts]=9;

    map<int, int>::const_iterator it = table.begin();
    map<int, int>::const_iterator end = table.end();

    cout << "\nFirst Division\n";

    while(it!=end){
        cout << coms[it->second].name << "\t" << it->first << endl;
        ++it;
    }
}

Your algorithm is somewhat confusing with the lack of information, I can't guarantee the correctness of that example. Though the basic idea should be sound. Ideally I'd store the coms[i] object as the value for the map, but I don't know what the type is...

@Arbus: oh, yeah. You're right. And I've been searching for solution for a few hours...

@Narue: it is probably a good idea, but it's a bit too advanced for my course..
coms[] is a class type.

Here is more info about the code:

Club coms[9]; // Club is a class
userClub uc; // userClub is a class
coms[].pts is an integer;
coms[].name is a string;
coms[]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.