Okay let me rephrase. You didn't ask a question, you made a statement. What's the question? Neither myself or anyone else here will write the program for you, this is an obvious homework problem. What do you have so far? I will say that you obviously need to have five inputs, which can be accomplished either in a loop or simply by having five input statements. Then as you get them, you have a variable that stores the largest value. You compare each value as it is received and if it's larger than the value that you already have stored as the largest, you replace that largest value with the new one. I'd suggest an if statement for that. Good luck and please post some code here if you need additional help.
#include <iostream>
int main()
{
int biggest_so_far ; // biggest number entered till now
int number_entered ; // number entered by the user
std::cin >> number_entered ; // 1
biggest_so_far = number_entered ; // biggest_so_far is the first number
// repeat four more times
// (if you have learnt about loops, use a for loop instead)
std::cin >> number_entered ; // 2
if( number_entered > biggest_so_far ) // if this number is larger than biggest_so_far
biggest_so_far = number_entered ; // this is the biggest_so_far
std::cin >> number_entered ; // 3
if( number_entered > biggest_so_far ) // if this number is larger than biggest_so_far
biggest_so_far = number_entered ; // this is the biggest_so_far
std::cin >> number_entered ; // 4
if( number_entered > biggest_so_far ) // if this number is larger than biggest_so_far
biggest_so_far = number_entered ; // this is the biggest_so_far
std::cin >> number_entered ; // 5
if( number_entered > biggest_so_far ) // if this number is larger than biggest_so_far
biggest_so_far = number_entered ; // this is the biggest_of the lot
// print our the biggest number
std::cout << "biggest number entered was: " << biggest_so_far << '\n' ;
}
#include<iostream>
usingnamespace std;
int main()
{
int largestnum=0;
int num[5];
cout<< "Please enter five numbers : "<<endl;
for(int i=0;i<5;i++)
{
cin>> num[i];
if(num[i]>largestnum)
largestnum=num[i];
}
cout<< "The largest number is: " <<largestnum <<endl;
return 0;
}
#include <iostream>
int main()
{
int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0;
std::cout << "Please enter five numbers : " ;
std::cin >> num1 >> num2 >> num3 >> num4 >> num5;
int max = num1;
if ( max < num2 ) max = num2;
if ( max < num3 ) max = num3;
if ( max < num4 ) max = num4;
if ( max < num5 ) max = num5;
std::cout << "The largest number is: " << max << std::endl;
return 0;
}
#include <iostream>
#include <initializer_list>
int main()
{
int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0;
std::cout << "Please enter five numbers : " ;
std::cin >> num1 >> num2 >> num3 >> num4 >> num5;
int max = num1;
for ( int x : { num2, num3, num4, num5 } )
{
if ( max < x ) max = x;
}
std::cout << "The largest number is: " << max << std::endl;
return 0;
}
#include<iostream>
usingnamespace std;
int main()
{
int largestnum=0;
int index=0;
cout<<"How many numbers do you want to compare?: ";
cin>>index;
int num[index];
cout<< "Please enter "<<index<<" numbers."<<endl;
for(int i=0;i<index;i++)
{
cin>> num[i];
if(num[i]>largestnum)
largestnum=num[i];
}
cout<< "The largest number is: " <<largestnum <<endl;
return 0;
}
#include <iostream>
#include <algorithm>
int main()
{
auto getInt = [](int i=0) { return std::cin >> i, i; };
std::cout << "Please enter five numbers : ";
int max = std::max( { getInt(), getInt(), getInt(), getInt(), getInt() } );
std::cout << "The largest number is: " << max << "\n";
}
int a(0); //Not constant. Can be changed.
constint b(45); //Constant. Cannot be changed.
constexprint c(1005); //Also constant. Also cannot be changed. C++11 only
Amongst all the fancy solutions, tntxtnt's 'moar' is the clear winner.
With one change required:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <algorithm>
int main()
{
// default arguments are not allowed for lambda expressions
// auto getInt = [](int i=0) { return std::cin >> i, i; };
constauto getInt = [] { int i = 0 ; std::cin >> i ; return i ; } ;
std::cout << "Please enter five numbers : ";
int max = std::max( { getInt(), getInt(), getInt(), getInt(), getInt() } );
std::cout << "The largest number is: " << max << "\n";
}
Chriscpp's 'More flexible' requires c++14 (compile with -std=c++1y).