15,074,181 members

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??

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++

Copy Code

#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; }

Comments

You need to replace

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`

with `if`

inside a:C++

Copy Code

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

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

please elaborate the reason for not using if's please?? and i am busy today so will learn and implement breakpoints tomorrow

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.

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.

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:

Give it a try: it may seem more complex, but it really, really isn't once you get your head around it!

Start by not using "named" variables to hold the data you read from the user: use an array instead:

C++

Copy Code

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++

Copy Code

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

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++

Copy Code

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++

Copy Code

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

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

—SA

—SA

—SA

Enter five numbers and

thenchoose ...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.