accepting +123 help

This is a discussion on accepting +123 help within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> main() { int state, n=0; char input; state=0; fputs("Enter a positive or negetive integer\n", stdout); while ( ...

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

    accepting +123 help

    Code:
    #include <stdio.h>
    
    main()
    {
    
       int state, n=0;
       char input;
       state=0;
       fputs("Enter a positive or negetive integer\n", stdout);
    
       while ( (input=getchar()) !='\n' )
       {
    
           switch ( state )
           {
    
                  case 0 : (input=='+'||'-')?(state=2):(state=1);
                           break;
                  case 1 : (isdigit(input))?(state=2):(state=3);
                           break;
                  case 2 : (isdigit(input))?(state=2):(state=3);
                           break;
                  case 3 : (isdigit(input) || input=='+' || input =='-')?(state=2):(state=5);
                           break;
                  case 4 : (isdigit(input))?(state=2):(state=5);
          }
       }
    
      if ( state == 2 )
        printf("Accepted state  = A\n");
      else
        printf("Rejected, state = R\n");
    
    
    
    return 0;
    }
    the above is supposed to accept positive or negative numbers...

    example: +12132
    or: 12345
    or: -12345
    or: 1

    it is supposed to regect all else. including

    1,111,111
    +123+1
    +
    johnyboy
    1johnyboy1
    j12345

    the program does everything it should except it accepts illegal characters as long as they arn't the last digit.

    Code:
    #include <stdio.h>
    
    main()
    {
    
       int state, n=0;
       char input;
       state=0;
       fputs("Enter a positive or negetive integer\n", stdout);
    
       while ( (input=getchar()) !='\n' )
       {
    
           switch ( state )
           {
                  case 0 : (input=='+'||'-')?(state=3):((isdigit(input))?(state=1):(state=2));
                           break;
                  case 1 : (isdigit(input))?(state=1):(state=2);
                           break;
                  case 2 : (state=2);
                           break;
                  case 3 : (isdigit(input))?(state=1):(state=5);
          }
       }
    
      if ( state == 1 )
        printf("Accepted state  = A\n");
      else
        printf("Rejected, state = R\n");
    
    
    
    return 0;
    }
    the above works better... it's only problem is that it rejects any input that is only 1 character...
    example: 1
    or 2
    or 3
    or +
    or -
    or v

    does anybody see an easy way to fix either of these programs?

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    (input=='+'||'-')
    This should be (input == '+' || input == '-') and it would also be better if you used if-else tags.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    20
    the compiler accepts (input=='+'||'-')... thats not the problem... and i could use if-else tags but that would be just as difficult... regaurdless thats not whats wrong with the program...

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Yes it is the problem. Here's why:

    In C, true is non-zero. The hyphen character can also be a number: 0x2D. Since this is non-zero you end up with a false positive here:

    (input == '+' || '-')

    Follow OnionKnight's advice.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by peanut
    the compiler accepts (input=='+'||'-')... thats not the problem... and i could use if-else tags but that would be just as difficult... regaurdless thats not whats wrong with the program...
    It is the problem. It may compile, but it doesn't mean you're right. What's the point of asking for help if you're going to ignore what people tell you?

    "Help!"
    "That's the problem."
    "Oh no, I couldn't possibly be doing something wrong!"


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    20
    why so sarcastic quzah? did i say something to offend you?

    thanks citizen and onionKnight. it does work now... i thought onionKnight was simply making a suggestion to make my code look better... but i really did want to use the switch statement regaurdless of readability and run-time. Sorry for shooting down your advice i thought it was simply a readability preference.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Don't insult folks who try to correct your screwups, whether or not they use kid gloves or an open fist. Try to see the big picture. We try to do whatever it takes to help others not screw up.

    This
    the compiler accepts (input=='+'||'-')... thats not the problem
    is an example of what not to do. You were informed of this, as well as receiving other corrections.

    Accept them all as constructive criticisms. Many more will follow throughout your learning.

    Someday when you or I write code that no one can criticize is the point at which you can feel confident that you or I may be doing things correctly.

    And...
    There are other and better and simpler and more correct ways of doing what you are attempting, but I'll reserve comment depending on your posts.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Quote Originally Posted by peanut
    why so sarcastic quzah? did i say something to offend you?

    thanks citizen and onionKnight. it does work now... i thought onionKnight was simply making a suggestion to make my code look better... but i really did want to use the switch statement regaurdless of readability and run-time. Sorry for shooting down your advice i thought it was simply a readability preference.
    I wasn't thinking about the switch, it was the conditional operators.
    Code:
    switch (state) {
        case 0:
            if (input == '+' || input == '-')
                state = 3;
            else if (isdigit(input))
                state = 1;
            else
                state = 2;
                break;
    
        case 1:
            if (isdigit(input))
                state = 1;
            else
                state = 2;
            break;
    
        case 2:
            state = 2;
            break;
    
        case 3:
            if (isdigit(input))
                state = 1;
            else
                state = 5;
            break;
    }
    Not as compact, but easier to follow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 03:22 PM
  2. Having problem with C program plz help
    By Game in forum C Programming
    Replies: 14
    Last Post: 04-12-2008, 03:47 PM
  3. Reading in data from Text file
    By fortune2k in forum C Programming
    Replies: 214
    Last Post: 04-10-2008, 12:12 PM
  4. accepting char and int
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2001, 11:48 PM
  5. Replies: 0
    Last Post: 10-22-2001, 01:02 AM

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