CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: switch-case

  1. #1
    Join Date
    Mar 2009
    Posts
    78

    switch-case

    Hello, I just finished the following code. I would like to know if it's ok so far.

    Pseudocode

    Code:
    
    Function main
      Pass In: nothing
      Do
        Call: clear_m
        Display "Welcome to the Fun Program."
        Display "Select from the menu."
        Display a blank line.
        Display "A gets Counting Loop."
        Display "B gets Impossible."
        Display "C gets Missing Item."
        Display "D gets Odd or Even."
        Display "E gets Poem."
        Display a blank line.
        Display "Q quits the program."
        Display a blank line.
        Display "Enter the letter of your choice.".
        Display "Then hit the enter key.".
        Display a blank line.
        Display "Your choice: --> ".
        Get the response for user_choice from the keyboard
        Case of user_choice
          'A'  Call: counting_loop
          'B'  Call: impossible
          'C'  Call: missing_item
          'D'  Call: odd_even
          'E'  Call: poem
        Endcase
      While user_choice not equal to 'Q'
      Pass Out: zero to the OS
    Endfunction
    
    ********************
    
    Function counting_loop
      Pass In: nothing
      For x starts at 0, x < 5, increment x
        Display "Are we having fun?"
      Endfor
      Call: pause_m
      Pass Out: nothing
    Endfuction
    
    ********************
    
    Function impossible
      Pass In: nothing
      Display "The repeat until loop is impossible in C++."
      Call: pause_m
      Pass Out: nothing
    Endfuction
    
    ********************
    
    Function missing_item
      Pass In: nothing
      Display "This program is missing only the while loop."
      Call: pause_m
      Pass Out: nothing
    Endfuction
    
    ********************
    
    Function odd_even
      Pass In: nothing
        Display a message asking user for an integer value
        Get the user_integer from the keyboard
        If user_integer modulus 2
          Display "Your number was odd."
        Else
          Display "Your number was even."
        Endif
      Call: pause_m
      Pass Out: nothing
    Endfuction
    
    ********************
    
    Function poem
      Pass In: nothing
      Display a 4 line poem
      Call: pause_m
      Pass Out: nothing
    Endfuction
    
    ********************
    
    clear_m
    and
    pause_m
    are functions defined in the udst_monitor.h



    Code

    Code:
    // Headers and Other Technical Items
    
    #include <iostream>
    using namespace std;
    #include "C:\\Dev-Cpp\\user_library\\udst_monitor.h"
    
    // Function Prototypes
    
    void clear_m (void);
    void counting_loop (void);
    void impossible(void);
    void missing_item(void);
    void odd_even(void);
    void poem(void);
    void pause_m(void);
    
    
    // Variables
    
    char user_choice;
    int x;
    int user_integer;
    
    
    //******************************************************
    // main
    //******************************************************
    
    
    int main(void)
    {
    clear_m ();
    cout << "\nWelcome to the Fun Program ";
    cout << "\nSelect from the menu. ";
    cout << "\n                            ";
    cout << "\nA gets Counting Loop. ";
    cout << "\nB gets Impossible. ";
    cout << "\nC gets Missing Item. ";
    cout << "\nD gets Odd or Even. ";
    cout << "\nE gets Poem. ";
    cout << "\n                            ";
    cout << "\nQ quits the program. ";
    cout << "\nEnter the letter of your choice. ";
    cout << "\nThen hit the enter key. ";
    cout << "\n                            ";
    cout << "\nYour choice: ";
    cin >> user_choice;
        switch (user_choice)
        {
        case 'A': counting_loop();
                  break;
        case 'B': impossible();
                  break;
        case 'C': missing_item();
                  break;
        case 'D': odd_even();
                  break;
        case 'E': poem();
        }
        while (user_choice != 'Q')
        return 0;
      
    }
                  
    //******************************************************
    // counting_loop
    //******************************************************
    
    void counting_loop (void)
      {
        for (x = 0; x < 5;)
        {
        cout << "\nAre we having fun? ";
        x++;
        }
        pause_m();
    }
    
    //******************************************************
    // Impossible
    //******************************************************
    
    void impossible(void)
      {
      cout <<"\nThe repeat until loop is impossible in C++. ";
      pause_m();
      }
    
    //******************************************************
    // missing_item
    //******************************************************
    
    void missing_item(void)
      {
      cout << "\nThis program is missing only the while loop. ";
      pause_m();
      }
    
    //******************************************************
    // odd_even
    //******************************************************
    
    void odd_even(void)
      {
      cout << "\nPlease enter an integer. --->: ";
      cin >> user_integer;
      if (user_integer % 2)
         {
         cout <<"\nYour number was odd. ";
         }
      else
          {
          cout <<"\nYour number was even. ";
          }
      pause_m ();
      }
    //******************************************************
    // poem
    //******************************************************
    
    void poem(void)
      {
      cout << "\nA comp sci exam and such a long lab must I do, ";
      cout << "\nOn a six-final-exam-ridden week,  ";
      cout << "\nWaiting for me at my chiropractic 'schooo', ";
      cout << "\nNonetheless, I press on -- without a squeak. ";
      pause_m ();
      }

    I hope everything is ok. Is that a good way to do it?

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

    Re: switch-case

    I'm not feeling your while loop.

  3. #3
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: switch-case

    I have not read all your code, but try this:
    Code:
    do{
       cin >> user_choice;
        switch (user_choice)
        {
        case 'A': counting_loop();
                  break;
        case 'B': impossible();
                  break;
        case 'C': missing_item();
                  break;
        case 'D': odd_even();
                  break;
        case 'E': poem();
        }
    }while (user_choice != 'Q');
        return 0;

  4. #4
    Join Date
    Mar 2009
    Posts
    78

    Re: switch-case

    Quote Originally Posted by Tronfi View Post
    I have not read all your code, but try this:
    Code:
    do{
       cin >> user_choice;
        switch (user_choice)
        {
        case 'A': counting_loop();
                  break;
        case 'B': impossible();
                  break;
        case 'C': missing_item();
                  break;
        case 'D': odd_even();
                  break;
        case 'E': poem();
        }
    }while (user_choice != 'Q');
        return 0;

    Thank you for your input! Yes, that's working good.

    Also, where's the problem with the {} here?
    I don't see it

    Code:
    void counting_loop (void)
      {
        for (x = 0; x < 5;)
        {
        cout << "\nAre we having fun? ";
        x++;
        }
        pause_m();
    }

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

    Re: switch-case

    Quote Originally Posted by XodoX View Post
    Thank you for your input! Yes, that's working good.

    Also, where's the problem with the {} here?
    I don't see it

    Code:
    void counting_loop (void)
      {
        for (x = 0; x < 5;)
        {
        cout << "\nAre we having fun? ";
        x++;
        }
        pause_m();
    }
    What makes you think there's a problem there? Why is x global? Typically you'd declare it in the for statement.

  6. #6
    Join Date
    Mar 2009
    Posts
    78

    Re: switch-case

    Because it says so. "primary expression expected ebfore void". In fact, it does say that for ALL of the voids.

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

    Re: switch-case

    Quote Originally Posted by XodoX View Post
    Because it says so. "primary expression expected ebfore void". In fact, it does say that for ALL of the voids.
    Which line does it specifically point to?

    Also, you should be using single \ not double in your #include.

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