Thread: Math function code - couple probs, tips?

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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, 02:46 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM