well,this is my college assignment question.the question ask us to write a program that can compare 3 number,who is the biggest and smaller.the output should be like this:

Enter 3 numbers.
Number1:
Number2:
Number3:

Main Menu
1)Find the biggest value among the 3 value you have enter juz now.
2)Find the smallest value among the 3 value you have enter juz now.
3)Exit program

Here are my coding for the upper part.

#include<iostream.h>
void main(){

int num1,num2,num3,x,max;

cout<<"Enter 3 numbers."<<endl;
cout<<"Number1:";
cin>>num1;
cout<<"Number2:";
cin>>num2;
cout<<"Number3:";
cin>>num3;

cout<<"         Main Menu"<<endl
<<"1)Find and Display Biggest Number"<<endl
<<"2)Find and Display Smallest Number"<<endl
<<"3)Exit program"<<endl;
cin>>x;

if(x==1){
if(num1>num2 && num1>num3){
max=num1;
cout<<max;
}else if(num2>num1 && num2>num3){
max=num2;
cout<<max;
}else if(num3>num1 && num3>num2){
max=num3;
cout<<max;

what i want to ask is the lower part of this question.start from if statement there.is that correct?inside the if statement still can have if statement?

I think a switch statement would be more efficient here.

huh....at lower part there need to check 2 condition right?1 is for main menu ones.and another 1 is to check the 3 value.so,what i want to ask is how should i use the those selection statement.i mean the arrangement.which 1 1st.and how to put.T^T

I think what you have is a reasonable start. You have the right idea, using nested if statements is one way to do it. I think that papanyquiL is correct that a switch()... case statement would be more tidy (for deciding which choice the user has made). I would probably go for a structure like:

do{
    std::cout "Enter your choice: ";
    std::cin >> choice;

    switch(choice){
    case 1:    max(num1, num2, num3);
               break;
    case 2:    min(num1, num2, num3);
               break;
    case 3:    break;
    default:   badChoice();
               break;
    }
}while(choice != 3);

Then you would make functions to calculate and display the max and min etc.

looks like u misunderstand what i code up there.int max up there mean biggest value.for example,i enter 1,2,3 at 1st,then the main menu come out.then,i enter 1,after that,it will find the biggest value and that is 3.sorry for my lousy English.
can u help me check is that any wrong in my switch statement below.i cant get the biggest value.

switch(x==1){
case 1:num1>num2 && num1>num3;
cout<<num1;
break;
case 2:num2>num1 && num2>num3;
cout<<num2;
break;
case 3:num3>num1 && num3>num2;
cout<<num3;
break;
default:cout<<"You have enter at least 2 same number.";
break;
};

No, I'm afraid you have misunderstood. But don't worry, that's why we have this kind of forum :)

The switch()... case statement is used (in this case) to decide which path to take; to calculate the minimum, maximum or to exit. It is not, as you have it, for deciding which number is the maximum. You should use a function to decide which number is actually the max, min or whatever.

OK, do you understand about defining your own functions?

A function can be defined by you in your code to do certain things, for example in your case it could "return the maximum of three numbers. When you define a function you need to tell the computer three things:

  1. What kind of variable is returned by the function when it has done the thing that it was supposed to do. For example, an int , char or double .
  2. What is the name of the function, so that you can write it in your code and the compiler will know what you want it to do.
  3. The arguments of the function. These are the variable that you give to the function so that it can do its job. In your case, the three numbers that the user entered (which are all of type int ).

So, you might want a function called max that takes three integers as arguments and returns and integer, which would be the maximum of the three arguments. We would declare this function at the top of the code, like this:

#include <iostream>

int max(int, int, int);  /* Declaration of max function */

int main(){

/* The rest of your code goes here */

The function declaration starts with the return-type (the kind of variable that's returned by the function. Sometime a function doesn't return a variable at all, e.g. if it just prints a message to the screen, in this case we use void as the return-type). In this case the return type is an int , but that's just for this example, it could be any variable type, depending on what you want the function to do. Then there is the function name, max and finally, in round brackets after the name is the argument list. Again, here they're all int but that's just for this function, they could be any kind of variable type.

This line doesn't actually calculate the maximum, it just warns the compiler that you're going to make a function with these details at some point. The next thing is to make a function implementation, which is where you actually do the calculations. The best part is that you have already written a perfectly good implementation in your code, so we can just go ahead and copy that into the implementation of max . The implementation can go anywhere outside main() and below the declaration of the function, it can even go in a completely different file! A standard place to put the implementation (in a small program like this) is after main() . So, now your program might look like:

#include <iostream>

/* The function declaration, again */
int max(int, int, int);

int main(){
   /* Insert your bit for getting the user to enter the three numbers here... */
   /* ... and to print the menu */

   /* Use a do... while loop to keep repeating the program until the user asks to stop */
    do{
        int result;   /* use this to store the result of the min/max calculation */
        int choice;   /* Use this to store the user's menu choice                */

        /* ask the user for a choice from the menu */
        std::cout "Enter your choice: ";
        std::cin >> choice;

        /* Use a switch()... case to process the user's menu choice */
        switch(choice){
            /* They've chosen '1', so calculate the max */
            case 1:    result = max(num1, num2, num3);
                       std::cout << result << std::endl;
                       break;
            /* They've chosen '2', so calculate the min */
            case 2:    result = min(num1, num2, num3);
                       std::cout << result << std::endl;
                       break;
            /* They've decided to exit, so maybe print a "goodbye" message */
            case 3:    exitMessage();
                       break;
            /* They've made a choice that doesn't exist, so maybe print a message */
            /* That reminds them of the possible choices                          */
            default:   badChoice();
                       break;
        }
    }while(choice != 3);   /* If the user chose '3' then they wanted to exit the program, otherwise carry on looping! */

    /* Done! */
    return 0;
}
/* MAIN FINISHES HERE !               */
/* NOW FUNCTION IMPLEMENTATIONS START */

/* Implementation of the max function, notice the arguments have names now, as well as types */
int max(int num1, int num2, int num3){
    int max;   /* This is going to be the value that we return */
    /* Now find the maximum, just like you did before */
    if(num1>num2 && num1>num3){
        max=num1;
    }else if(num2>num1 && num2>num3){
        max=num2;
    }else if(num3>num1 && num3>num2){
        max=num3;
    }

    /* Once you've decided the maximum, you 'return' it                    */
    /* This has the effect of inserting this number in the place where you */
    /* originally called the function in the main()                        */
    return max;
}

This code will not compile, since you still need to make functions for min() , exitMessage() and badChoice() (declarations and implementations), but hopefully you get the idea: functions help keep code tidy and reduce the need to repeat yourself.

ok,i try to understand the coding above and i come out wif the the thing like this.

#include<iostream.h>
int max(num1,num2,num3);
int min(num1,num2,num3);
void main(){

cout<<"Enter 3 numbers."<<endl;
cout<<"Number1:";
cin>>num1;
cout<<"Number2:";
cin>>num2;
cout<<"Number3:";
cin>>num3;

cout<<"         Main Menu"<<endl
<<"1)Find and Display Biggest Number"<<endl
<<"2)Find and Display Smallest Number"<<endl
<<"3)Exit program"<<endl;

do{
int result;
int choice;
cout<<"Enter your choice:";
cin>>choice;

switch(choice){
case 1:result=max(num1,num2,num3);
cout<<result<<endl;
break;
case 2:result=min(num1,num2,num3);
break;
case 3:
cout<<"Goodbye";
break;
default:cout<<"Invalid Entry";
break;
};
while(choice!=3);
}
int max(num1,num2,num3){
int max;
if(num1>num2 && num1>num3){
max=num1;
}else if(num2>num1 && num2>num3){
max=num2;
}else if(num3>num1 && num3>num2){
max=num3;
}
return max;
}
int min(num1,num2,num3){
int min;
if(num1<num2 && num1<num3){
min=num1;
}else if(num2<num1 && num2<num3){
min=num2;
}else if(num3<num1 && num3<num2){
min=num3;
}
return min;
}

since i using void main.did i still need to write the return max or return min?and what is mean by std?our lecturer haven't teach about that yet,what is it use for?

since i using void main.did i still need to write the return max or return min?and what is mean by std?our lecturer haven't teach about that yet,what is it use for?

  1. You should use int main() , not void main() , see here
  2. You should use #include <iostream> not #include<iostream.h> . Using <iostream.h> means that you're not using the std namespace, google for "C++ namespace" and I'm sure you will get lots of pages. If you use <iostream> you will get an error saying that the functions cout and endl aren't recognised. In this case, you have to use std::cout and std::endl etc.
  3. Carefully compare the code you posted with the code that I posted. There are a number of important differences (apart from the ones that I mentioned above). Understand these and fix them and you might have more luck :)

Apart from these issues, your program is nearly done.

Also, if you want to understand more about what happens when you call functions, I made another post here. It's mostly about why you shouldn't call main() , but it has a description of what we mean when we say a function "returns".

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.