Click here to Skip to main content
15,067,251 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
So I am working on a menu driven program which is

Q)Write a C++ Menu driven program that allows a user to enter five numbers and then
choose between findings the smallest, largest, sum or average. Use else if statement
to determine what action to take.
just else if allowed no other loop and no arrays

and when I run largest of 5 choice with integers 1,2,3,4,5 it gives 2 as answer what's the problem can someone point out??

C++
#include<iostream>
using namespace std;

int main()
{
      int num1=0,num2=0,num3=0,num4=0,num5=0,choice=0;
      int sum=0,avg=0,largest,smallest;

    re_enter_label:cout << "\t\t\t''MENU''\n"<<endl;
    cout <<"1.  Find Smallest of 5"<<endl;
    cout <<"2.  Find Largest of 5"<<endl;
    cout <<"3.  Find Sum of 5"<<endl;
    cout <<"4.  Find Average of 5"<<endl;
    cout <<"Choice: ";
    cin >> choice;
    cout <<"Enter 5 numbers : ";
    cin>>num1>>num2>>num3>>num4>>num5;

            if(choice==1)
{
        smallest=num1;

        if(num2 < smallest )
        { // compare num2 to smallest,
         smallest = num2;
        }
        else if(num3 < smallest )
        {
		smallest = num3;
        }
        else if(num4 < smallest )
        {
		smallest = num4;
        }
        else if(num5 < smallest )
        {
		smallest = num5;
        }

	cout <<"Smallest of all integers: " <<smallest<<endl;

	return 0;
}
    else if(choice==2)
    {
        largest=num1;

        if(num2 > largest )
        {
         largest = num2;
        }
        else if(num3 > largest )
        {
		largest = num3;
        }
        else if(num4 > largest )
        {
		largest = num4;
        }
        else if(num5 > largest )
        {
		largest = num5;
        }

	cout <<"Largest of all integers: " <<largest<<endl;

    }
    else if(choice==3)
    {
         sum = ( num1 + num2 + num3 + num4 + num5 );
         cout <<"Sum of the given numbers: "<<sum<<endl;
    }
    else if(choice==4)
    {
    avg = ( ( num1 + num2 + num3 + num4 + num5 ) / 5 );
    cout <<"Average of the given numbers: "<<avg<<endl;
    }
    else if(choice>5)
    {
            goto re_enter_label;
    }
    return 0;
}
Posted
Comments
   
Such problems are solved by using the debugger.
—SA
Farhan_Karim 7-Mar-15 10:53am
   
Sorry but I am a beginner and we have not been taught about breakpoints in class ,but I'll ask the instructor for that tomorrow.Thanks
   
People with brain don't need to be "taught about breakpoints" and asking instructor. Just start using the debugger, look through its menu and read help when something is not clear. Being beginner makes little difference, it's merely mean you did not do it yet, but, say, tomorrow you will begin...
—SA
Farhan_Karim 7-Mar-15 11:20am
   
Lol, busy schedule ahead for today, will learn about breakpoints tomorrow ;)
   
Great.
—SA
Jochen Arndt 7-Mar-15 3:42am
   
Just at tip: Read your assignment

Enter five numbers and then choose ...
Farhan_Karim 7-Mar-15 10:53am
   
elaboration please
Jochen Arndt 7-Mar-15 11:05am
   
Your solution asks first for the method and then for the numbers. But the assignment says: "Enter five numbers and then choose ...".

So a possible implementation would do all necessary calculations while the numbers are entered and then print the selected result.

I wrote my commment to show you this misinterpretation of the assignment.
Farhan_Karim 7-Mar-15 11:16am
   
oh,ok got it :D

Your logic is wrong. If 2 > 1 is true then the largest = 2, the rest of else if will be skipped.
   
v2
You need to replace else if with if inside a:
C++
if(choice==1)
{
   // Use only if-s
}
else if(choice==2)
{
   // Use only if-s
}

But as Sergey mentioned this would be a good opportunity for you to learn how to debug your code, so put the break point at else if(choice==2) and reproduce your issue again. After the program's execution reaches the break point go step by step and observe what happens (you should notice that if(num2 > largest ) is going to be executed but the rest of the else if are skipped.
   
Comments
Farhan_Karim 7-Mar-15 11:26am
   
Thanks, worked :D
please elaborate the reason for not using if's please?? and i am busy today so will learn and implement breakpoints tomorrow
Mario Z 8-Mar-15 4:39am
   
Well the reason is the following, you see when you have 'if' statement + multiple 'else if' statements only one of these statements will execute. To be more specific the first one which has a true condition in its brackets will execute.

So when you are choosing what to do (Find Smallest, Find Largest, Find Sum or Find Average) than you need 'if' and multiple 'else if' because you want only one choice to be executed.

But when you are looking for the largest or the smallest number, then you want to check every number.

Now for your given situation here is what happens:
- first you choose 2. task (Find Largest of 5)
- then you enter your 5 numbers (1 2 3 4 5)
- the program goes to 'if(choice==1)' which is false, so it goes to next 'else if(choice==2)' which is true
- now inside of it you have 'largest = num1;' so largest is '1'
- next comes 'if(num2 > largest )' which is 'if(2 > 1)' and that is true so 'largest = num2;' is executed and largest is now '2'
- after this is done all the rest of the 'else if' statements are skipped and the program continues with 'cout <<"Largest of all integers: ...'

That is why you needed to replace 'else if' with 'if', so that program doesn't skip any numbers checking.
Farhan_Karim 11-Mar-15 2:14am
   
Thanks a lot for clearing that up for me. :)
What Peter says in Solution 1 is true, but doesn't really address the problems you are having, and will have with the way you are doing things.

Start by not using "named" variables to hold the data you read from the user: use an array instead:
C++
int data[5];
for (i = 0; i < 5; i++)
   {
   cin>>data[i];
   }
Then finding the largest, or smallest, sum and average are all just very similar (and very simple) loops:
C++
sum = 0;
for(i = 0; < < 5; i++)
   {
   sum += data[i];
   }
And if your task changes to want ten numbers instead of five the changes become trivial.

Give it a try: it may seem more complex, but it really, really isn't once you get your head around it!
   
Comments
Farhan_Karim 7-Mar-15 11:22am
   
for loops and arrays are not taught yet but will try your solution after the topic is covered next week, Thanks
Ok so i replaced the else if's in the code of smallest and largest and it worked but why wasn't it working before can someone explain?

C++
else if(choice==2)
   {
       largest=num1;

       if(num2 > largest )
       {
        largest = num2;
       }
       if(num3 > largest )
       {
       largest = num3;
       }
       if(num4 > largest )
       {
       largest = num4;
       }
       if(num5 > largest )
       {
       largest = num5;
       }

   cout <<"Largest of all integers: " <<largest<<endl;

   }


C++
       if(choice==1){
{
        smallest=num1;

        if(num2 < smallest )
        { // compare num2 to smallest,
         smallest = num2;
        }
        if(num3 < smallest )
        {
		smallest = num3;
        }
        if(num4 < smallest )
        {
		smallest = num4;
        }
        if(num5 < smallest )
        {
		smallest = num5;
        }

	cout <<"Smallest of all integers: " <<smallest<<endl;

	return 0;
}
}
   
Comments
Philippe Mori 7-Mar-15 17:08pm
   
Do it on paper. With only 5 numbers, it is really not difficult to figure out why that code works while thye original one do not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900