# Thread: how to find greatest input

1. ## how to find greatest input

quick question...in the loop below, what kind of code is neccessary to find the largest input entered by the user?

from my understanding, every iteration the variable changes and that is why I'm having a dilema?

Code:
```//PURPOSE: This program reads an arbitrary number of real numbers.
//The input sequence is ended by a sentinel value.
//The program then reports the quantity, sum, and average
//of all numbers entered; how many of them are positive negative or
//zero;how many seven and elevens were entered, whether positive or
//negative; and the largest and smallest numbers entered.
//*********************************************************************

#include<iostream.h> // for I/O tools

int main()
{
//declaring variables
double num; //number the user will enter

//declaring counter variables to count number of times
//certain events occur.
int  count;
int  countpositives;
int  countnegatives;
int  countzeros;
int  countpositivesevens;
int  countnegativesevens;
int  countpositiveelevens;
int  countnegativeelevens;

//variables that will be assigned the total numbers
int  totalsevens;
int  totalelevens;

//varibles for largest and smallest number entered by user
float largestnumber;
float smallestnumber;

float sum;      //Computes the sum of user inputs
float average;  //Computes the average of user inputs

//Setting these counts to zero because they all equal zero at
//this point in the program and so that the number of times the
//loop body is true corresponds to the number of the particular
//item the program keeps track of.
count=0;
countpositives=0;
countnegatives=0;
countzeros=0;
countpositivesevens=0;
countnegativesevens=0;
countpositiveelevens=0;
countnegativeelevens=0;

//So that my following sum assign.
//statements keep track of sum.
sum=0;

//Describing the program to user and also prompting the user.
//Also telling the user to input sentinel value when done.
cout<<"This program reports things about an arbitrary ";
cout<<"number of real numbers that\nyou will be asked " ;
cout<<"to enter."<<endl<<endl;
cout<<"Enter any number or real numbers.";
cout<<"  Following the last value you input,"<<endl;
cout<<"input 45.8931. Thank you!"<<endl<<endl;

cin>>num;
//If statements within a loop. If the input doesn't equal
//the sentinel value,45.8931, the sum will be computed and
// count variable value will increment 1.
//Next, the If statements test to see if an event is true, and
//if so it increases the count of the variable name for that event
//Finally, we get the next user input just before the ned of the
//loop.
while(num!=45.8931)
{

sum=sum+num;
count++;
if(num==0)
countzeros++;
if(num>0)
countpositives++;
if(num<0)
countnegatives++;
if(num==7)
countpositivesevens++;
if(num==-7)
countnegativesevens++;
if(num==11)
countpositiveelevens++;
if(num==-11)
countnegativeelevens++;

cin>>num;

}

average=sum/count; //computing the average

//Computing total sevens and elevens
totalsevens=countpositivesevens+countnegativesevens;
totalelevens=countpositiveelevens+countnegativeelevens;

//Formatting the output; reporting to the user the
// things about the number the user entered.
cout<<endl;
cout<<"The number of real numbers you entered is ";
cout<<count<<"."<<endl;
cout<<"The sum of them is "<<sum<<"."<<endl;
cout<<"The average "<<average<<"."<<endl<<endl;
cout<<"You entered in "<<countpositives<<" positive ";
cout<<"numbers, "<<countnegatives<<" negative numbers, ";
cout<<"and "<<countzeros<<" zeros."<<endl<<endl;
cout<<"You entered in "<<countpositivesevens;
cout<<" positive sevens and "<<countnegativesevens;
cout<<" negative sevens, making a total of\n";;
cout<<totalsevens<<" sevens,whether positive or ";
cout<<"negative.";
cout<<"  You entered in "<<countpositiveelevens;
cout<<" positive elevens and "<<countnegativeelevens;
cout<<" negative elevens, making a total of ";
cout<<totalelevens<<" elevens, whether positive ";
cout<<"or negative.";

return 0;
}```
Code tags added by Hammer.

2. I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you

For example:

Without code tags:

for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}

With Code Tags:
Code:
```for(int i=0;i<5;i++)
{
cout << "This code is easy to read";
}```
This is of course a basic example...more complicated code is even easier to read with code tags than without.

Hammer added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.

Good Luck,
Kermi3

3. ## Re: how to find greatest input

Originally posted by m712
quick question...in the loop below, what kind of code is neccessary to find the largest input entered by the user?

from my understanding, every iteration the variable changes and that is why I'm having a dilema?

First - check your PMs (Private Messages)

Now to the code.... to determine the highest value, simply have a variable called something like "Highest", initialise it to 0, when the user enters a number, compare it to Highest, and if it's greater, update Highest to have the new value. At the end, simply print the value of Highest...... job done.

4. Actually, all you have to do is introduce another variable like 'double bigNum = 0;' and compare it against 'num'.

For example:
Code:
```#include <iostream>
#include <cstdlib>

inline void pause() { system("PAUSE"); }

int main(void)
{
double num = 0, bigNum = 0;

while ( num != 45.8931 ){
std::cout << "Enter a number: -> ";
std::cin >> num;

if ( num > bigNum)
bigNum = num;
}

std::cout << bigNum << std::endl;

pause();
return 0;
}```
-Skipper

P.S. Thanks, Hammer. My timing, as usual, is impeccable!

5. Hi,
Do you mean something like this:
Code:
```int number, greatest=1;     //you have to initialize greatest according to what you need in your program
for ( int=0; i<5; i++)
{
cin >> number;
if ( number > greatest )
greatest = number;
}```
I hope this helps.
you can first read a value of number then make greatest = number then enter the loop.

Popular pages Recent additions