## C++ Simple Calculator

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

### C++ Simple Calculator

Hello!

My name is Emiljan and i just graduated high school and i am going to school for programming in the fall semester, I want to be a C++ Programmer and i am starting to learn now so i can have some experience before i start college.
Ive been learning C++ for almost two weeks now and so far i have learned enough to make a simple calculator in C++ using the CodeBlocks ide.
What i want to learn next is how to make the program loop so i doesn't exit after you chose to do one problem.
Right now it only does the option you choose once, then exits but i want to make it loop the same function or another function of your choosing, until you tell it to exit.

I need some help to make my program do what i want it to.

Code: Select all
`#include <iostream>#include <cstdlib>#include <cmath>using namespace std;int main(){    signed long  int num1, num2;                               signed long int ans;    cout<<" SIMPLE CALCULATOR! " << endl;                      cout<< endl;                                               cout<<"1. Addition " << endl;    cout<<"2. Subtraction " << endl;    cout<<"3. Multiplication " << endl;    cout<<"4. Division " << endl;    cout<<"5. Exit Program " << endl;    cout<< endl;    cout<<"Please chose a number<1-5>: " ;                       cin >> ans;    if (ans == 1)                                                {        cout<< endl;        cout<<"(Simple Addition) " << endl;        cout<<"Please enter first number: " ;        cin >> num1;        cout<<"Please enter second number: " ;        cin >> num2;        cout << endl;        cout <<"The answer is: " << num1 + num2 << endl;    }    if (ans == 2)    {        cout<< endl;        cout<<"(Simple Subtraction) " << endl;        cout<<"Please enter first number: " ;        cin >> num1;        cout<<"Please enter second number: " ;        cin >> num2;        cout << endl;        cout <<"The answer is: " << num1 - num2 << endl;    }    if (ans == 3)    {        cout<< endl;        cout<<"(Simple Multiplication) " << endl;        cout<<"Please enter first number: " ;        cin >> num1;        cout<<"Please enter second number: " ;        cin >> num2;        cout << endl;        cout <<"The answer is: " << num1 * num2 << endl;    }    if (ans == 4)    {        cout<< endl;        cout<<"(Simple Division) " << endl;        cout<<"Please enter first number: " ;        cin >> num1;        cout<<"Please enter second number: " ;        cin >> num2;        cout << endl;        cout <<"The answer is: " << num1 / num2 << endl;    }    if (ans == 5)    {        cout << endl;        cout << endl;        cout<< "Thank You! ";        exit (1);    }    system("pause");    return 0;}`
emiljan1991

Posts: 1
Joined: Tue Jun 16, 2009 8:28 am

### Re: C++ Simple Calculator

You need to use a loop. Here are two ways of doing it:
Code: Select all
`  char do_it_again;  do {    // Your code here    ...        // Ask if we should go again    cout << "Do you want to do it again?" << endl;    cin >> do_it_again;  } while(do_it_again == 'y' || do_it_again == 'Y');`

Code: Select all
`  while (1) {    // Your code here    ...        // Ask if we should go again    cout << "Do you want to do it again?" << endl;    char do_it_again;    cin >> do_it_again;    if(do_it_again != 'y' && do_it_again != 'Y')      break;  }`

If you still don't know how to write loops, try to find a tutorial or read that chapter in your book.

EDIT: If you don't know how to use `char' variables, you can read about that too, or you can ask the user to enter 0 to quit and 1 to do it again.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

### Re: C++ Simple Calculator

you can add a "while(true)" loop to keep your program is running all along.

