Creating a menu Hey all, I\'m new to C++ I am trying to acomplish the following:
ID: 671024 • Letter: C
Question
Creating a menu
Hey all, I'm new to C++ I am trying to acomplish the following:
Make a menu that has 5 options
1.Sum of 5 numbers
2. Mean of 5 numbers
3. Max of 5 numbers
4. Min of 5 numbers
5, Exit
After making a selection the user is prompted to enter 5 numbers. Here is my attempt,
#include <iostream>
using namespace std;
int main()
{
int choice,a,b,c,d,e,sum,avg;
do
{
cout<<"*****Menu***** ";
cout<<"1. Sum of 5 numbers. ";
cout<<"2. Mean of 5 numebrs. ";
cout<<"3. Min of 5 numbers. ";
cout<<"4. Max of 5 numbers. ";
cout<<"5. Exit. ";
cin>>choice;
switch(choice)
{
case '1':
cout<<"Enter the first number. ";
cin>>&a;
cout<<"Enter the second number. ";
cin>>&b;
cout<<"Enter the third number. ";
cin>>&c;
cout<<"Enter the fourth number. ";
cin>>&d;
cout<<"Enter the fifth number. ";
cin>>&e;
sum=a+b+c+d+e;
cout<<"The sum is:"<<sum;
break;
case '2':
cout<<"Enter the first number. ";
cin>>&a;
cout<<"Enter the second number. ";
cin>>&b;
cout<<"Enter the third value. ";
cin>>c;
cout<<"Enter the fourth value. ";
cin>>&d;
cout<<"Enter the fifth value. ";
cin>>&e;
sum=a+b+c+d+e;
avg=sum/5;
cout<<"The avg is:"<<avg;
break;
case '3':
cout<<"Enter the first number. ";
cin>>&a;
cout<<"Enter the second number. ";
cin>>&b;
cout<<"Enter the third number. ";
cin>>&c;
cout<<"Enter the fourth number. ";
cin>>&d;
cout<<"Enter the fifth number. ";
cin>>&e;
if(a>b && a>c && a>d && a>e)
{
cout<<"The max is:"<<a;
}
else if(b>a && b>c && b>d && b>e)
{
cout<<"The max is:"<<b;
}
else if(c>a && c>b && c>d && c>e)
{
cout<<"The max is:"<<c;
{
else if(d>a && d>b && d>c && d>e)
{
cout"The max is:"<<d;
}
else
{
cout"The max is:"<<e;
}
break;
case '4':
cout<<"Enter the first number. ";
cin>>&a;
cout<<"Enter the second number. ";
cin>>&b;
cout<<"Enter the third number. ";
cin>>&c;
cout<<"Enter the fourth number. ";
cin>>&d;
cout<<"Enter the fifth number. ";
cin>>&e;
if(a<b && a<c && a<d && a<e)
{
cout<<"The min is:"<<a;
}
else if(b<a && b<c && b<d && b<e)
{
cout<<"The min is:"<<b;
}
else if(c<a && c<b && c<d && c<e)
{
cout<<"The min is:"<<c;
}
else if(d<a && d<b && d<c && d<e)
{
cout<<"The min is:"<<d;
}
else
{
cout<<"The min is:"<<e;
}
break;
case '5':
cout<<"End of Program. ";
break;
default:
cout<<"Not a valid choice, try again. ";
}
} while (choice !=5)
return 0;
}
Please let me know what's wrong, or how I should do it. Thanks
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int choice,a[5],sum=0,avg=0,max,min;
do
{
cout<<" *****Menu***** ";
cout<<"1. Sum of 5 numbers. ";
cout<<"2. Mean of 5 numebrs. ";
cout<<"3. Min of 5 numbers. ";
cout<<"4. Max of 5 numbers. ";
cout<<"5. Exit. ";
cin>>choice;
switch(choice)
{
case 1:
cout<<" Enter 5 numbers. ";
for(int i=0;i<5;i++)
cin>>a[i];
sum=0;
for(int j=0;j<5;j++)
sum+=a[j];
cout<<"The sum is:"<<sum;
break;
case 2:
cout<<" Enter 5 numbers. ";
for(int i=0;i<5;i++)
cin>>a[i];
sum=0;
for(int j=0;j<5;j++)
sum+=a[j];
avg=sum/5;
cout<<"The avg is:"<<avg;
break;
case 3:
cout<<" Enter 5 numbers. ";
for(int i=0;i<5;i++)
cin>>a[i];
min=a[0];
for(int j=1;j<5;j++){
if(a[j]<min)
min=a[j];
}
cout<<" The Min is "<<min;
break;
case 4:
cout<<" Enter 5 numbers. ";
for(int i=0;i<5;i++)
cin>>a[i];
max=a[0];
for(int j=1;j<5;j++){
if(a[j]>max)
max=a[j];
}
cout<<" The Max is "<<max;
break;
case 5:
cout<<"End of Program. ";
exit(1);
default:
cout<<"Not a valid choice, try again. ";
}
} while (choice !=5);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.