#include <iostream>
#include <windows.h>
usingnamespace std;
int main()
{
float first;
float second;
string choice;
select:
Beep(200,10);
cout << "Input calculation method:\n";
cin >> choice;
if(choice == "Add"||"ADD"||"add"){goto add;}elseif(choice == "Subtract"||"subtract"||"SUBTRACT"){goto subtract;}elseif(choice == "Multiply"||"multiply"||"MULTIPLY"){goto multiply;}elseif(choice == "Divide"||"divide"||"DIVIDE"){goto divide;}
add:
Beep(200,1000);
cout << "Enter first number:\n";
cin >> first;
cout << "Now enter the number you want to add it with:\n";
cin >> second;
cout << first << " plus " << second << " = " << first + second << ".";
system("PAUSE");
system("CLS");
goto select;
subtract:
Beep(200,1000);
cout << "Enter first number:\n";
cin >> first;
cout << "Now enter the number you want to subtract it with:\n";
cin >> second;
cout << first << " minus " << second << " = " << first - second << ".";
system("PAUSE");
system("CLS");
goto select;
multiply:
Beep(200,1000);
cout << "Enter first number:\n";
cin >> first;
cout << "Now enter the number you want to multiply it with:\n";
cin >> second;
cout << first << " multiplied by " << second << " = " << first * second << ".";
system("PAUSE");
system("CLS");
goto select;
divide:
Beep(200,1000);
cout << "Enter first number:\n";
cin >> first;
cout << "Now enter the number you want to divide it by:\n";
cin >> second;
cout << first << " divided by " << second << " = " << first / second << ".";
system("PAUSE");
system("CLS");
goto select;
}
Okay, I noticed you used a pointer reference in that code. I hate pointers, so that's pretty much a bad idea to try and give source codes to me that involve pointers. :)
And I didn't clearly get that code you gave. It's a whole different type of way I'd write a code.
EXTRA: BUT I'll admit that message box prompt is good for error checking, and I never thought of such a way to determine an input error in a calculator, so props(although the code is a bit above my level of knowledge, so if you could break it down that'd be very helpful to my progess). :)
/* Include all the code in the file "iostream" into this file. */
#include <iostream>
/* Include all the code in the file "windows.h" into this file. I do this so I can access the MessageBox function */
#include <windows.h>
/* Include all the code in the file "conio.h" into this file. I do this so I can access the getch fucntion */
#include <conio.h>
/* Starting function of my program */
int main () {
/* Use the std namespace so I can access its members without the typical syntax to access them which is std:: */
usingnamespace std;
/* A while loop to keep the program open */
while (1) {
/* Because this loop will be running a lot (If we want it to) I dynamically allocate variables so there isn't a bunch of memory hogs in the program */
/* Here I dynamically allocate an array of integers, which will hold the two numbers to calculate, and a menu selection */
int *ipInput = newint [3];
/* Ask the user for what I want */
cout << "Enter number one: ";
/* Get input */
cin >> ipInput [0];
/* Ask the user for what I want */
cout << endl << "Enter number two: ";
/* Get input */
cin >> ipInput [1];
/* Print the menu */
cout << endl << endl << "Select calculation: " << endl << "1) Add" << endl << "2) Sub" << endl << "3) Mult" << endl << "4) Div";
/* Print some more text */
cout << endl << "Input: ";
/* Get menu input */
cin >> ipInput [2];
/* Print some more text */
cout << endl << endl << "Result: ";
/* Our menu input is an int, so I use a switch statement to handle the input */
switch (ipInput [2]) {
/* If the input is 1 (add) then I output to the console window the sum of the two numbers inputed */
case 1: cout << ipInput [0] + ipInput [1]; break;
/* If the input is 2 (subtract) then I output to the console window the difference of the two numbers inputed */
case 2: cout << ipInput [0] - ipInput [1]; break;
/* If the innput is 3 (multiply) then I output to the console window the product of the two numbers inputed */
case 3: cout << ipInput [0] * ipInput [1]; break;
/* If the input is 4 (divide) then I output to the console window the quotent of the two numbers inputed. The I output a . and then the remainder. */
case 4: cout << ipInput [0] / ipInput [1] << '.' << ipInput [0] % ipInput [1]; break;
/* Default is used for all other options (not 1 - 4) so here I call the messagebox function to tellthe user that there is an invalid input */
default: MessageBox (NULL, "Invalid Input!", NULL, MB_ICONWARNING); break;
}
/* Deollacate my integer array */
delete [] ipInput;
/* Wait for the user to press a key */
getch ();
/* Clear the screen */
system ("cls"); // I try not to use system calls, but I wanted to add this here to make the program look better
}
/* Exit he applicstion */
return 0;
}
Yeah I was just showing you how I made it, if you want you can delete the lines 15 and 46 and do this right before the while loop:
int ipInput [3];
But I suggest using dynamic memory allocation when you are declaring variables in a loop or in a function that is being called a lot. Why do you hate pointers?
If we have a variable declaration in a loop, or a function that is called a lot then each time the loop is ran or the function is called the program will allocate enough memory for the variable. If we keep doing this then we will wastefully use up memory. For most small programs it isn't a issue, because they don't take up that much memory. But if you are making something like a game, that uses a lot of memory, then we might want to dynamically allocate our variables. When we do this we allocate the memory like usual, but then we delete the variable, which de-allocates the variable and frees up the memory that was used.
I was also told it has speed advantages. If you use dynamically allocated variables then they usually perform faster.
Stack allocation is faster ( http://stackoverflow.com/questions/161053/c-which-is-faster-stack-allocation-or-heap-allocation ), so what you're doing in that loop is actually slowing you down (not that it matters in a simple calculator). It's also inappropriate because a variable that only lives inside a loop iteration is just begging to be automatic (stack allocated). Just let the scope do its job. Dynamic allocation should be used when the data being allocated needs to outlive the scope in which it was allocated. Or when you have a big chunk of data which is likely to overflow the stack.
EDIT: also notice:
If we have a variable declaration in a loop, or a function that is called a lot then each time the loop is ran or the function is called the program will allocate enough memory for the variable.
An automatic variable will be created and destroyed on every loop iteration or function call, so unless your function implements a recursive process (not the case) the use of memory will be linear.
If we have a variable declaration in a loop, or a function that is called a lot then each time the loop is ran or the function is called the program will allocate enough memory for the variable.
Well, the space for all local variable is always allocated on the stack at the start time of the function (loop or not).
the position of the variables inside the functions has to do with calling the constructor/destructor (if there is one)
helpmeagainANDagain wrote:
'Nuff said....Pointers are obviously associated with the devil or evil.