Post: [C++] Advanced Calculator 'x, +, /, -'
02-12-2011, 05:30 PM #1
Girby2K11
☮ ☯ ☢ ✔ ➝
(adsbygoogle = window.adsbygoogle || []).push({}); Ok i have always wanted to make my own calculator that doesn't use the method that only uses one operator e.g + or -, that is all it does. but my program asks the user to input there own operator e.g +.

heres the code i was on about at the start (the plain boring one) BTW I'm using Dev C++ because i can make an executable for you guys Smile

    #include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
double value1;
double value2;
double result;

cout << "Put two boring numbers in and ill only use +" << endl;

cin >> value1;
cout << "+" << endl;
cin >> value2;

result = value1 + value2;

cout << "= " << result << endl;


system("PAUSE");
return EXIT_SUCCESS;
}



now here is the strong advanced one =D



    #include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
double value1;
double value2;
double result;
char oOperator;


cout << "Enter a number then a operator and then another number! e.g \n1+1, 1x1, 1/1, 1-1 (x + / -) \a \n";

cin >> value1;
cin >> oOperator;
cin >> value2;

switch (oOperator)
{
case 'x':
case 'X':
case '*':
{
result = value1 * value2;
cout << result;
break;
}

case '+':
{
result = value1 + value2;
cout << result;
break;
}

case '-':
{
result = value1 - value2;
cout << result;
break;
}

case '/':
{
result = value1 / value2;
cout << result;
break;
}

default:
{
cout << "!!!Was that a mathmatical symbol?!!!";
break;
}
}
cout << "\n";
_sleep(1000);

system("PAUSE");
return 0;
}


tell me what you guys think also the download link it below.

You must login or register to view this content.
02-12-2011, 05:48 PM #2
Originally posted by Girby2K11 View Post
Ok i have always wanted to make my own calculator that doesn't use the method that only uses one operator e.g + or -, that is all it does. but my program asks the user to input there own operator e.g +.

heres the code i was on about at the start (the plain boring one) BTW I'm using Dev C++ because i can make an executable for you guys Smile

    #include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
double value1;
double value2;
double result;

cout << "Put two boring numbers in and ill only use +" << endl;

cin >> value1;
cout << "+" << endl;
cin >> value2;

result = value1 + value2;

cout << "= " << result << endl;


system("PAUSE");
return EXIT_SUCCESS;
}



now here is the strong advanced one =D



    #include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
double value1;
double value2;
double result;
char oOperator;


cout << "Enter a number then a operator and then another number! e.g \n1+1, 1x1, 1/1, 1-1 (x + / -) \a \n";

cin >> value1;
cin >> oOperator;
cin >> value2;

switch (oOperator)
{
case 'x':
case 'X':
case '*':
{
result = value1 * value2;
cout << result;
break;
}

case '+':
{
result = value1 + value2;
cout << result;
break;
}

case '-':
{
result = value1 - value2;
cout << result;
break;
}

case '/':
{
result = value1 / value2;
cout << result;
break;
}

default:
{
cout << "!!!Was that a mathmatical symbol?!!!";
break;
}
}
cout << "\n";
_sleep(1000);

system("PAUSE");
return 0;
}


tell me what you guys think also the download link it below.

You must login or register to view this content.


:P, the calculator should loop, also try integrating functions.
02-12-2011, 06:08 PM #3
Girby2K11
☮ ☯ ☢ ✔ ➝
Originally posted by TheUberFail View Post
:P, the calculator should loop, also try integrating functions.


well i tried to to a goto loop and when i done that the whol program kept bleeping like mad and would close tried to end the process that didnt works untill i click restart then aborted the restart so it closed the program. Outie
02-12-2011, 06:28 PM #4
Ritztro
I am a Game Developer
Now try and make it a scientific calculator so that you can just type in a binomial like 4+5 and it will only accept an int as the first variable, a char as the second and another int as the third. After you accompish that try moving into unknown polynomials like 4+5+6 or 4-5+6*7

The following user thanked Ritztro for this useful post:

ImTheBabyKicker
02-12-2011, 06:34 PM #5
3arc
Meow.
Here's a simple calculator i just wrote:

    
// calculator
#include <iostream>
#include <string>

using namespace std;

#define charToInt(x) ((int) (x)-4Cool Man (aka Tustin)
#define intToChar(x) ((char) (x)+4Cool Man (aka Tustin)

int main() {
string calcString;

puts("Enter string: ");
getline(cin, calcString);

int result = charToInt(calcString[0]);

for(int i = 0; i < calcString.length(); i++) {
if(calcString[i] == '+'Winky Winky {
result += charToInt(calcString[i+1]);
}
else if(calcString[i] == '-'Winky Winky {
result -= charToInt(calcString[i+1]);
}
else if(calcString[i] == '*'Winky Winky {
result *= charToInt(calcString[i+1]);
}
else if(calcString[i] == '/'Winky Winky {
result /= charToInt(calcString[i+1]);
}
else if(calcString[i] == '%'Winky Winky {
result %= charToInt(calcString[i+1]);
}
}
cout << "Result: " << result << "\n\n";
return 0;
}
02-12-2011, 08:14 PM #6
Originally posted by Girby2K11 View Post
well i tried to to a goto loop and when i done that the whol program kept bleeping like mad and would close tried to end the process that didnt works untill i click restart then aborted the restart so it closed the program. Outie


Dont ever use goto commands, they are not good and are pointless.
02-12-2011, 08:47 PM #7
Girby2K11
☮ ☯ ☢ ✔ ➝
Originally posted by TheUberFail View Post
Dont ever use goto commands, they are not good and are pointless.


what should i use instead? while?
02-12-2011, 10:33 PM #8
Originally posted by Girby2K11 View Post
what should i use instead? while?


while(x==y)
{
}
02-13-2011, 10:34 AM #9
Girby2K11
☮ ☯ ☢ ✔ ➝
Originally posted by TheUberFail View Post
while(x==y)
{
}


Ok thanks :arate:
02-13-2011, 01:05 PM #10
Originally posted by Girby2K11 View Post
Ok thanks :arate:


The code inside {} will only loop if the test expression (x==y) evaluates to true.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo