Download my calculator (beginers,comes with source code!))
Sep 6, 2011 at 5:21pm UTC
hey i thought i would give back to the community and share a calculator that can
add,subtract,multiply,divide and find the square root.Keep in mind i am a beginner myself so dont expect it to be top notch!
http://www.mediafire.com/?o8x16kr62q0grb4
I hope this helped you in some way also if your not a beginner could you see the code and improve it and tell me how.
Opean source FTW.
Sep 9, 2011 at 4:07am UTC
@learningtocode14
I liked your calculator program. I especially liked the way you made the menu a routine. I did make a few small changes to your program. for one, it's a lot easier to just add a
for a newline instead of a routine to be called. Two, using
in a program is bad practice, so I made it into a do\while loop. I also added a WaitKey() routine. I hope you're pleased with my changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
// Calculator.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" /*used for microsoft visual c++, most compilers dont need this but i do!*/
#include <iostream> /*used for everything else*/
#include <cmath> /* this only for square root*/
#include <windows.h>
using namespace std;
void options(); /*fuction prototype*/
char WaitKey();
int main()
{
float number,number2;
int ans;
do
{
options(); /*the function I made that outputs the options!*/
cout << "Your function choice? : " ;
cin >> ans; /*user input*/
system("CLS" ); /*clears what is on the screen*/
if (ans == 1) /*this says that if a person inputs 1 then add*/
{
cout << "\nAddition :\n----------" ;
cout << "\n\nEnter first number: " ;
cin >> number;
cout << "\nEnter secound number: " ;
cin >> number2;
cout << "\n\nAnswer: " << number + number2 << "\n" ;
cout << "\nPress 'ENTER' to continue..." ;
WaitKey();
}
if (ans == 2)
{
cout << "\nSubtraction :\n-------------" ;
cout << "\n\nEnter first number: " ;
cin >> number;
cout << "\nEnter secound number: " ;
cin >> number2;
cout << "\n\nAnswer: " << number - number2 << "\n" ;
cout << "\nPress 'ENTER' to continue..." ;
WaitKey();
}
if (ans == 3)
{
cout << "\nMultiplying :\n-------------" ;
cout << "\n\nEnter first number: " ;
cin >> number;
cout << "\nEnter secound number: " ;
cin >> number2;
cout << "\n\nAnswer: " << number * number2 << "\n" ;
cout << "\nPress 'ENTER' to continue..." ;
WaitKey();
}
if (ans == 4)
{
cout << "\nDivision :\n----------" ;
cout << "\n\nEnter First number: " ;
cin >> number;
cout << "\nEnter secound number: " ;
cin >> number2;
cout << "\n\nAnswer: " << number / number2 << "\n" ;
cout << "\nPress 'ENTER' to continue..." ;
WaitKey();
}
if (ans == 5)
{
cout << "\nEnter number : " ;
cin >> number;
cout << "\n\nAnswer: " << sqrt(number) << "\n" ; /*finds the square root of said number =)*/
cout << "\nPress 'ENTER' to continue..." ;
WaitKey();
}
if (ans == 6) /*if /person inputs 6 exit program*/
{
cout << "\n\nThanks for trying my program.\nPress any key to close.." ;
WaitKey();
return 0;
}
if (ans < 1 || ans > 6)
{
cout << "\nBad choice. Press 'ENTER' to continue..." ;
WaitKey();
}
//WaitKey();
system("CLS" ); /*clears what is on the screen*/
} while ( ans != 6 );
return 0;
}
void options() /*function, that displays options*/
{
cout << "1. Addition" << endl << endl;
cout << "2. Subtraction" << endl << endl;
cout << "3. Multiply" << endl << endl;
cout << "4. Division" << endl << endl;
cout << "5. Square root" << endl << endl;
cout << "6. End program" << endl << endl;
}
CHAR WaitKey ()
{
HANDLE hStdin = GetStdHandle (STD_INPUT_HANDLE);
INPUT_RECORD irInputRecord;
DWORD dwEventsRead;
CHAR cChar;
while (ReadConsoleInputA (hStdin, &irInputRecord, 1, &dwEventsRead)) /* Read key press */
if (irInputRecord.EventType == KEY_EVENT
&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_SHIFT
&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_MENU
&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_CONTROL)
{
cChar = irInputRecord.Event.KeyEvent.uChar.AsciiChar;
ReadConsoleInputA (hStdin, &irInputRecord , 1, &dwEventsRead); /* Read key release */
return cChar;
}
return EOF;
}
Of course, this program is NOT the best way to program, but it is a jumping off point.
Topic archived. No new replies allowed.