Help please (summation, exponential and factorial)

General textbook question,
Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered.

but professor wants me to add more and he's not very helpful.
1. Display a Menu of Arithmetic Functions to the user, allowing them to choose Summation, Factorial, or Exponential

2.The menu repeats until quit is chosen. It's not just one choice and then bam!, it's over. The menu displays, the users gets to see the math function work, and then the menu appears again. The only way out of math function hell is to enter a '4. quit'. Use a Do While statement loop to drive the menu. See page 245 in your textbook for driving the menu. Use a Switch statement to decide which function to execute and to validate input

and i'm really stuck so please help this is all i have so far...
#include <iostream>
using namespace std;

int main ()
{
//menu choice
int choice, integer;
long double sum = 0, fac = 0;


double n = 2, total = 1;


//const for menu choices
const int choiceOne= 1, choiceTwo = 2, choiceThree = 3, quit = 4;

do {
//display menu
//do this until the user selects quit
cout<< " \t MATHEMATICAL FUNCTION MENU\n\n"
<<"1. Summation\n"
<<"2. Factorial\n"
<<"3. Exponetial\n"
<<"4. Quit Program\n"
<<" Enter your choice. \n";
cin >> choice;

//verify that only numbers 1-4 were entered and nothing else
while (choice<choiceOne || choice>quit ){
//display messege = wrong number was enter
//and let user make choice again
cout<<"please enter a valid choice";
cin>>choice;
}

//
switch (choice)
{
case choiceOne:
cout<<"enter an integer\n";
cin>>integer;
//for loop to get summation of interger entered
for (int counter = 0; counter <= integer; counter++)
{
sum = sum + integer;
counter++;
}

if (integer < 1)
{
cout<< "\n" << integer << " is an invalid input.";
cout<< " please enter a valid integer\n";
cin>>integer;
}

else
cout<< "\n the summation of " << integer
<< " " << "is " << sum << "\n"<<endl;
break;

case choiceTwo:
cout<<"enter an integer\n";
cin>>integer;

//for loop to get factorial of interger entered
for (int counter = 0; counter <= integer; counter++)
{
fac = fac * integer;
}
if (integer < 1)
{
cout<< "\n" << integer << " is an invalid input.";
cout<< " please enter a valid integer\n";
cin>>integer;
}

else
cout<< "\n the factorial of " << integer
<< " " << "is " << fac << "\n"<<endl;

break;




case choiceThree:
cout<<"enter an exponent for the number 2\n";
int expo;
cin>>expo;

for (int k=1; k<=expo; k++)
{
total = total*n;

}
if (expo < 1)
{
cout<< "\n2" << expo << " is an invalid input.";
cout<< " please enter a valid integer\n";
cin>>total;
}

else
cout << total <<endl;

break;

case quit:
cout<< "Goodbye"<<endl;

}

//selecting 4 will quit the program
} while (choice !=quit);
return 0;
Last edited on
Topic archived. No new replies allowed.