How to write program in C++

Write your question here.
I want to write the code which ask for five numbers to enter, and show the biggest number of five entert

What's the question?
I'm new in C++
I need to write program which is asking to put 5 numbers and then program can find which is biggest number of 5 entert
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.
dear friend randisking
Her is what am trying to due simply dosent work i don't know what to do

# include <iostream>
using namespace std;
int main ()
{
int num1, num2, num3 ,num4 ,num5 ,;


cout<< "Please enter five numbers : " ;
cin>> num1;
cin>> num2;
cin>> num3;
cin>> num4;
cin>> num5;


if(num1 > num2 && num1 > num3 > num4 > num5 )
{

cout<< "The largest number is:" <<num1 <<endl;

}
else
{
if(num2 > num1 && num2 > num3 > num4 > num5)
{
cout<< " The largest number is:" <<num2 <<endl;
}
else
{
if (num3 > num1 && num3 > num2 >num4 > num5)

{
cout<< " The largest numberis:" <<num3 <<endl;
}

}

system("pause");

return 0;

}

> num1 > num3 > num4 > num5

This doesn't work the way you think it works.

1
2
// if(num1 > num2 && num1 > num3 > num4 > num5 )
if( num1 > num2 && num1 > num3 && num1 > num4 && num1 > num5 ) // this does 


Or:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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' ;
}
Hello zeosmani, welcome to C++.com!

Glad to see you.

How much do you know?

JLBorges' post should help you very much!

If there is anything that I can do to help, just send me a PM!

Also, before posting, please read this:
http://www.cplusplus.com/search.do?q=using+code+tags
Thanks!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace 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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>

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;

   std::cout << "The largest number is: " << std::max( { num1, num2, num3, num4, num5 } ) << std::endl;

   return 0;
}
And without std::max

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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;
}
And one more example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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;
}
Last edited on
More flexible ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace 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;
}
@Chriscpp

More flexible ;)


It is not more flexible. It is simply an invalid C++ code because in C++ the size of an array shall be a constant expression.
moar

1
2
3
4
5
6
7
8
9
10
#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";
}
I think it is a constant so to say because it's assigned only once by a variable and will never be changed in this case.
@Chriscpp
I think it is a constant so to say because it's assigned only once by a variable and will never be changed in this case.


Constant variables are those that have the qualifier const or the specifier constexpr.
I think it is a constant...

To illustrate what vlad said:
1
2
3
int a(0); //Not constant. Can be changed.
const int b(45); //Constant. Cannot be changed.
constexpr int c(1005); //Also constant. Also cannot be changed. C++11 only 


Edit:
Wrote out "const".
Last edited on
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; };
    const auto 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).
Dear All,
Thank you very much for this exelent work you did,I told you am new in this field, C++
Topic archived. No new replies allowed.