Hello, I'm trying to realize a simple calculator but when I double click the file.exe to see the output, my program keeps running in the terminal, maybe I created an infinite loop.. I'm using cin.get() I don't understand..
I compiled it with G++ (MinGW), using Win Xp.
can someone help me ?
Thanks in advance

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
using std::cout;
using std::cin;
using std::endl;

int main()
{
    double num1, num2, i = 0, total, side;
       int choice;

    do
    {

     cout << "Please select an option." << endl;

      cout<<"Select 1 for addition"<<endl;

      cout<<"Select 2 for subtraction"<<endl;

      cout<<"Select 3 for division"<<endl;

      cout<<"Select 4 for multiplication"<<endl;

      cout<<"Select 5 for area of circle"<<endl;

      cout<<"Select 6 for square root"<<endl;

      cout<<"Select 7 for PI"<<endl;

      cout<<"Select 8 for percentage"<<endl;

      cout<<"Select 9 for ogarithm"<<endl;

      cout<<"Select 10 for absolute value"<<endl;

      cout<<"Select 11 for power exponent."<<endl;

      cout<<"Select 12for exponential value."<<endl;

      cout<<"Select 13 for sine."<<endl;

      cout<<"Select 14 for cosine."<<endl;

      cout<<"Select 15 for arc tangent."<<endl;

       cout<<"Select 16 for arc cosine."<<endl;

       cout<<"Select 17 for arc sine."<<endl;

       cout<<"Select 18 for x * 2 exp."<<endl;

      }
     while (choice < 1 || choice > 19);

     if (choice == 1)
    {

      float num1, num2, sum;

      cout<<"enter the first number"<< endl;
      cin>> num1;

      cout<<"enter the 2nd number"<< endl;
      cin>>num2;

      sum= num1 + num2;

      cout<<"sum =" << setprecision(20)<< sum<< endl;

    }
//**************************************************************************

      if (choice == 2)
    {

      float num1, num2,sub;

      cout<<"enter the first number"<< endl;
      cin>> num1;

      cout<<"enter the 2nd number"<< endl;
      cin>> num2;

      sub = num1 - num2;

      cout<<"subtraction =" << setprecision(20)<< sub << endl;

    }
//**************************************************************************

      if (choice == 3)
    {

     float num1, num2, div;

      cout<<"enter the first number"<<endl;
      cin>> num1;

      cout<<"enter the 2nd number"<<endl;
      cin>> num2;

      div = num1/ num2;

      if (num2 == 0)
      {
      cout << "Cannot divide by zero." << endl;
      exit(1);
      }

      else if (num2 != 0)
    {
     cout<<"division =" << setprecision(20) << div <<endl;

    }}

//**************************************************************************
      if (choice == 4)
    {

      float num1, num2, mult;

      cout<<"enter the first number"<<endl;
      cin>> num1;

      cout<<"enter the 2nd number"<<endl;
      cin>> num2;

      mult = num1 * num2;

      cout<<"multiply =" <<setprecision(20)<<mult<<endl;

    }
//**************************************************************************
     if (choice == 5)
    {

      float  radius, area, PI=3.14;

      cout<<"enter the radius"<<endl;
      cin>>  radius;

      area = PI * ( radius *  radius) ;

      cout<<"area of square =" <<setprecision(20)<<area<<endl;

     }
//**************************************************************************      
      if (choice == 6)
    {

     double number1, square;

      cout<<"enter a number"<<endl;
      cin>> number1;

      square = sqrt (number1);

      cout<<"square root =" << setprecision(20) << square <<endl;

    }
//**************************************************************************       
      if (choice == 7)
    {

     float number1;
    cout << "enter a number" << endl;
    cin >> number1;

    if (number1 != 3.14159265359)
    {
    number1 = 3.14159265359;
    }
   cout<<"PI is "<< number1 <<endl;

   }
//**************************************************************************  
    if (choice == 8)
    {

     float number1,  number2, percentage;

     cout<<"enter the first number"<<endl;
      cin>> number1;

    cout<<"enter the 2nd number"<<endl;
     cin>> number2;

    percentage = number1 / number2 *  100;
    cout<<"percentage=" <<setprecision(20)<< percentage <<endl;
    }
//**************************************************************************     
    if (choice == 9)
    {

    double number1, logarithm;
    cout<<"enter the first number"<<endl;
    cin>> number1;

    logarithm = log (number1);
    cout<<"logarithm =" <<setprecision(20)<< logarithm <<endl;
    }
//**************************************************************************      
    if (choice == 10)
    {

    int number1;
    cout<<"enter a number"<<endl;
    cin>> number1;

    cout<<"absolute value  =" << setprecision(20) <<  abs (number1) <<endl;
    }
//**************************************************************************     
    if (choice == 11)
    {

   int number1, exponent, power;

   cout<<"enter the first number"<<endl;
   cin>> number1;

    cout<<"enter the exponent"<<endl;
    cin>> exponent;   

    power = pow (number1, exponent);

    cout<<"power exponent is "<< power <<endl;
    }
//**************************************************************************   
     if (choice == 12)
    {

    double  number1, result;

    cout<<"enter a number"<<endl;
    cin>> number1;

    result = exp (number1);

    cout<<"exponential value is "<<  result <<endl;
    }
//**************************************************************************    
     if (choice == 13)
    {

    #define PI 3.14159265

    double number1, result;

    cout<<"enter a number"<<endl;
    cin>> number1;

    result = sin (number1 * PI/180);

    cout<<"sine is "<<  result <<endl;
    }
 //**************************************************************************    
    if (choice == 14)
    {

    #define PI 3.14159265

    double number1, result;

    cout<<"enter a number"<<endl;
    cin>> number1;

    result = cos (number1 * PI/180);

    cout<<"cosine is "<<  result <<endl;
    }
 //**************************************************************************    
    if (choice == 15)
    {

    #define PI 3.14159265

    double x, y, result;

    cout<<"enter the first number"<<endl;
    cin>>x;

    cout<<"enter the 2nd number"<<endl;
    cin>>y;   

    result = atan2 (y,x) * 180 / PI;

    cout<<"arc tangent is "<<  result <<endl;
    }
 //**************************************************************************    
    if (choice == 16)
    {

    #define PI 3.14159265

    double number1, result;

    cout<<"enter a number"<<endl;
    cin>> number1;

    result = acos (number1) * 180.0 / PI;

    cout<<"arc cosine is "<<  result <<endl;
    }
 //**************************************************************************   
    if (choice == 17)
    {

    #define PI 3.14159265

    double number1, result;

    cout<<"enter a number"<<endl;
    cin>> number1;

    result = asin (number1) * 180.0 / PI;

    cout<<"arc sine is "<<  result <<endl;
    }
 //**************************************************************************  
    if (choice == 18)
    {

    double number1, result;

    int exponent;

    cout<<"enter a number"<<endl;
    cin>> number1;

    cout<<"enter the exponent"<<endl;
    cin>> exponent;  

    result = ldexp (number1 , exponent);

    cout<<" x * 2 exp is "<<  result <<endl;

    cin.get();
    return 0;

    }
    }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please use code tags in the future.

In your first do-while loop, you only print out the possible choices for mathematical operation. You never ask/receive the input for choice (assumption;from std::cin) and your integer choice will never change thus the infinite loop. Also you should initialize choice to some integer value.

Your program starts with a do...while loop that looks at the value of a variable named "choice" as its terminating condition. After that, you have a collection of if statements that also look at the value of this variable.

However, I can find no place in your program where you ever give that variable a value. What value do you expect to have, and how do you expect the value to get there?

Your program starts with a do...while loop that looks at the value of a variable named "choice" as its terminating condition. After that, you have a collection of if statements that also look at the value of this variable.

However, I can find no place in your program where you ever give that variable a value. What value do you expect to have, and how do you expect the value to get there?

Thank you, you have reason, now I understand which is the problem.

This question has already been answered. Start a new discussion instead.