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-13-2011, 01:19 PM #11
Girby2K11
☮ ☯ ☢ ✔ ➝
Originally posted by TheUberFail View Post
The code inside {} will only loop if the test expression (x==y) evaluates to true.


i know i have learnt all of that stuff
02-13-2011, 05:51 PM #12
Originally posted by 3arc View Post
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;
}


Although this does not work for numbers bigger than 1 digit :/

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo