Help With Switch Statements!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • charmeda103
    New Member
    • Oct 2008
    • 36

    Help With Switch Statements!

    Im working on a program in C++ and i need to use switch statements. this is my first using switches. how to put on in my while loop. can you give me an example.
    heres what I have so far:

    [CODE]

    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    #include <cmath>



    using namespace std;

    int main()
    {





    string sampleInput;

    ifstream inFile;
    inFile.open("F: \\data3.txt");
    ofstream outFile;
    outFile.open("F :\\charges.txt" );


    while(!inFile.e of())
    {



    getline(inFile, sampleInput,'\n ');



    if ( sampleInput=="" )
    {
    cout << endl;


    }
    else
    cout << sampleInput << endl;



    }
    cout << endl;


    inFile.close();

    return 0;
    }

    [CODE]
  • archonmagnus
    New Member
    • Jun 2007
    • 113

    #2
    You need a switch variable. For example:

    [code=cpp]
    #include <iostream>

    using namespace std;

    int main (void)
    {
    short method;
    bool exiting = false;

    while (!exiting)
    {
    // Prompt the user for a method choice
    cout<<"Select:" <<endl
    <<" 1.) Method 1"<<endl
    <<" 2.) Method 2"<<endl
    <<" 3.) Method 3"<<endl
    <<" Anything else to exit"<<endl
    <<"Selection: ";

    // Read the method of choice
    cin>>method;

    // Use a switch statement for flow control
    switch (method)
    {
    case 1:
    cout<<"Method 1"<<endl;
    break;

    case 2:
    cout<<"Method 2"<<endl;
    break;

    case 3:
    cout<<"Method 3"<<endl;
    break;

    // Anything else exits
    default:
    exiting = true;
    break;
    }
    }

    return 0;
    }
    [/code]

    In the code example above, the code prompts the user for an input value. That input value is used in the switch statement to determine the program flow. In regard to the other portion of your question, the switch statement can be used in a while loop (or any other loop for that matter) in the same way as outside of a loop.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      A switch statement selects from among a set of alternatives. The criteria used to make the selection is the switch variable. There's little point in using a switch statement if you don't have alternatives to choose between.

      Were you given a problem to solve (with a hint that the switch statement would be helpful)?

      Do you need to invent some problem that demonstrates the use of the switch statement?

      Note: there are no programming situations where a switch statement is the only way to get the job done. You can do everything the switch does with an if -else if - else cascade. The main advantages of the switch statement are to people reading the code -- the structure makes it more apparent what is being attempted.

      Comment

      • curiously enough
        New Member
        • Aug 2008
        • 79

        #4
        Switch statements are useless and long, and they can't be used with certain variable types. They never should have been invented in the first place. If statements are shorter, easier, and more efficient.
        I personally have never found any use for this stupid statement.
        Conclusion: Don't use switch statements unless asked to.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by curiously enough
          Switch statements are useless and long, and they can't be used with certain variable types. They never should have been invented in the first place. If statements are shorter, easier, and more efficient.
          I personally have never found any use for this stupid statement.
          Conclusion: Don't use switch statements unless asked to.
          Despite opinions to the contrary, there are not as many cases with only One True Way to do the job as you might think. There are almost always trade-offs.

          You write a program for two quite different audiences: the maintenance programmers (ie, anybody else who might read your code) and the machine. Let's look at some ways in which the switch statement can be helpful to each audience.

          For maintenance programs (ie, people reading the code, including you) ...
          ... You only need to write the switch variable once; no risk of mistyping it anywhere in the if cascade.
          ... Logical 'OR' (where any of several values lead to the same choice) is much more concise in the switch statement than in an if cascade.
          ... The default clause can be used to trap impossible failures. The use of such traps is a hallmark of robust professional sottware. Static analysis tools like lint can warn you if you forgot to put in the default clause.
          ... When the switch variable is an enumerated type, static analysis tools like flexelint can warn you if you left any of the enumeration values out of the case clauses.
          ... When reading code casually, it is obvious that all case clauses depend on the single switch variable. If cascades must be examined more closely to determine that the same variable is tested every time.

          For the machine ...
          ... I've heard rumors that some optimizing compilers turn switch statements into jump tables, although I've never run across one myself. A jump table would be a whole lot faster than an if cascade.
          ... Duff's Device!

          Don't get me wrong, there are lots of situations where an if cascade is a better choice than switch. My code contains a lot more if statements than switch statements. I hope I've given you some reason to be a little less dismissive of the switch statement.

          Comment

          Working...