15,958,834 members
See more:
C++
```#include<iostream>
#include<math.h>
#include<conio.h>
using namespace std;
int main ()
{
start:
float a,b,PI;
int c;
cout<<endl;
cout<<"....................... Scientific Calculator....................................\n";
cout<<"..................................................................................\n";
cout<<"Operations\t"<<"\tTrigonometric Functions"<<"\t\tLogarithmic Functions"<<endl;
cout<<".................................................................................."<<endl;
cout<<"1: Division\t\t"			<<"7: Sin\t\t"			<<"13:Natural Log(base= e)"<<endl;

cout<<"2: Multiplication\t"		<<"8: Cos\t\t"			<<"14:Common Log(base= 10)"<<endl;

cout<<"3: Subtraction\t\t"		<<"9: Tan"<<endl;

cout<<"4: Addition\t\t"			<<"10: Inverse of Sin"<<endl;

cout<<"5: Exponent\t\t"			<<"11: Inverse of Cos"<<endl;

cout<<"6: Square root\t\t"		<<"12: Inverse of Tan"<<endl;

cout<<"________________________________________________________________________________"<<endl;
cout<<"Hyperbolic Functions\t"<<"Inverse Hyperbolic"<<endl;
cout<<"________________________________________________________________________________"<<endl;
cout<<"15: Sin Hyp\t\t"		<<"18: aSinh"<<endl;
cout<<"16: Cos Hyp\t\t"		<<"19: aCosh"<<endl;
cout<<"17: Tan Hyp\t\t"		<<"20: aTanh"<<endl;
cout<<"_______________________________________________________________________________\n";
cout<<"Enter the Number of function that you want to perform : ";
cin>>c;
cout<<endl;
PI=3.14159265;
switch(c)
{
case 1:
cout<<"Enter 1st number : ";
cin>>a;
cout<<endl;
cout<<"Enter 2nd number : ";
cin>>b;
cout<<endl;
cout<<"Division = "<<a/b<<endl;
break;
case 2:
cout<<"Enter 1st number : ";
cin>>a;
cout<<endl;
cout<<"Enter 2nd number : ";
cin>>b;
cout<<endl;
cout<<"Multiplication = "<<a*b<<endl;
break;
case 3:
cout<<"Enter 1st number : ";
cin>>a;
cout<<endl;
cout<<"Enter 2nd number : ";
cin>>b;
cout<<endl;
cout<<"Subtraction = "<<a-b<<endl;
break;
case 4:
cout<<"Enter 1st number : ";
cin>>a;
cout<<endl;
cout<<"Enter 2nd number : ";
cin>>b;
cout<<endl;
break;
case 5:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Enter the exponent : ";
cin>>b;
cout<<endl;
cout<<"Exponent = "<<pow(a,b)<<endl;
break;
case 6:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Square Root = "<<sqrt(a)<<endl;
break;
case 7:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Sin = "<<  sin(a)<<endl;
break;
case 8:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Cos = "<<  cos(a)<<endl;
break;
case 9:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Tan = "<<  tan(a)<<endl;
break;
case 10:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Inverse of Sin = "<<  asin(a)*180.0/PI<<endl;
break;

case 11:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Inverse of Cos = "<<  acos(a)*180.0/PI<<endl;
break;
case 12:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Inverse of tan = "<<  atan(a)*180.0/PI<<endl;
break;
case 13:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Natural Log(base= e)"<<  log(a)<<endl;
break;
case 14:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Common Log(base= 10) = "<<  log10(a)<<endl;
break;
case 15:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Sine Hyperbolic = "<<  sinh(a)<<endl;
break;
case 16:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Cos Hyperbolic = "<<  cosh(a)<<endl;
break;
case 17:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Tan Hyperbolic = "<<  tanh(a)<<endl;
break;
case 18:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Sin Hyp Inverse = "<<  asinh(a)<<endl;
break;
case 19:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Cos Hyp Inverse = "<<  acosh(a)<<endl;
break;
case 20:
cout<<"Enter the number : ";
cin>>a;
cout<<endl;
cout<<"Tan Hyp Inverse = "<<  atanh(a)<<endl;
break;
default:
cout<<"error! operator is not correct\n";
break;
}
cout<<"want to do another calculation y/n ";
cin>>c;
if(c!='n')
{
goto start;
}
else
{

cout<<"the program ends ";//but if the user press n the program will end
}
return 0;
}```

