CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2011
    Posts
    11

    switch state strings!

    Here's my code
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <string.h>
    
             using namespace std;
     
    int regular (); 
    int complex ();
             
    int main () 
    {
        
              double dNum1;
              double dNum2;
              char cSymbol;
              char cSetting;
              char cOption;
              char cComplex[3];
    
              
              cout << "Type R for Regular or C for Complex" << endl << endl;
              cin >> cSetting;
              
                 switch (cSetting)
                 {
                        case 'r':
                             regular();
                             break;
                             
                        case 'R':
                             regular();
                             break;
                        
                        case 'c':
                             complex();
                             break;
                             
                        case 'C':
                             complex();
                             break;
                 }
                  
                  
    }
    
    
    int regular ()
    {
              double dNum1;
              double dNum2;
              char cSymbol;
              char cSetting;
              char cOption;
              char cComplex[3];
              
                            system("CLS");
                   cout << "Type in your problem." << endl;
                   cout << " '1h1' for more information " << endl;
                   cin >> dNum1 >> cSymbol >> dNum2;
                  switch (cSymbol)
                        { 
                          case '+':
                          cout << (dNum1 + dNum2) << endl;
                          break;
                        
                          case '-':
                          cout << (dNum1 - dNum2) << endl;
                          break;
                        
                          case '*':
                          cout << (dNum1 * dNum2) << endl;
                          break;
                        
                          case 'x':
                          cout << (dNum1 * dNum2) << endl;
                          break;
                        
                          case 'X':
                          cout << (dNum1 * dNum2) << endl;
                          break;
                        
                          case '/':
                          cout << (dNum1 / dNum2) << endl;
                          break;
                          
                          case 'h':
                          cout << "Action:         Example Command:      Result:" << endl;
                          cout << "Addition        2+2                   4"<< endl;
                          cout << "Subtraction     2-2                   0" << endl;
                          cout << "Multiplication  2*2                   4" << endl;
                          cout << "Division        2/2                   1" << endl;
                          cout << endl;
                          
                          cout << "Press R to return or X to exit" << endl;
                          cin >> cOption;
                              
                              switch (cOption)
                               {
                               case 'r':
                               regular();
                               break;
                               
                               case 'R':
                               regular();
                               break;
                               
                               default:
                               return 0;
                               break;
                               }
                          
                        }    
        system("PAUSE");
        return 0;
    }
    
    int complex()
    {
        
              double dNum1;
              double dNum2;
              char cSymbol;
              char cSetting;
              char cOption;
              char cComplex[3];
       
                            system("CLS");
                   cout << "Type in your problem." << endl;
                   cout << " 'h1 1' for more information " << endl;
                   cin >> cComplex[3] >> dNum1 >> dNum2;
                  switch (cComplex[3])                    
                        {
                          case 'r':
                          cout << sqrt(dNum1) << endl; 
                          break;
                          
                          case 'pow':
                          cout << pow(dNum1,dNum2) << endl; 
                          break;
                          
                          case 'cos':
                          cout << cos(dNum1) << endl; 
                          break;
                          
                          case 'sin':
                          cout << sin(dNum1) << endl; 
                          break; 
                     
                          case 't':
                          cout << tan(dNum1) << endl; 
                          break;   
                          
                          case 'h':
                          cout << "Action:        Example Command:      Result:" << endl;
                          cout << "Squareroot     r4 0                  2"<< endl;
                          cout << "Power          p2 5                  32" << endl;
                          cout << "Cos            c8 0                  -0.1455" << endl;
                          cout << "Sin            s8 0                  0.989358" << endl;
                          cout << "Tan            t8 0                  -6.79971" << endl;
                          cout << endl;
                          
                          cout << "Press R to return or X to exit" << endl;
                          cin >> cOption;
                              
                              switch (cOption)
                               {
                               case 'r':
                               complex();
                               break;
                               
                               case 'R':
                               complex();
                               break;
                               
                               default:
                               return 0;
                               break;
                               }                           
                        }
        system("PAUSE");
        return 0;   
    }

    the part where it says
    Code:
    int complex()
    {
        
              double dNum1;
              double dNum2;
              char cSymbol;
              char cSetting;
              char cOption;
              char cComplex[3];
       
                            system("CLS");
                   cout << "Type in your problem." << endl;
                   cout << " 'h1 1' for more information " << endl;
                   cin >> cComplex[3] >> dNum1 >> dNum2;
                  switch (cComplex[3])                    
                        {
                          case 'r':
                          cout << sqrt(dNum1) << endl; 
                          break;
                          
                          case 'pow':
                          cout << pow(dNum1,dNum2) << endl; 
                          break;
                          
                          case 'cos':
                          cout << cos(dNum1) << endl; 
                          break;
                          
                          case 'sin':
                          cout << sin(dNum1) << endl; 
                          break; 
                     
                          case 't':
                          cout << tan(dNum1) << endl; 
                          break;
    Is a very big problem.

    I want my code to do the following action when you type in 'pow'
    Help?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: switch state strings!

    You can't do it with a switch, but an if statement would work.

    One thing you can do is combine your cases such as
    Code:
    case 'r':
    case 'R':
           regular();
           break;

  3. #3
    Join Date
    May 2011
    Posts
    11

    Re: switch state strings!

    Could you please give me an example of an if then statement?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: switch state strings!

    Quote Originally Posted by Dgameman1 View Post
    Could you please give me an example of an if then statement?
    I really don't mean to sound rude, but an if statement is really, really basic and covered in any C++ text. If you're not familiar with it, you should probably work through a good tutorial before attempting any serious programming. As I'm sure your compiler is pointing out, you have quite a few other syntactical errors. C++ doesn't lend itself to guessing very well.

  5. #5
    Join Date
    May 2011
    Posts
    11

    Re: switch state strings!

    Quote Originally Posted by GCDEF View Post
    I really don't mean to sound rude, but an if statement is really, really basic and covered in any C++ text. If you're not familiar with it, you should probably work through a good tutorial before attempting any serious programming. As I'm sure your compiler is pointing out, you have quite a few other syntactical errors. C++ doesn't lend itself to guessing very well.
    Yeah, I understand.
    I meant an if statement that would fit in that context, but I got it all figured out =P

  6. #6
    Join Date
    May 2011
    Posts
    0

    Re: switch state strings!

    If a String switch expression cannot be proven by static analysis to be interned, then the compiler will generate a call to the String's intern() method.

    Example 1

    String state variety = condition ? "fish" : "fowl";
    switch (variety) {
    case "fish":
    return 1;
    case "fowl":
    return 2;
    }
    return 3;

    In Example 1, it's clear from path analysis that variety can evaluate only to one of two String constants, and since String constants are always interned, the compiler doesn't generate code to intern the switch expression's String value.

    U want more questions Click this link http://www.coolinterview.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured