input/switch statement issues

This is a discussion on input/switch statement issues within the C Programming forums, part of the General Programming Boards category; Code: /* * * Purpose: Determine if a string of 0's and/or 1's ends in 00 or 11. * * ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    20

    input/switch statement issues

    Code:
    /*
    *
    *  Purpose: Determine if a string of 0's and/or 1's ends in 00 or 11.
    *
    *   Input: String of n 0's or 1's terminated with a newline
    *
    *
    *  Output: Message:
    *
    *          Accepted (if the string ends in 00 or 11), state = (accept state) #
    *                               -or-
    *          Rejected (if the string does not end in 00 or 11), state (reject state) #
    *
    *          Caveat: If the input contains other than 0's and/or 1's, the result (output)
    *          undefined!
    *
    *  Return Values: Subject to the above caveat, if the return value is:
    *
    *          0: No input
    *          1: The last two characters were 10
    *          2: The last two characters were 01
    *          3: The last two characters were 00
    *          4: The last two characters were 11
    *
    */
    
    
    #include <stdio.h>
    
    main()
    {
    
       int state, n=1;
       char input;
    
       for(;n==1;)
    {
       state=0;
       printf("Enter a string of 0's and 1's (e.g. 101011) followed newline\n\n");
    
       while ( (input=getchar()) != '\n' )
       {
    
         /* State 0 */
           if ( state == 0 )
             if ( input == '0' )
               state=1;
             else
               state = 2;
    
           else
    
        /* State 1 */
           if ( state == 1 )
             if ( input == '0' )
               state = 3;
             else
               state = 2;
    
           else
    
        /* State 2 */
           if ( state == 2 )
             if ( input == '0' )
               state = 1;
             else
               state = 4;
    
           else
    
        /* State 3 */
           if ( state == 3 ) {
             if ( input == '1' )
               state = 2;
             }
    
          else
    
        /* State 4 */
           if ( state == 4 ) {
             if ( input =='0' )
               state = 1;
          }
      }
    
      if ( state > 2 )
        printf("Accepted, state = %i\n", state);
      else
        printf("Rejected, state = %i\n", state);
    
    
    }
    
    
    }

    I want to turn these if-else statements to switch statements/conditional expressions but i'm having a little bit of trouble.
    Any help?

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Here is an example of the switch statement in action.

    By the way : "C switch statement example". Google search. It will give you all your answers.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    20
    I've looked through google, but it's more of a if problem, I understand single character inputs, but not double...
    How does a '0' return a different answer than a '00'?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    '0' - is a char - can be assigned to variable of type char or int - can be used in case statement of the switch

    '00' - is illegal sintax

    "00" - is a string cannot be assigned to any integer type - cannot be used in the case statement of the switch
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    20
    ok thank you.
    I figured it out.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Note that you still can check user input with the switch - two characters at once - you only need a little conversion that generates a unique integer per every string you can get

    The easiest and intuitively understandable will get the following:
    Code:
     
    "00" --> 0
    "01" --> 1
    "10" --> 2
    "11" --> 3

    and will require not more than 4 simple arithmetic operations like plus, minus, shift etc (no * or / - they too time consuming for this easy task)...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Break statement issues, how to return to start of loop?
    By brdiger31 in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2007, 03:29 PM
  3. switch statement issues...(undeclared identifiers)
    By mero24 in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2005, 07:05 PM
  4. Issues with if statement
    By tameeyore in forum C Programming
    Replies: 7
    Last Post: 09-27-2004, 09:34 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21