I'm fairly new to programming in general and have just started to learn C++. So as a small project to test what I know and just see what I can accomplish on my own I decided to make a simple calculator program that will compute simple addition, subtraction, multiplication and division and output the result. However, I can't figure out a way to make this program output a remainder for division problems that would have one.
Code:
#include <iostream>
using namespace std;
int Operate(int numerouno, char operation, int numerodos); //Declares function 'Operate'
int main()
{
//VARIABLES
int num1;
char affirm;
int num2;
char affirm2;
char operation;
int result;
char restart;
int loop;
loop = 1;
//INTRODUCTION
cout<<"Welcome to the Basic Calculator. In this program you will be able to add, \nsubtract, multiply or divide two numbers.\n" << endl;
//PROMPT FOR FIRST VALUE
beginagain://Restart point if user wants to re-use program at the end.
cout<<"What is your first number going to be?\n" << endl;
/*goto command from Line 32*/ onenum:cin >> num1;
//Checking for accuracy on First Number
cout<<"\nAre you sure you want to use " << num1 << " as your first number?\nPress 'y' for yes and 'n' for no.\n" << endl;
cin >> affirm;
cout<<endl;
//If User says number is not correct prompts them to re-enter it by using goto.
while (affirm == 'n')
{
cout<<"Well then re-enter your first number, please.\n" << endl;
goto onenum; //On line 17
}
//After "affirm" is 'y' - Program continues and user is informed.
cout<<"Ok! First number registered.\n"<<endl;
//Prompts for second number.
cout<<"What do you want your second number to be?\n" << endl;
twonum:cin >> num2;
//Checks for accuracy of second number.
cout<<"\nAre you sure you want to use " << num2 << " as your second number?\nPress 'y' for yes and 'n' for no.\n" << endl;
cin >> affirm2;
//User can revise their second number if answer to above question is "n"
while (affirm2 == 'n')
{
cout<<"\nOh, then please re-enter your second number.\n" << endl;
goto twonum; //On Line 41
}
//After affirm2 = y, program continues.
cout<<"\nOk! Now that we have both of your chosen numbers, what would you like to do withthem?\n"<<endl;
//Gives instructions on what to input for desired operation.
cout<<"+ for Addition\n- for Subtraction\n* for Multiplication\n/ for Division\n" << endl;
cin>>operation;
//Gets result by calling function Operate
result = Operate(num1, operation, num2);
//prints out the expression and the answer
cout<<"\n"<<num1<<operation<<num2<<"="<<result<<endl;
cout<<"WARNING: If dividing and there is a remainder, the remainder will not show."<<endl;
cout<<"\n\n\nWould you like to go back to the beginning of the Calculator?\n'Y' for yes or 'N' for no."<<endl;
cin>>restart;
if (restart=='Y')
goto beginagain;
else
loop=0;
}
//FUNCTIONS
int Operate(int num1, char operation, int num2)
{
if (operation=='+')
return num1+num2;
else if (operation=='-')
return num1-num2;
else if (operation=='*')
return num1*num2;
else if (operation=='/')
return num1/num2;
else cout<<"You suck."<<endl;
}
That's the code for a working program that does all the right functions. I'm just not sure about how to make it output a remainder value for division. I don't care if it outputs a '0' remainder for addition subtraction and multiplication as well, but I would like it to output a correct remainder for the division.