This program isn't asking for the predetermined 5 numbers from the array?

Posted by user1801781 on Stack Overflow See other posts from Stack Overflow or by user1801781
Published on 2012-12-04T04:50:54Z Indexed on 2012/12/04 5:03 UTC
Read the original article Hit count: 129

Filed under:

Okay, so this question is difficult to state.

I'm a beginner at C++, and I rarely run into problems with these simple assignments, but something is majorly wrong here and I cannot identify it. I've been trying for hours.

This program is supposed to read 5 numbers from an array that the user enters, and then print the largest one. (I know it's easier to just write a for-loop, but our professor wanted us to call a function).

The only problem is that instead of asking for 5 numbers, it asks for 2. It works other than that, I JUST NEED IT TO ASK FOR 5 NUMBERS. haha.

Your input would be greatly appreciated. I aspire to be a programmer one day, so don't be afraid to go harsh on me.

#include <iostream>
using namespace std;

int largest_number(int score[], int max)
{ 
for (int i=1; i<5; i++)
{
    cin >> score[i];
    if(score[i] > max)
        max=score[i];

return (max);
}

}

int main ()
{
int score[5], max, z;

cout << "Enter 5 numbers: " <<endl;
cin >> score[0];
max = score[0];


z = largest_number(score, max);

cout << "The largest number is: " << z <<endl;

system("pause");
return 0;
}

© Stack Overflow or respective owner

Related posts about c++