Question:
How do you write a C++ program for this?
anonymous
2011-04-05 12:14:52 UTC
Write a well-documented C++ program to create the equivalent of a four-function
calculator. The program should request the user to enter a number, an operator
(data type - char), and another number. It should then carry out the specified
arithmetic operation: adding, subtracting, multiplying, or dividing the two numbers.
(Hint: use a switch statement to select the operation). For division, if the
denominator is zero, output an appropriate message.

Finally, the program should display the result in an algebraic equation format.
For examples,

num operation num = answer

5 + 6 = 11
12 / 3 = 4
Three answers:
?
2011-04-05 12:39:10 UTC
you could try this :





#include

#include



using namespace std;



int main(int argc, char *argv[])

{

int num1;

int num2;

char operation;



cout << "Ryan McMillans Calculator (C)2011.\nWarning Division Does Not Give Remainder!.\n\n";



cout << "Which operation would you like to do ? (+, -, /, *)\n";

cin >> operation;



if ((operation!='+')&&(operation!='-')&&(operation!='/')&&(operation!='*'))

{

cout << "Invalid Operation!\n";

system("PAUSE");

return EXIT_SUCCESS;

}



cout << "Enter the first operand: ";

cin >> num1;

cout << endl;





cout << "Enter the second operand: ";

cin >> num2;

cout << endl;



if (operation=='+')

{

cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;

}

else if(operation=='-')

{

cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;

}

else if(operation='/')

{

float result = (float)num1 / (float)num2;



cout << num1 << " / " << num2 << " = " << result << endl;

}

else if(operation=='*')

{

cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;

}





system("PAUSE");

return EXIT_SUCCESS;

}



RyanMcMillan(C)2011
?
2016-10-27 07:44:34 UTC
Why do not you in reality upload a temporary variable? ok, in basic terms thinknig about it, this works: (this may all be pseudo code because i am going to't bear in mind C) Variables will be referred to as var1 and var2. var1 = var1 + var2 var 2 = var1 - var2 var1 = var2 - var1 each and every step being performed after the previous one. wish it facilitates.
anonymous
2011-04-05 12:19:16 UTC
Well, you'd load up your C++ IDE and write the code and then compile it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...