Overclock.net - An Overclocking Community - View Single Post - We lost our programming virginity
View Single Post
post #177 of (permalink) Old 11-10-2009, 02:39 PM
Havegooda
New to Overclock.net
 
Join Date: Sep 2007
Location: Seaside, CA
Posts: 6,125
Rep: 393 (Unique: 296)
Code:
#include <iostream>
using namespace std;

int add(double num1, double num2);
int sub(double num1, double num2);
int multiply(double num1, double num2);
int divide(double num1, double num2);

int main()
{
double num1, num2;
int selection;

cout << "What do you want to do?" << endl
<< "<1> Add" << endl
<< "<2> Subtract" << endl
<< "<3> Multiply" << endl
<< "<4> Divide" << endl
<< "Selection: ";
cin >> selection;

if (selection >= 1 && selection <=4)
{
cout << "First number: ";
cin >> num1;
cout << "Second number: ";
cin >> num2;
}

switch(selection)
{
case 1:
add(num1, num2);
break;
case 2:
sub(num1, num2);
break;
case 3:
multiply(num1, num2);
break;
case 4:
divide(num1, num2);
break;
default:
cout << "Learn to choose a correct number dip****" << endl;
return 0;
}

return 0;
}

int add(double num1, double num2)
{
double total;
total = num1 + num2;

cout << num1 << " + " << num2 << " is " << total << endl;

return 0;
}

int sub(double num1, double num2)
{
double total;
total = num1 - num2;

cout << num1 << " - " << num2 << " is " << total << endl;

return 0;
}

int multiply(double num1, double num2)
{
double total;
total = num1 * num2;

cout << num1 << " * " << num2 << " is " << total << endl;

return 0;
}

int divide(double num1, double num2)
{
double total;
total = num1/num2;

cout << num1 << " / " << num2 << " is " << total << endl;

return 0;
}
I was bored. Made a quick calculator. Anything glaringly wrong?

Only thing I think I may add is a dedicated output function. Gets rid of the repeated "num1 +/-/* num2 = bleh" stuff in the code.

EDIT: Fixed a little bug...

~Gooda~


Start folding HERE!
Quote:
Originally Posted by alexgheseger go_quote.gif
Wow...

I never thought i'd have to say this, but any penis pictures will net you a hefty infraction mmkay?
Engineering Sample CPUs: Q6600 B0 - Xeon E3110 (E8400) E0 - E5400 (possibly) - E8400 (possibly)
Havegooda is offline