Printing the maximum numbers after user's input using Array? guys I'm struggling with this program.

here's what I wrote but it's not working..

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int maxArray[10];
    int maxValue=0;
    for(int j=0; j<4; j++)
    {
        cout<<"Enter a number: ";
        cin>>maxArray[j];

         if(maxArray[j]>maxValue)
        {
            maxValue=maxArray[j];
          cout << "The highest value is: " << maxValue;
        }   

    }  

    getch();
    return 0;

}

Dani AI

Generated

The original post has a few small but important problems that explain the strange behavior: the array is declared as maxArray[10] but only four slots are read, the program prints the current max inside the input loop (so it prints repeatedly), maxValue is initialized to 0 (so a set of all-negative inputs will give the wrong result), and conio.h/getch() are nonstandard and unnecessary for basic examples. correctly suggested separating input from evaluation, and showed a two-loop fix. A more robust, modern approach avoids magic sizes, validates input, and finds the maximum safely.

#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>

int main() {
    std::size_t n;
    std::cout << "How many numbers? ";
    if (!(std::cin >> n) || n == 0) return 0;

    std::vector<int> v;
    v.reserve(n);
    for (std::size_t i = 0; i < n; ++i) {
        int x;
        while (!(std::cin >> x)) {                     // simple input validation
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid integer, try again: ";
        }
        v.push_back(x);
    }

    int maxv = *std::max_element(v.begin(), v.end());   // handles negatives correctly
    std::cout << "Maximum value: " << maxv << '\n';
    return 0;
}

Notes and quick troubleshooting:

  • Initialize the running maximum from the first input (or use std::numeric_limits<int>::min()), not from 0, to handle negative numbers correctly.
  • If using a fixed-size array, ensure the input loop uses the intended bound (or track how many values were actually entered).
  • If a single-pass solution is preferred, update a max variable while reading but print only once after the loop.
  • Avoid conio.h/getch() for portable code; let the program terminate normally or use standard I/O techniques to pause if necessary.

Recommended Answers

All 2 Replies

You have an array of 10, but the loop counter only counts to 4. Any reason for that?

here's what I wrote but it's not working..

what's not working? It should print something on the screen every time you enter a number that is greater than any previous number entered.

If all you want the program to do is tell you only one time what the highest number is, you will need another loop. First loop like you have it but delete line 10. After that loop finished write another loop that scans all the array looking for the largest number. When that loop is finished then print the largest value like you did on line 10.

so you program should do this:

  • Loop to enter data into the array
  • Loop to find largest value in the array
  • Print the largest value

Try this:

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int maxArray[10];
    int maxValue=0;
    for(int j=0; j<4; j++)
    {
        cout<<"Enter a number: ";
        cin>>maxArray[j];
    }
    for (int j = 0; j < 4; j++)
    {
        if(maxArray[j]>maxValue)
        {
            maxValue=maxArray[j];
        }   
    }
    cout << "The highest value is: " << maxValue << endl;
    getch();
    return 0;
}

Yes, you could evaluate the maxvalue in the first loop, but I like to keep my input, evaluation, and output loops separate. Just personal preference, and it helps me to keep "domains of responsibility" distinct.

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.