What I have tried:

My programs runs but when it asks again for running the program and I enter y or n, so infinite loop starts which is a problem. So what is the solution?
Posted
Updated 14-Jan-21 11:42am
v2

## Solution 1

1: don't use `goto`. C++ provides a good number of structured ways to loop - use one of them, such as `while` or `do while`

2) Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
```Input   Expected output    Actual output
1            2                 1
2            4                 4
3            6                 9
4            8                16```
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
```int Double(int value)
{
return value * value;
}```

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

## Solution 3

C++
```...
int main ()
{
start:
float a,b,PI;
int c; // here you say c is an integer
cout<<endl;
...
cout<<"Enter the Number of function that you want to perform : ";
cin>>c; // and here you expect the user input to be automatically converted to integer
cout<<endl;
PI=3.14159265;
switch(c)
...
cout<<"want to do another calculation y/n ";
cin>>c; // but here, you expect the user input to keep the char code
if(c!='n')
{
goto start;
}
else
{

cout<<"the program ends ";//but if the user press n the program will end
}
return 0;
}```

What magic are you using to tell the compiler that one time input for `c` is an integer, and one time it is a char?
In fact typing 110 will be seen as 'n' (ascii code) in your code.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

CPallini 14-Jan-21 16:19pm
5.
Patrice T 14-Jan-21 16:23pm
Thank you

## Solution 2

I have two recommendations. First, break this into code functions. There should be at least three : display the options and prompt for input (could be split into two), calculate and display the answer, and ask to continue. Your main function will then control the looping based on the result of the third function, asking to continue.

Second - use the constants defined in math.h. Namely, M_PI. To get the constants defined include it like this :
C++
```#define _USE_MATH_DEFINES
#include <math.h>```
You can also implement other functions like one for converting degrees to radians.

The point is it is extremely useful to learn how to work with functions.

## Solution 4

Hey, it is `C++`
C++
```#include <iostream>
#include <map>
#include <cmath>
#include <functional>
using namespace std;

using binary_map = map<int, pair < string, function<double (double, double)> > >;
using unary_map =  map<int, pair < string,  function<double (double)> > >;

void show_menu( const binary_map & bm, const unary_map &um);
double apply( function<double (double, double)>  bin_fun);
double apply( function<double (double)>  una_fun);

int main()
{
binary_map bm =
{
{ 1, { "division", [](double a, double b){ return a/b; } } },
{ 2, {"multiplication", [](double a, double b){ return a*b; } } },
//...
};
unary_map um =
{
{ 6, { "square root ", [](double x ){return sqrt(x);} } },
{ 7, {"sin", [](double x) { return sin(x);} } },
//...
};

for (;;)
{

int choice;
cin >> choice;
bool found = false;

if ( ! found )
{
auto it = bm.find(choice);
if ( it != bm.end())
{
auto result = apply(it->second.second);
cout << "result is " << result << "\n";
found = true;
}
}
if ( ! found )
{
auto it = um.find(choice);
if ( it != um.end() )
{
auto result = apply(it->second.second);
cout << "result is " << result << "\n";
found = true;
}
}
if ( ! found ) break;
}
}

void show_menu( const binary_map & bm, const unary_map &um)
{

for ( const auto & [key, value] : bm )
cout << key << " - " << value.first << endl;

for ( const auto & [key, value] : um )
cout << key << " - " << value.first << endl;
}

double apply( function<double (double, double)>  bin_fun)
{
double x,y;
cout << "please enter the two operands\n";
cin >> x >> y;
return bin_fun(x,y);
}

double apply( function<double (double)>  una_fun)
{
double x;
cout << "please enter the operand\n";
cin >> x ;
return una_fun(x);
}```