Code: Select all
`#include <iostream>#include <cstdlib>#include <cmath>using namespace std;int main(){       while(true) {   //add here    signed long  int num1, num2;                               signed long int ans;    cout<<" SIMPLE CALCULATOR! " << endl;                      cout<< endl;                                               cout<<"1. Addition " << endl;    cout<<"2. Subtraction " << endl;    cout<<"3. Multiplication " << endl;    cout<<"4. Division " << endl;    cout<<"5. Exit Program " << endl;    cout<< endl;    cout<<"Please chose a number<1-5>: " ;                       cin >> ans;    if (ans == 1)                                                {        cout<< endl;        cout<<"(Simple Addition) " << endl;        cout<<"Please enter first number: " ;        cin >> num1;        cout<<"Please enter second number: " ;        cin >> num2;        cout << endl;        cout <<"The answer is: " << num1 + num2 << endl;    }    if (ans == 2)    {        cout<< endl;        cout<<"(Simple Subtraction) " << endl;        cout<<"Please enter first number: " ;        cin >> num1;        cout<<"Please enter second number: " ;        cin >> num2;        cout << endl;        cout <<"The answer is: " << num1 - num2 << endl;    }    if (ans == 3)    {        cout<< endl;        cout<<"(Simple Multiplication) " << endl;        cout<<"Please enter first number: " ;        cin >> num1;        cout<<"Please enter second number: " ;        cin >> num2;        cout << endl;        cout <<"The answer is: " << num1 * num2 << endl;    }    if (ans == 4)    {        cout<< endl;        cout<<"(Simple Division) " << endl;        cout<<"Please enter first number: " ;        cin >> num1;        cout<<"Please enter second number: " ;        cin >> num2;        cout << endl;        cout <<"The answer is: " << num1 / num2 << endl;    }    if (ans == 5)    {        cout << endl;        cout << endl;        cout<< "Thank You! ";        exit (1);    }    }  //end here    system("pause");    return 0;}`

Pencil

Posts: 1
Joined: Wed Jun 17, 2009 4:08 am
Location: China

### Re: C++ Simple Calculator

http://www.mycoding.net/?p=270
For my programs written in C/C++ and Java : www.mycoding.net
frozenmonkey

Posts: 5
Joined: Sun Feb 13, 2011 3:56 am

### Re: C++ Simple Calculator

hi, i can give u a different answer,, instead of loop u can use "goto" statement....
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
signed long int num1, num2;
char ans,choice; // because
start: cout<<" SIMPLE CALCULATOR! " << endl;
cout<< endl;
cout<<"2. Subtraction " << endl;
cout<<"3. Multiplication " << endl;
cout<<"4. Division " << endl;
cout<<"5. Exit Program " << endl;
cout<< endl;
cout<<"Please chose a number<1-5>: " ;
ans=getche();
if (ans == '1') // here u must use ' '
{
cout<< endl;
cout<<"Please enter first number: " ;
cin >> num1;
cout<<"Please enter second number: " ;
cin >> num2;
cout << endl;
cout <<"The answer is: " << num1 + num2 << endl;

}

else if (ans == '2')
{
cout<< endl;
cout<<"(Simple Subtraction) " << endl;
cout<<"Please enter first number: " ;
cin >> num1;
cout<<"Please enter second number: " ;
cin >> num2;
cout << endl;
cout <<"The answer is: " << num1 - num2 << endl;
}

else if (ans == '3')
{
cout<< endl;
cout<<"(Simple Multiplication) " << endl;
cout<<"Please enter first number: " ;
cin >> num1;
cout<<"Please enter second number: " ;
cin >> num2;
cout << endl;
cout <<"The answer is: " << num1 * num2 << endl;
}

else if (ans == '4')
{
cout<< endl;
cout<<"(Simple Division) " << endl;
cout<<"Please enter first number: " ;
cin >> num1;
cout<<"Please enter second number: " ;
cin >> num2;
cout << endl;
cout <<"The answer is: " << num1 / num2 << endl;
}

else if (ans == '5')
{
cout << endl;
cout << endl;
cout<< "Thank You! ";
exit (1);

}
if(ans!='5')
{
cout<<"Do u want to Do it again?press(y/n): ";
choice=getche();
if (choice=="y")
{
goto start;
}
else {
break;
}
}
system("pause");
return 0;
}
Awais Ali

Posts: 10
Joined: Wed Mar 16, 2011 8:13 am

### Re: C++ Simple Calculator

That post is about 2 years old, chances are the poster has completed that project.

Goto's are a sign of poor coding and should be avoided. Very very rarely are the useful.

ventsyv

Posts: 2810
Joined: Mon Sep 22, 2003 5:25 pm
Location: MD USA

### Re: C++ Simple Calculator

But "ventsyv" goto statement is very easy to understand,,,,
Why u r saying avoid to use goto statement ?????
give me plzzz any solid reason.....
may b i satisfied with ur answer....
Awais Ali

Posts: 10
Joined: Wed Mar 16, 2011 8:13 am

### Re: C++ Simple Calculator

It's just a sign of badly structured code. If you use goto in a program that's over a few hundred lines of code, it makes the code difficult to understand and maintain. There isn't anything that goto can do that can't be done with another construction. Google "why is goto bad" and you'll find a number of articles on the subject.

ventsyv

Posts: 2810
Joined: Mon Sep 22, 2003 5:25 pm
Location: MD USA