# Thread: Math function code - couple probs, tips?

1. ## Math function code - couple probs, tips?

This code allows the user to pick a math function (trig funtions, log, ln), set an x range, and it displays the x,y values of the funtion in that range.

I have three problems:

typing "4" doesn't make it exit < - fixed
typing anything other than a number makes the program go wacky
even with the if statement in the switch-case function, making the minimum range less than 0 STILL does not warn the user, if they have a log function selected. <-fixed

I'd also like some tips, pointers, anything, to make it smaller and more efficient.

Code:
```// The purpose is to take an x range and then display all the values of certain mathematical functions within that range.
// Demonstrates use of switch-case statemsnts, while statemsnts, and basic classes.

#include <iostream.h>
#include <stdlib.h>
#include <math.h>

void DrawTable(int,int,int);

int main()
{
bool exit = false;
bool exit2 = false;
int opt;
int opt2;
int function = 1;
int max = 10;
int min = -10;

cout << "************************************************************\n";
cout << "**********************MATH FUNCTIONS************************\n";
cout << "************************************************************\n";
while(exit == false)
{
cout << "Please make a selection\n";

cout << "1. Select the function\n";
cout << "2. Set the x range\n";
cout << "3. Display the value table\n";
cout << "4. Exit\n";
cin >> opt;

switch(opt)
{
case(1): while(exit2 == false)
{
cout << "\nSelect one of these functions: " << endl;
cout << "1. sine\n";
cout << "2. cosine\n";
cout << "3. tangent\n";
cout << "4. log (base 10)\n";
cout << "5. natural log (base e)\n";
cin >> opt2;
switch(opt2)
{
case(1): function = 1;
exit2 = true;
break;
case(2): function = 2;
exit2 = true;
break;
case(3): function = 3;
exit2 = true;
break;
case(4): function = 4;
exit2 = true;
break;
case(5): function = 5;
exit2 = true;
break;
default: cout << "Please select again\n\n";
break;
}
}
break;

case(2): for(;;)
{
cout << "What should the minimum x value be?\n";
cin >> min;
cout << "\nWhat should the maximum x value be?\n";
cin >> max;
if ( ( min > max ) || ( ( min <= 0 ) && ( function = ( 4 || 5 ) ) ) )
{
cout << "The minimum cannot be greater than the max, or you have chosen values less than 0 for a logarithmic function!\n";
continue;
}
else break;
}
break;

case(3): DrawTable(min, max, function);
break;
case(4): exit = true;
break;
default: cout << "Please make another selection\n";
break;
}
}
system("PAUSE");
return 0;
}

void DrawTable(int mi, int ma, int func)
{
switch(func)
{
case(1): cout << "\nX\t|\tY\n";
cout << "\t|\n";
for(mi;mi<=ma; mi++)
{
cout << mi << "\t|\t" << sin(mi) << endl;
}
break;
case(2): cout << "\nX\t|\tY\n";
cout << "\t|\n";
for(mi;mi<=ma; mi++)
{
cout << mi << "\t|\t" << cos(mi) << endl;
}
break;
case(3): cout << "\nX\t|\tY\n";
cout << "\t|\n";
for(mi;mi<=ma; mi++)
{
cout << mi << "\t|\t" << tan(mi) << endl;
}
break;
case(4): cout << "\nX\t|\tY\n";
cout << "\t|\n";
for(mi;mi<=ma; mi++)
{
cout << mi << "\t|\t" << log10(mi) << endl;
}
break;
case(5): cout << "\nX\t|\tY\n";
cout << "\t|\n";
for(mi;mi<=ma; mi++)
{
cout << mi << "\t|\t" << log(mi) << endl;
}
break;
default: cout << "ERROR!\n";
}
return;
}```
This is like.. the 2nd program I've written since starting reading a book (and I'm 300 pages in the book). I thought it'd be time to apply some stuff :I know (although I would have liked to use classes, pointers, and references in there somewheres)

Thanks for any help.

2. Code:
```case(4): exit == true;
break;```

Code:
```case(4): exit = true;
break;```

3. you don't need the parentesis in the cases either

case (1):

to

case 1:

I've never actually seen anyone even use parentesis in a case statement either. I didn't know you could even do that, but I guess you can, for statements like 1+(x*3)/4+(3*2) or something.

4. Thanks for the tips so far, but there's gotta be something else! I know I didn't make a perfect program on nearly my first shot!

5. Captain Penguin, the program looks fine (except the small thing that achacha found).

You asked for some tips to make your program smaller...
Code:
```void DrawTable(int mi, int ma, int func)
{
double (*f[5])(double) = { sin, cos, tan, log10, log };

if(func > 0 && func < 6)
{
cout << "\nX\t|\tY\n\t|\n";
for(mi;mi<=ma; mi++)
cout << mi << "\t|\t" << f[func-1](mi) << endl;
}
else
cout << "ERROR" << endl;
}```

Popular pages Recent additions