DEV Community

crhodes
crhodes

Posted on

C++ Array List

I am trying to solve the algorithm of finding and displaying the largest age in the array. This is what I have so far.

#include <iostream>

using namespace std;

int main()
{
    int brothers[5] = {6,3,24,12,22};
    int i = 0;
    int eldestAge = 0;

    for (int j = 0; j < brothers[i]; ++j)
        {
            if (eldestAge < brothers[i])
            {
                eldestAge = brothers[i];
            }
            else
            {
                // do nothing
            }
        }

        cout << "Eldest Age is = " + eldestAge << endl;

        system("pause");
}

However the output doesn't display anything except the quote. Please let me know what am I doing wrong.

Top comments (2)

Collapse
 
mitjmcc profile image
Mitch McClellan

It looks like you're trying to add a string and an int, when C++ does not automatically cast the int like say C# does.

Following this, you just need to change

cout << "Eldest Age is = " + eldestAge << endl;

to

cout << "Eldest Age is = " << eldestAge << endl;
Collapse
 
crhodes2 profile image
crhodes • Edited

Oh wow! Such stupid mistake I've made, lol! Thanks!
Now could you give me any ideas as to why the largest number is not displaying? What am I doing wrong? :/
Edit: Nevermind! Solved it!