Math function code - couple probs, tips?

This is a discussion on Math function code - couple probs, tips? within the C++ Programming forums, part of the General Programming Boards category; This code allows the user to pick a math function (trig funtions, log, ln), set an x range, and it ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    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.
    Last edited by Captain Penguin; 06-17-2002 at 09:23 PM.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    66
    Code:
    case(4): exit == true;
      break;
    should read

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

  3. #3
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    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. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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 subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21