Calculator++, Now Available for download on SourceForge!

Status
Not open for further replies.
Instead of goto use a Do/While loop. Here's one possible way you could do it:

Code:
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{


int WhichFunction;
bool user_quit;

user_quit = false;

Do
   {

cout << "What would you like to do ? " << endl << "HINT: 1 = add, 2 = subtract, 3 = multiply, 4 = divide, 5 = Sqr. Root," << endl << "6 = Exponents" << " 99 = Quit" << endl;

cin >> WhichFunction;
switch(WhichFunction){

case 1:
double a;
cout << "Enter the first number: ";
cin >> a;
double b;
cout << "Enter the second number: ";
cin >> b;
double c;
c = a + b;
cout << "Calculating: " << c << endl;
system("PAUSE");
return 0;
break;

case 2:
double aa;
cout << "Enter the first number: ";
cin >> aa;
double bb;
cout << "Enter the second number: ";
cin >> bb;
double cc;
cc = aa - bb;
cout << "Calculating: " << cc << endl;
system("PAUSE");
return 0;
break;

case 3:
double a3;
cout << "Enter the first number: ";
cin >> a3;
double b3;
cout << "Enter the second number: ";
cin >> b3;
double c3;
c3 = a3 * b3;
cout << "Calculating: " << c3 << endl;
system("PAUSE");
return 0;
break;

case 4:
double a4;
cout << "Enter the first number: ";
cin >> a4;
double b4;
cout << "Enter the second number: ";
cin >> b4;
double c4;
c4 = a4 / b4;
if(a4 > 0){
cout << "Calculating: " << c4 << endl;
}
if(b4 > 0){
cout << "Calculating: " << c4 << endl;
}
if(b4 <= 0){
cout << "You Cannot Divide by Zero " << endl;
}
system("PAUSE");
return 0;
break;

case 5:
double a5;
cout << "Enter Your Number: ";
cin >> a5;
double b5;
b5 = sqrt(a5);
cout << "Calculating: " << b5 << endl;
break;

case 6:
double a6,b6;
cout << "Enter your Number: ";
cin >> a6;
cout << "Enter the Exponent: ";
cin >> b6;
double c6;
c6 = pow(a6,b6);
cout << "Calculating: " << c6 << endl;
break;

case 99;
user_quit = true;
break;

default:
cout << "An Error Has Occured, Please try and run the program again. ";
break;
}

} While !user_quit;

system("PAUSE");
return EXIT_SUCCESS;
}
 
Thanks,
Basically what i did is i used a goto loop to loop individual functions. And i used strollin's do while loop, although i'm not sure what thats supposed to do, as i am new to loops.

