I need a menu driven program that has the following 5 choices.
Output a sum of even numbers
Output a table of odd numbers
Output their square root
Output all lower case and upper case letter
Quit.
I have the code to all the functions now I just need to put it into a menu driven
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int firstNum, secondNum;
int sumEvenNum = 0;
int sumSquareOddNum = 0;
char chCounter;
int counter;
//Part a
cout << "Enter two numbers." << endl;
cout << "First number must be less than ";
cout << "the second number you enter" << endl;
cout << "Enter numbers: " << flush;
cin >> firstNum >> secondNum;
cout << endl;
//Part b
if (firstNum % 2 == 0)
counter = firstNum;
else
counter = firstNum + 1;
while (counter <= secondNum)
{
sumEvenNum = sumEvenNum + counter;
counter = counter + 2;
}
cout << "Sum of even integers between " << firstNum << " and "
<< secondNum << " = " << sumEvenNum << endl;
//Part c
if (firstNum % 2 == 0)
counter = firstNum + 1;
else
counter = firstNum;
cout << "Odd integers between " << firstNum << " and "
<< secondNum << " are: " << endl;
for (; counter <= secondNum; counter += 2)
{
cout << counter << " ";
}
cout << endl;
//Part d
cout << "Number Square of Number" << endl;
counter = 1;
while (counter <= 10)
{
cout << setw(4) << counter << setw(18)
<< counter * counter << endl;
counter++;
}
cout << endl;
//Part e
if (firstNum % 2 == 0)
counter = firstNum + 1;
else
counter = firstNum;
while (counter <= secondNum)
{
sumSquareOddNum = sumSquareOddNum + counter * counter;
counter = counter + 2;
}
//Part f
cout << "Upper case letters are: ";
chCounter = 'A';
do {
cout << chCounter << " ";
chCounter++;
} while (chCounter - 1 <= 'Z');
cout << endl;
cout << "Lower case letters are: ";
chCounter = 'a';
do {
cout << chCounter << " ";
chCounter++;
} while (chCounter - 1 <= 'z');
cout << endl;
return 0;
}
that is the code for all the different functions.
Update:Yes I need to give them a menu where they need to choose what they want to do. I have tried doing in it with a switch statement several times but i apparently fail at calling the functions so i went back to just having the functions to do everything.
Copyright © 2024 EBIN.TIPS - All rights reserved.
Answers & Comments
Verified answer
You should do like following :
int iChoice = 0;
while(5!=iChoice){
cout<<"=====Menu====="<<endl
<<"1. Output a sum of even numbers"<<endl
<<"2. Output a table of odd numbers"<<endl
<<"3. Output their square root"<<endl
<<"4. Output all lower case and upper case letter"<<endl
<<"5 .Quit." << endl
<<"Enter your choice : "<<endl;
switch(iChoice){
case 1:
//call the function for Output a sum of even numbers
break;
case 2:
//call the function for Output a table of odd numbers
break;
case 3:
//call the function for Output their square root
break;
case 4:
//call the function for Output all lower case and upper case letter
break;
case 5:
//call the function for Quit
break;
case default:
cout<<"Invalid Choice\nPlease enter valid choice"<<endl;
//call the function which displays this menu
break;
}
}
I think I am a little unsure of your question. Do you need a menu that asked a person "What would you like to do?" and give them a choice between answers? If so the best way to do it is to place each part in its own function and just have the menu call out the function, for instance in the main function you will have:
int main()
{
int choice;
while (choice == 0)
{
cout << "What would you like to do?" << endl;
cout << "1) Output the sum of two even numbers?" << endl;
cout << "2) Output a table of odd numbers?" << endl;
//you will add the rest in order
cin >> choice;
switch (choice) // the switch statement will which ever function is needed
{
case '1':
choiceA();
case '2':
choiceB();
default: // if the user inputs anything then the choices then it will reset to 0 and loop
{
cout<< "Please enter a valid response" << endl;
bout << "Please try again." << endl;
choice = 0;
}
}
return 0;
}
Hope this helps, if you need more help just respond. Thanks.