This program isn't asking for the predetermined 5 numbers from the array?
c++
Solution
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); //whoops! it's inside the for loop
}
}
- You made a mistake by putting your return inside of the for loop, this means no matter what you're only entering `score[1]` because the function will return max afterwards.
Remove the return inside your for loop and put it at the end of the function, and you should be fine.
Problem
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; } ```