Ok... I have been reading a book on programming and some such and I wanted to make a simple function. I am doing it wrong though.
This won't let me refer to "addition" more than once or else the compiler throws a bitch fit. Why is that?
#include <cstdlib>
#include <iostream>
using namespace std;
int main ()
{
char MenuA;
cout << "please enter A for addition: ";
cin >> MenuA;
float x, y, aso;
double addition ();
{
cout << "please enter the first number to be added: ";
cin >> x;
cout << "please enter the second number to be added: ";
cin >> y;
aso = x + y;
cout << "the solution is: " << aso;
}
if ( MenuA == 'A')
{
addition;
}
else if (MenuA != 'A')
{
cout << "you must enter A";
}
return 0;
}
Name:
Anonymous2008-02-09 13:49
>>1 Ok... I have been reading a book on programming and some such
It'd better be called SICP or you're in for some trouble!
addition(); or addition(void);
The code looks strange too - Why do you define the function in the middle of main(), and check if (MenuA == 'A') after it?
Name:
Anonymous2008-02-09 14:06
ok a bit better
#include <cstdlib>
#include <iostream>
using namespace std;
double addition(void)
{
float x, y, aso;
cout << "please enter the first number to be added: ";
cin >> x;
cout << "please enter the second number to be added: ";
cin >> y;
aso = x + y;
cout << "the solution is: " << aso;
}
int main ()
{
char MenuA;
cout << "please enter A for addition: ";
cin >> MenuA;
if ( MenuA == 'A')
{
addition();
}
else if (MenuA != 'A')
{
cout << "you must enter A";
}
woooo now it runs until you don't enter 'y'
:D
#include <cstdlib>
#include <iostream>
using namespace std;
double addition(void)
{
float x, y, aso;
cout << "please enter the first number to be added: ";
cin >> x;
cout << "please enter the second number to be added: ";
cin >> y;
aso = x + y;
cout << "the solution is: " << aso << "\n";
}
int main ()
{
char yns;
char MenuA;
cout << "please enter A for addition: ";
cin >> MenuA;
if ( MenuA == 'A')
{
addition();
}
else if (MenuA != 'A')
{
cout << "you must enter A";
}
cout << "would you like to go again? (y/n): ";
cin >> yns;
do
{
addition();
cout << "would you like to go again? (y/n): ";
cin >> yns;
} while (yns == 'y');
}
#include <cstdlib>
#include <iostream>
using namespace std;
double addition(void)
{float x, y, aso; cout << "please enter the first number to be added: "; cin >> x;
cout << "please enter the second number to be added: "; cin >> y;
aso = x + y; cout << "the solution is: " << aso << "\n";}
int main ()
{
char yns;
char MenuA;
cout << "please enter A for addition: "; cin >> MenuA;
if ( MenuA == 'A'){addition();}
else if (MenuA != 'A')
{cout << "you must enter A";}
cout << "would you like to go again? (y/n): "; cin >> yns;
do{ addition(); cout << "would you like to go again? (y/n): "; cin >> yns; } while (yns == 'y');}
>>17
Switches can be optimized with jump tables, depending on the cases. No modern compiler compiler optimizes if-else
Name:
Anonymous2008-02-09 15:53
I added some functions... but the way I am trying to get the common denominator isn't accurate enough :(
#include <cstdlib>
#include <iostream>
using namespace std;
float addition(void)
{float x, y, aso; cout << "please enter the first number to be added: "; cin >> x;
cout << "please enter the second number to be added: "; cin >> y;
aso = x + y; cout << "the solution is: " << aso << "\n";}
float division (void)
{ float x, y, dso;
cout << "please enter the numerator "; cin >> x;
cout << "please enter the denominator: "; cin >> y;
dso = x/y; cout << "the solution is: " << dso << "\n";}
float subtraction (void)
{ float x, y, sso;
cout << "please enter the first number to be subtracted: "; cin >> x;
cout << "please enter the second number to be subtracted: "; cin >> y;
sso = x - y; cout << "the solution is: " << sso << "\n";}
float multiplication (void)
{ float x, y, mso;
cout << "please enter the first number to be multiplied: "; cin >> x;
cout << "please enter the second number to be multiplied: "; cin >> y;
mso = x * y; cout << "the solution is: " << mso << "\n";}
float degrees_to_radians (void)
{float radianst, radiansb;
int dgrs, cdr;
cdr = 1;
cout << "please enter degrees to be converted to radians: " ;
cin >> dgrs;
while (dgrs%cdr == 0 && 180%cdr == 0)
{cdr++;}
radianst = dgrs/cdr;
radiansb = 180/cdr;
cout << dgrs << "in radians is: " << radianst << "pi/" << radiansb << "\n";
}
int main ()
{
char yns;
char Menu;
cout << "menu: " << "\n";
cout << "A: Addition\n";
cout << "D: Division\n";
cout << "S: Subtraction\n";
cout << "M: Multiplication\n";
cout << "R: Degrees to radians\n";
cin >> Menu;
switch (Menu)
{
case 'A':
addition();
break;
case 'D':
division();
break;
case 'S':
subtraction();
break;
case 'M':
multiplication();
break;
case 'R':
degrees_to_radians();
break;
}
return main();
}
Name:
Anonymous2008-02-09 16:01
fuck this
Name:
Anonymous2008-02-09 16:04
HOLY FUCKING SHIT
Seriously, get Emacs, and paste the code in, then hit tab on every line. It will AUTO-INDENT IT FOR YOU.
Name:
Anonymous2008-02-09 16:06
>>22
indentation shouldn't really matter... if it matters that much to you go use python or something you faggot.
>>35
It's true. I actually did hit CAB once. And I lost a lot of work too. It's a good thing writing Lisp code is just as much fun the second time around. ;_;
Name:
Anonymous2008-02-10 2:40
You realize you can turn off and rebind that key combo?
When I would ever hit shift-alt-meta-super-k, I have no idea, but I know I will NEVER accidentally hit it. Ever.