Current Source:
Code:
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;
int main()
{
     loopA:
     int WhichFunction;
     bool user_quit;
     
     user_quit = false;
     
     do
       {
    cout << "What would you like to do ? " << endl << "HINT: 1 = Add, 2 = Subtract, 3 = Multiply, 4 = Divide, 5 = Sqr. Root," << endl << "6 = Exponents, 7 = Pythagorean Theorum, 8 = Sine, 9 = Cosine, 10 = Tangent," << endl << "11 = Circumference, 12 = Area of Circles, 13 = Logarithms, 14 = Area of Squares,15 = Area of Triangles, 16 = Perimeter of Squares, 17 = Absolute Value," << endl << "18 = Volume of Cubes, Rect. Prisms, 19 = Volume of Cylinders," << endl << "20 = Volume of Tri. Prisms, 21 = Round Down, 22 = Round Up, 23 = Cube Root," << endl << "24 = Density, 25 = Volume of Pyramids, 99 = Quit" << endl;
    
cin >> WhichFunction;
    switch(WhichFunction){
      
case 1:
           loop:
           int redo;
           double a;
           cout << "Enter the first number: ";
           cin >> a;
           double b;
           cout << "Enter the second number: ";
           cin >> b;
           double c; 
           c = a + b;
           cout << "Calculating: " << c << endl;
           cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
           cin >> redo;
           
if(redo == 1){
           goto loop;
                   }
           
if(redo == 0){
           system("PAUSE");
           return 0;
                   }
           system("PAUSE");
           return 0;
           break;
      
case 2:
          loop2:
          int redo2;
          double aa;
          cout << "Enter the first number: ";
          cin >> aa;
          double bb;
          cout << "Enter the second number: ";
          cin >> bb;
          double cc;
          cc = aa - bb;
          cout << "Calculating: " << cc << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo2;
          
if(redo2 == 1){
          goto loop2;
          }
          
if(redo2 == 0){
          system("PAUSE");
          return 0;
                  }
          system("PAUSE");
          return 0;
          break;
      
case 3:
          loop3:
          int redo3;
          double a3;
          cout << "Enter the first number: ";
          cin >> a3;
          double b3;
          cout << "Enter the second number: ";
          cin >> b3;
          double c3;
          c3 = a3 * b3;
          cout << "Calculating: " << c3 << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo3;
          
if(redo3 == 1){
          goto loop3;
          }
          
if(redo3 == 0){
          system("PAUSE");
          return 0;
                  }
          system("PAUSE");
          return 0;
          break;
      
case 4:
          loop4:
          int redo4;
          double a4;
          cout << "Enter the first number: ";
          cin >> a4;
          double b4;
          cout << "Enter the second number: ";
          cin >> b4;
          double c4;
          c4 = a4 / b4;
          
if(b4 > 0){
          cout << "Calculating: " << c4 << endl;  
          }
          
if(b4 <= 0){
          cout << "You Cannot Divide by Zero " << endl;
          system("PAUSE");
          return 0;      
          }
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo4;
          
if(redo4 == 1){
          goto loop4;
          }
          
if(redo4 == 0){
          system("PAUSE");
          return 0;
                  }
          system("PAUSE");
          return 0;
          break;
      
case 5:
           loop5:
           int redo5;
           double a5;
           cout << "Enter Your Number: ";
           cin >> a5;
           double b5;
           b5 = sqrt(a5);
           cout << "Calculating: " << b5 << endl;
           cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo5;
          
if(redo5 == 1){
          goto loop5;
          }
          
if(redo5 == 0){
          system("PAUSE");
          return 0;
                  }
           system("PAUSE");
           return 0;
           break;
      
case 6:
           loop6:
           int redo6;
           double a6,b6;
           cout << "Enter your Number: ";
           cin >> a6;
           cout << "Enter the Exponent: ";
           cin >> b6;
           double c6;
           c6 = pow(a6,b6);
           cout << "Calculating: " << c6 << endl;
           cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo6;
         
 if(redo6 == 1){
          goto loop6;
          }
         
 if(redo6 == 0){
          system("PAUSE");
          return 0;
                  }
           system("PAUSE");
           return 0;
           break;
      case 7:
           loop7:
           int redo7;
           double a7;
           double b7;
           double c7;
           double x7;
           cout << "-----------------------" << endl;
           cout << " Pythagorean Theorem" << endl;
           cout << "-----------------------"  << endl;
           cout << "   ." << endl;
           cout << "   .  . " << endl;
           cout << " a .     . c" << endl;
           cout << "   .       . "<< endl;
           cout << "   .         ."<< endl;
           cout << "   ............."<< endl;
           cout << "        b      "<< endl;
           cout << endl;
           cout << "How to: " << endl;
           cout << endl;
           cout << "When asked for your missing variable type press '0' and then enter." << endl;
           cout << endl;
           cout << "Equation: a(sqaured) + b(square) = c(squared)" << endl;
           cout << endl;
           cout << "Please input your 'a' variable: ";
           cin >> a7;
           cout << "Please input your 'b' variable: ";
           cin >> b7;
           cout << "Please input your 'c' variable: ";
           cin >> c7;
          
 if (c7 == 0)
           {
           x7 = (a7*a7) + (b7*b7);
           cout << "Your missing length is " << sqrt(x7) << endl;
           }
          
 if (a7 == 0)
           {
           x7 = (c7*c7) - (b7*b7);
           cout << "Your missing length is " << sqrt(x7) << endl;
            }
           
 if (b7 == 0)
            {
            x7 = (c7*c7) - (a7*a7);
            cout << "Your missing length is " << sqrt(x7) << endl;
            }
            cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo7;
          
if(redo7 == 1){
          goto loop7;
          }
         
 if(redo7 == 0){
          system("PAUSE");
          return 0;
                  }
            system("PAUSE");
            return 0;
            break;
      
case 8:
          loop8:
          int redo8;
          double a8;
          cout << "Enter your Number: ";
          cin >> a8;
          cout << "The sine of your number is: " << sin(a8) << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo8;
          
if(redo8 == 1){
          goto loop8;
          }
          
if(redo8 == 0){
          system("PAUSE");
          return 0;
                  }
          system("PAUSE");
          return 0;
          break;
     
case 9:
          loop9:
          int redo9;
          double a9;
          cout << "Enter your Number: ";
          cin >> a9;
          cout << "The Cosine of your Number is: " << cos(a9) << endl; 
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo9;
          
if(redo9 == 1){
          goto loop9;
          }
          
if(redo9 == 0){
          system("PAUSE");
          return 0;
                  }
          system("PAUSE");
          return 0;
          break;
     
case 10:
          loop10:
          int redo10;
          double a10;
          cout << "Enter Your Number: ";
          cin >> a10;
          cout << "The Tangent of your Number is: " << tan(a10) << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo10;
         
 if(redo10 == 1){
          goto loop10;
          }
         
 if(redo10 == 0){
          system("PAUSE");
          return 0;
                  }
          system("PAUSE");
          return 0;
          break;
     
case 11:
          loop11:
          int redo11;               
          double diameter;
          cout << "Enter Your Diameter: ";
          cin >> diameter;
          double circumference;
          circumference = diameter * 3.14;
          cout << "The circumference of your circle is: " << circumference <<  endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo11;
          
if(redo11 == 1){
          goto loop11;
          }
          
if(redo11 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 12:
          loop12:
          int redo12;
          double radius;
          cout << "Enter Your Radius: ";
          cin >> radius;
          double area;
          area = 3.14 * pow(radius,2);
          cout << "Your Area Is: " << area << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo12;
          
if(redo12 == 1){
          goto loop12;
          }
          
if(redo12 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 13:
          loop13:
          int redo13;
          double a13;
          cout << "Enter your Number: ";
          cin >> a13;
          double b13;
          b13 = log(a13);
          cout << "Calculating: " << b13 << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo13;
          
if(redo13 == 1){
          goto loop13;
          }
          
if(redo13 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 14:
          loop14:
          int redo14;
          double base;
          double height;
          double area2;
          cout << "Enter the Base: ";
          cin >> base;
          cout << "Enter the Height: ";
          cin >> height;
          area = base * height;
          cout << "Calculating: " << area << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo14;
          
if(redo14 == 1){
          goto loop14;
          }
         
 if(redo14 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 15:
          loop15:
          int redo15;
          double base2;
          double height2;
          double area3;
          cout << "Enter the Base: ";
          cin >> base2;
          cout << "Enter the Height: ";
          cin >> height2;
          area3 = (base2 * height2) / 2;
          cout << "Calculating: " << area3 << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo15;
         
 if(redo15 == 1){
          goto loop15;
          }
          
if(redo15 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 16:
          loop16:
          int redo16;
          double side1;
          double side2;
          double side3;
          double side4;
          double perimeter;
          cout << "Enter the length of your first side: ";
          cin >> side1;
          cout << "Enter the length of your second side: ";
          cin >> side2;
          cout << "Enter the length of your third side: ";
          cin >> side3;
          cout << "Enter the length of your fourth side: ";
          cin >> side4;
          perimeter = side1 + side2 + side3 + side4;
          cout << "Calculating: " << perimeter << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo16;
          
if(redo16 == 1){
          goto loop16;
          }
          
if(redo16 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 17:
          loop17:
          int redo17;
          double a17;
          double b17;
          cout << "Enter Your Number: ";
          cin >> a17;
          b17 = fabs(a17);
          cout << "Calculating: " << b17 << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo17;
          
if(redo17 == 1){
          goto loop17;
          }
          
if(redo17 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 18:
          loop18:
          int redo18;
          double base3;
          double width;
          double height3;
          double volume;
          cout << "Enter Your Base: ";
          cin >> base3;
          cout << "Enter Your Width: ";
          cin >> width;
          cout << "Enter Your Height: ";
          cin >> height3;
          volume = base3 * width * height3;
          cout << "Calculating: " << volume << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo18;
          
if(redo18 == 1){
          goto loop18;
          }
          
if(redo18 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 19:
          loop19:
          int redo19;
          double radius2;
          double area4;
          double height4;
          double volume2;
          cout << "Enter Your Radius: ";
          cin >> radius2;
          area4 = pow(radius2,2);
          cout << "Enter your Height: ";
          cin >> height4;
          volume = area4 * height4;
          cout << "Calculating: " << volume2 << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo19;
          
if(redo19 == 1){
          goto loop19;
          }
          
if(redo19 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 20:
          loop20:
          int redo20;
          double base4;
          double height5;
          double height6;
          double area5;
          double volume3;
          cout << "Enter Your Base: ";
          cin >> base4;
          cout << "Enter Your Height: ";
          cin >> height5;
          area5 = base4 * height5;
          cout << "Enter Your Second Height: ";
          cin >> height6;
          volume3 = height6 * area5;
          cout << "Calculating: " << volume3 << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo11;
          
if(redo20 == 1){
          goto loop20;
          }
          
if(redo20 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 21:
          loop21:
          int redo21;
          double a21;
          double b21;
          cout << "Enter Your Number: ";
          cin >> a21;
          b21 = floor(a21);
          cout << "Calculating: " << b21 << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo21;
          
if(redo21 == 1){
          goto loop21;
          }
          
if(redo21 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 22:
          loop22:
          int redo22;
          double a22;
          double b22;
          cout << "Enter Your Number: ";
          cin >> a22;
          b22 = ceil(a22);
          cout << "Calculating: " << b22 << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo22;
          
if(redo22 == 1){
          goto loop22;
          }
          
if(redo22 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     case 23:
          loop23:
          int redo23;
          double val;
          cout << "Enter Your Number: ";
          
if (cin >> val){
          cout << "Cube Root of " << val << ": " << pow(abs(val), 1.0 / 3.0) << endl;        
          }
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo23;
          
if(redo23 == 1){
          goto loop23;
          }
          
if(redo23 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 24:
          loop24:
          int redo24;
          double mass;
          double volume4;
          double density;
          cout << "Enter The Mass of Your Object: ";
          cin >> mass;
          cout << "Enter The Volume of Your Object: ";
          cin >> volume4;
          density = mass / volume4;
          cout << "Calculating: " << density << " g/cm3, g/ml " << endl;
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo24;
          
if(redo24 == 1){
          goto loop24;
          }
          
if(redo24 == 0){
          system("PAUSE");
          return 0;
                  }
          break;
     
case 25: 
          loop25:
          int redo25;
          double width25;
          double height25;
          double height25b;
          double volume25;
          cout << "Enter The Width of the Base: ";
          cin >> width25;
          cout << "Enter The Height of the Base: ";
          cin >> height25;
          cout << "Enter The Height of The Pyramid: ";
          cin >> height25b;
          volume25 = ((width25 * height25) *height25b) / 2;
          cout << "Calculating: " << volume25 << endl; 
          cout << "Would You Like to Run The Function Again?" << endl << "If YES Type 1, If NO Type 0" << endl;
          cin >> redo25;
          
if(redo25 == 1){
          goto loop25;
          }
          
if(redo25 == 0){
          system("PAUSE");
          return 0;
                  }
     case 99:
          user_quit = true;
          break;
               
     default:
          int RedoDefault;
          cout << "An Error Has Occured, Please try and run the program again. " << endl;
          cout << "Go To The Beginning? YES = 1, NO = 0" << endl;
          cin >> RedoDefault;
          if(RedoDefault == 1){
          goto loopA;
          }
          if(RedoDefault == 0){
          system("PAUSE");
          return 0;
                  }
          break;
          }
          } while (!user_quit);
    
system("PAUSE");
    return 0;
}
I might end Up Releasing The New Version a Month early since i have so many functions.
 
Again, you don't need the goto's. The do/while loop is all that you need. Essentially, you want your program to keep looping until the user decides to quit. In my example, the user indicates they want to quit by entering 99 at the prompt for "What you like to do?". If you wish to loop within the functions themselves, you could use a do/while loop there as well.

Goto's are really frowned upon in most programming circles as they tend to allow the habit of what is referred to a "spaghetti code" where program flow jumps all over the place.

As you can see, your switch statement is getting rather large and cumbersome. You might what to try writing actual C/C++ function routines for your math functions that can be called from within the switch cases. Only the simplest programs put everything into Main. Link to example.
 
UPDATE:
Calculator++ 2.0 Released! As i said before, I ended up releasing 2.0 a month early.
You can Get the new Version here: Calculator++Files@SourceForge
So go download it, and if it's not two much trouble recommend it to some friends.
Thanks for all your help,
eXtensible
 
it seems other command line calcs are performing a single calc and exiting. I'm thinking you would be able to create a flow with keywords and then prompt for values. It's tougher for a user to match a number with a keyword, but easier just to type the keyword. Add a help command to list all keywords; similar to help in the command line util for ftp.

http://cmdlinecalc.sourceforge.net/screenshot.jpg

A Command Line Calculator - CodeProject
 
The advantage of writing a calculator that runs on the command line (and accepts arguments) is that it can doing scripting and other cool stuff, it's also very simple to write. By way of example, here is a calculator that I wrote in yabasic:
Code:
if(peek("argument") = 3) then
	leftOperand = val(peek$("argument"))
	operator$ = peek$("argument")
	rightOperand = val(peek$("argument"))
else
	print "You must enter exactly three arguments"
	exit
endif

switch operator$
	case "*":
		print leftOperand * rightOperand
		break
	case "+":
		print leftOperand + rightOperand
		break
	case "/":
		print leftOperand / rightOperand
		break
	case "-":
		print leftOperand - rightOperand
		break
	default:
		print "Invalid operator; valid operators are: -, +, / and *"
end switch
(disclaimer: although the code works, I don't actually know yabasic so the above has poor precision as well)

Here is some stuff that it can do:

Use its own results from earlier calculations in the current calculation.
Code:
commcalc `commcalc 9 '*' 4` "-" `commcalc 8 '/' 7`

Use results from other programs in a calculation e.g. get the number of hidden files in a directory:
Code:
commcalc `ls -Al |wc -l` '-' `ls -l|wc -l`

Use numbers stored in files in calculations:
Code:
echo 1000000 >onefile
echo 999999 >twofile
commcalc `cat onefile` '-' `cat twofile`
 
Status
Not open for further replies.
Back
Top Bottom