Help with the code

i am having a little bit of trouble with a very basic code. It displays the menu, does the command but after the output I want it to redisplay the main menu (so a loop) unless specified to exit. I have tried while, do while but no luck. It is also not working properly on the line with "you have made the wrong decision''. Thank you for your help

Purpose of the program :
Creating a menu driven program that calculate sqrt, tan,
log, log10, and log with user specified base.
Section 1 Display a menu, then receive a selection.
Section 2 Calculate the equation, or generate an error message when
other than the specified operation is entered.
Section 3 Show the result on the screen.
Section 4 Repeat Secion 1 through Section 3 unless user decides to
quit.
(Hint: Use a decision and a loop statements.)
*/

#include<iostream>
#include<cmath>
using namespace std;

void main()
{
const double pi = 3.1415926535897932384626433832795;
int select;
double num1, num2, ans;

cout<<"Please type in a number first, if doing logs make it degrees ";
cin>>num1;
cout<<"Menu : Please make a selection:" <<endl << "1. sqrt" <<endl<< "2. tan" <<endl<< "3. natural log" <<endl<<
"4. log10" <<endl<< " 5. log base specified by user"<<endl<< "6 Exit"<<endl;
cin>>select;
while (select !=6)
{

if (select == 1)
cout<<"sqrt of " <<num1<<"="<<(sqrt(num1))<<endl;


if (select == 2)
cout<<"tan of " <<num1<<"="<<tan(num1*pi/180)<<endl;

if (select == 3)
cout<<"natural log"<<num1<<" of "<<num1<<" is "<<log(num1*(pi/180))<<endl;

if (select == 4)
cout<<"log10 of "<<num1<<" is "<<log10(num1*(pi/180))<<endl;

if (select == 5)
cout<<" Please enter the log base "<<endl;
cin>>num2;
cout<<"log base "<<num2<<" of "<<num1<<" is "<<log(num1*(pi/180))/log(num2*(pi/180));
if (select == 6)
break ;

if ((select !=1 && select !=2 && select !=3 && select !=4 && select !=5))
cout<<" You have made a wrong selection " <<endl;

}}


// main
You just wrap a do...while around everything.
1
2
3
4
5
6
7
string quit = "";
do 
{
    //All your code from cout onwards
    cout << "Quit?"; 
    cin >> s; 
}while(s != "quit")

For example.

Edit: Forgot your second question:
1
2
3
4
if(select > 6 || select < 1)
{
    cout << "What are you doing fewl? Wrong selection."; 
}
Last edited on
Topic archived. No new replies allowed.