Reputation: 21
I am a beginner to C++ and pretty much programming altogether (besides a little html and css).
I have decided to start my first project for C++.
A friend recommended me trying to make a simple calculator so here is my first shot. Any pointers would be great too! Not sure exactly what I am missing, if anything, but the error I am receiving is:
1>------ Build started: Project: CalculatorFinal, Configuration: Debug Win32 ------
1> CalculatorFinal.cpp
1>c:\users\ramee\documents\visual studio 2010\projects\calculatorfinal
\calculatorfinal\calculatorfinal.cpp(32): warning C4102: 'calc' : unreferenced label
1> CalculatorFinal.vcxproj -> c:\users\ramee\documents\visual studio 2010
\Projects\CalculatorFinal\Debug\CalculatorFinal.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
my code is below (apologize if its not formatted correctly on here. This is my first post :D
// CalculatorFinal.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" // Including header
#include <iostream> // Including ioStream
using namespace std; // Namespace
void calc (double x, double y);
double result;
double n1,n2; // Declaring Variables
char q,operation;
int main()
{
cout<<"Welcome to My Calculator" <<endl; // Outputs welcome message
cout<<""<<endl; // Blank Space
cout<<"INSTRUCTIONS: Input a mathmatical equation" <<endl; // Outputs instruction
cout<<" EX: 2 + 2" <<endl; // Outputs instruction
cout<<""<<endl; // Blank Space
cout<<"Operators:"<<endl; // Outputs operation header
cout<<"For Addition, select '+'"<<endl // Outputs ADD instruction
cout<<"For Subtraction, select '-'"<<endl; // Outputs SUB instruction
cout<<"For Multiplication, select '*'"<<endl; // Outputs MUL instruction
cout<<"For Division, select '/'"<<endl; // Outputs DIV instruction
cout<<""<<endl; // Blank Space
cout<<"To clear, select 'c'"<<endl; // Outputs clear instruction
cout<<"To quit, select 'q'"<<endl; // Outputs QUIT instruction
cout<<""<<endl; // Blank Space
cout<<"Input a mathmatical equation"<<endl; // Input instructions
cin>>n1>>operation>>n2;
calc:(n1,n2);
cout<<"The answer is:"<<result<<endl;
std::cin>>q; // Input "q" to "quit"
return 0;}
void calc(double x, double y) // Operator function
{ x=n1;
y=n2;
switch(operation) // Operator swtich statement
{case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
result = x / y;
break;
default:
cout<<"Improper equation. Please input a valid mathmatical equation"<<endl;
cin>>n1>>operation>>n2;
calc (n1,n2);
}
}
Upvotes: 0
Views: 41542
Reputation: 33
Here's my take at it, without making a class. Completely new from yours.
#include <iostream>
#include <cstdlib>
using namespace std;
double a, b;
char operation;
int main()
{
cout << "Welcome to my calculator program.\n";
cout << "To make a calculation simply use the following operators -> (+ - * /).\n";
cout << "To exit, simply write an operation but replace the operator with 'q'. (eg. 2q3).\n";
while (operation != 'q'){
cin >> a >> operation >> b;
if(std::cin.fail()){
cout << "Input not numerical. Exiting...";
exit(1);
}
switch (operation)
{
case '+':
cout << " = " << a + b << '\n';
break;
case '-':
cout << " = " << a - b << '\n';
break;
case '*':
cout << " = " << a * b << '\n';
break;
case ':':
case '/':
cout << " = " << a / b << '\n';
}
}
return 0;
}
Upvotes: 0
Reputation: 45
I had coded this simple calculator using c++ so that we can add, subtract, divide or multiple as many numbers as we want. example: 2+3+4-2= and then you will get your answer.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char c;
while(true){
cout << "To solve your math problem you can use this syntex: number(+, -, / or *)number=" << endl;
cout << "you can use as many number as you want." << endl << endl;
int n, ans;
char oper;
cin >> n;
ans = n;
cin >> oper;
while(oper!='='){
cin >> n;
if(oper=='+'){
ans = ans + n;
}
if(oper=='-'){
ans = ans - n;
}
if(oper=='/'){
ans = ans/n;
}
if(oper=='*'){
ans = ans*n;
}
cin >> oper;
}
cout << "answer: " << ans << endl << endl;
cout << "Press esc to exit or press any key to continue." << endl << endl;
c=getch();
if(c==27){
break;
}
}
return 0;
}
Upvotes: 0
Reputation: 1
This is my code,it too long but support more operators
#include "stdafx.h"
#include"iostream"
#include"math.h"
#include"iomanip"
#include <string>
#include <sstream>
using namespace std;
double calc(string mystring);
double calc2(string mystring);
double factoriel(double number);
double root(double num1,double num2);
double dowork(int a,int b,string c);
int main(){
cout<<"***************************************************\n";
cout<<"* *\n";
cout<<"* calculator *\n";
cout<<"***************************************************\n\n\n";
string inpstring;
cin >> inpstring;
int length_string=inpstring.length();
double result;
if(abs(calc(inpstring))>abs(calc2(inpstring))){
result=calc(inpstring);
}
else if(abs(calc(inpstring))<=abs(calc2(inpstring))){
result=calc2(inpstring);
}
double s;
s=3.14;
cout<<"\n"<<"\tresult : "<<result<<endl;
system("pause");
}
double calc(string mystring){
int a=0;//just for switchings
int numberofop=0;
int length_string=mystring.length();
string ops;
string myop;
double param1=0;
double param2=0;
double result=0;
string first_inp;
string second_inp;
ops="+-*/^%!R";
int length_ops=ops.length();
for (int i=0;i<=length_string-1;i++){
if (i==0){
if(mystring.substr (0,1)=="-"){
continue;
}
}
for (int j=0;j<=length_ops-1;j++){
if (!(mystring.substr (i,1).compare(ops.substr(j,1)))){
numberofop++;
if (numberofop==1){
myop=ops.substr(j,1);
first_inp = mystring.substr (0,i);
second_inp = mystring.substr (i+1,length_string-1);
stringstream(first_inp) >> param1;
stringstream(second_inp) >> param2;
if (myop=="+"){
a=1;
}
else if(myop=="-"){
a=2;
}
else if(myop=="*"){
a=3;
}
else if(myop=="/"){
a=4;
}
else if(myop=="^"){
a=5;
}
else if(myop=="%"){
a=6;
}
else if(myop=="!"){
a=7;
}
else if(myop=="R"){
a=8;
}
}
}
}
}
switch (a){
case 1:
result=param1+param2;
break;
case 2:
result=param1-param2;
break;
case 3:
result=param1*param2;
break;
case 4:
result=param1/param2;
break;
case 5:
result= pow(param1,param2);
break;
case 6:
result= int(param1)% int(param2);
break;
case 7:
result= factoriel(param1);
break;
case 8:
result= root(param1,param2);
break;
}
return result;
}
double factoriel(double a){
cout<<"enter number \n";
double i=a;
double d=1;
while(i>1){
d=d*i;
i--;
}
return d;
}
double root(double num1,double num2){
double result;
double reverce;
reverce=1/num2;
result=pow(num1,reverce);
return result;
}
double calc2(string mystring){
int a=0;//just for switchings
int numberofop=0;
int length_string=mystring.length();
double pi=3.1415;
double teta;
string ops;
string myop;
double param1=0;
double param2=0;
double result=0;
string first_inp;
string second_inp;
ops="logsincostancot";
int length_ops=ops.length();
for (int i=0;i<=length_string-1;i++){
if (i==0){
if(mystring.substr (0,1)=="-"){
continue;
}
}
for (int j=0;j<=length_ops-1;j++){
if (!(mystring.substr (i,3).compare(ops.substr(j,3)))){
numberofop++;
if (numberofop==1){
myop=ops.substr(j,3);
second_inp = mystring.substr (i+3,length_string-1);
stringstream(second_inp) >> param2;
if (myop=="log"){
a=1;
}
else if(myop=="sin"){
a=2;
}
else if(myop=="cos"){
a=3;
}
else if(myop=="tan"){
a=4;
}
else if(myop=="cot"){
a=5;
}
}
}
}
}
switch (a){
case 1:
result=log(param2);
break;
case 2:
teta=(double(param2)*pi)/180;
result=sin(teta);
break;
case 3:
teta=(double(param2)*pi)/180;
result=cos(teta);
break;
case 4:
teta=(double(param2)*pi)/180;
result=tanf(teta);
break;
case 5:
teta=(double(param2)*pi)/180;
result=1/tanf(teta);
break;
}
return result;
}
double dowork(int a,int b,string c){
string cut;
cut=c.substr(a,b);
double result;
result=calc(cut);
cout<<"\nresult is "<<result;
return result;
}
Upvotes: 0
Reputation: 441
I've commented that I have changed from your original source code. and I've checked this code is working on GCC ( G++ ) compiler
Good luck!
#include "stdafx.h"
#include <iostream>
using namespace std;
void calc (double _x, double _y); // CHANGED
double result;
double n1,n2;
double x,y; // CHANGED
char q,operation;
int main()
{
cout<<"Welcome to My Calculator" <<endl;
cout<<""<<endl;
cout<<"INSTRUCTIONS: Input a mathmatical equation" <<endl;
cout<<" EX: 2 + 2" <<endl;
cout<<""<<endl;
cout<<"Operators:"<<endl;
cout<<"For Addition, select '+'"<<endl;
cout<<"For Subtraction, select '-'"<<endl;
cout<<"For Multiplication, select '*'"<<endl;
cout<<"For Division, select '/'"<<endl;
cout<<""<<endl;
cout<<"To clear, select 'c'"<<endl;
cout<<"To quit, select 'q'"<<endl;
cout<<""<<endl;
cout<<"Input a mathmatical equation"<<endl;
cin>>n1>>operation>>n2;
calc(n1,n2); // CHANGED
cout<<"The answer is:"<<result<<endl;
std::cin>>q;
return 0;
}
void calc(double _x, double _y) // CHANGED
{
x=_x; // CHANGED
y=_y; // CHANGED
switch(operation)
{case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
result = x / y;
break;
default:
cout<<"Improper equation. Please input a valid mathmatical equation"<<endl;
cin>>x>>operation>>y; // CHANGED
calc (x,y); // CHANGED
}
}
Upvotes: 0
Reputation: 355
I wouldn't call this as a C++ program. It is a mistake which almost all amateur C++ programmers make. I would call this as a C style of writing C++ programs. Please don't get me wrong but you need to start thinking in object oriented way so that you can leverage the true power of C++.
I would recommend you to make a C++ class called calculator and think on the design of the class for a bit before starting to code. I would keep methods such as Add, Subtract, Divide and so on as public and other methods as private. This would also give you a chance to enhance the calculator class in future like say adding memory support to it so that it remembers the last operation or result. Start thinking in object oriented way, in order to avoid spaghetti code which is difficult to manage later.
Upvotes: 0
Reputation: 95
Here is a calculator program I wrote based off of yours which is much nicer:
#include <iostream>
using namespace std;
//Function prototype
int solve(int, int, char);
int main()
{
//Declare variables
int solution, num1, num2;
char oper;
//Output
cout << "Calculator\n----------\n" << endl;
cout << "Syntax:\n" << endl;
cout << "1 + 3\n" << endl;
cout << "Operators: +, -, *, /\n" << endl;
cout << "Equation: ";
//Input
cin >> num1 >> oper >> num2;
//Solve and output
solution = solve(num1, num2, oper);
cout << "Answer: " << solution << endl;
//Pause [until enter key] and exit
cin.ignore(); //Enter key from last cin may be passed, ignore it.
cin.get();
return 0;
}
int solve(int num1, int num2, char oper)
{
//Switch oper
switch(oper)
{
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
default:
cout << "\nIncorrect operation! Try again: ";
cin >> num1 >> oper >> num2;
solve(num1, num2, oper);
}
}
Here are some things to watch out for, from your last program: 1) Function prototypes do not have function names [i.e void func(int)] 2) Use return values [i.e. return result;] 3) Make sure you have semi-colons.
.
.
.
.
[OLD POST: cout<<"For Addition, select '+'"<;* // Outputs ADD instruction
[No ending semi-colon]
FYI:
std::cin>>q; // Input "q" to "quit"
std:: not required here. (using namespace std;)
(remove colon in calc:(n1,n2);)
--
Your program will work now.]
Upvotes: 1