Search code examples
c++inputaddition

How to print largest number from User Input


I am to let the user input how many floating number he wants to type in. Once the floating numbers are typed in I am to have a message that says something like "The largest number you inputted is so and so".

How do I recognizing the largest number inputted by the user.

#include <stdio.h>
#include <iostream>
#include <iomanip>

using namespace std; 

int main()
{
    float count; 
    float input; 
    float large; 

    cout << "Enter the number of floating numbers you wish to input: ";
    scanf("%f", &count); 

    do
    {
        cin >> input; 
        count--; 

    }
    while(0 < count); 

    return 0;

}

Solution

  • This method is quick and clean, basically read in values the number of times specified, and each time the number is greater than the current maximum, replace max with the value read.

        int main()
        {
            int num_entries;
            float num;
            float max = 0;
            cin >> num_entries;
            while (num_entries-- > 0){
                cin >> num;
                if (num > max) {
                    max = num;
                }
            }
        }