Switch Operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ambanks04
    New Member
    • Mar 2008
    • 8

    #1

    Switch Operator

    ok I have to do the following to a previous program

    A) Modify the program loop such that a ‘SWITCH’ structure is used to call each of the FUNCTIONS which display the individual Logical Operator Truth Tables, and
    B) Upon terminating the program, display a message as follows:
    “Thank you for using the Logical
    Operator Truth Table Display Program:
    ‘Your full name’ “

    Here's the program. Need advice on switch structure and how to displat that message
    // Asgn4.cpp
    // March 4,2008
    // 2:35 pm
    // Program that uses function to display
    // truth tables for the Logical And,
    // Logical Or, and Logical Not operators

    #include <iostream>
    using std::cout; // outputs data
    using std::endl; // endline
    using std::cin; //inputs data
    using std::boolalpha; // causes bool values to print as "true" or "false"
    void AND ()
    void OR ()
    void NOT ()

    // program has to display truth table at user's will and loop until user quits
    int main()

    {
    int a, b; // variables to store user input

    do {
    cout << "Choose one truth table to display:";
    cin >> a; // input for truth table to display
    if ( a == 1)
    AND ();
    // if user chooses 1 display logical and truth table

    if ( a == 2)
    OR ();
    // if user chooses 2 display logical or truth table

    if ( a == 3 )
    NOT ();
    // if user chooses 3 display logical not truth table

    // loop until user quits
    cout<<"Do you want to quit? Press 1 to quit or 0 to display another truth table \n";
    cin>>b; // input answer for continuance
    cout<<endl;
    } while ( b!= 1 ); // input of one needed to terminate

    return 0; // indicate successful termination
    }

    void AND()
    {
    // create truth table for && (logical AND) operator
    // make function
    cout << boolalpha << "Logical AND (&&)"
    << "\nfalse && false: " << ( false && false )
    << "\nfalse && true: " << ( false && true )
    << "\ntrue && false: " << ( true && false )
    << "\ntrue && true: " << ( true && true ) << "\n\n";
    }
    void OR ()
    {
    // create truth table for || (logical OR) operator
    // make function
    cout << boolalpha << "Logical OR (||)"
    << "\nfalse || false: " << ( false || false )
    << "\nfalse || true: " << ( false || true )
    << "\ntrue || false: " << ( true || false )
    << "\ntrue || true: " << ( true || true ) << "\n\n";
    }
    void NOT ()
    {
    // create truth table for ! (logical negation) operator
    cout << boolalpha << "Logical NOT (!)"
    << "\n!false: " << ( !false )
    << "\n!true: " << ( !true ) << endl;
    cout <<"\n";
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Just:

    [code=c]
    switch(a)
    {
    case 1:
    etc...
    break;
    }
    [/code]

    You can only switch on an integer value.

    This should be in any textbook.

    Comment

    Working...