Thread: Number that can not contain 8 and 9

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    Number that can not contain 8 and 9

    I want to enter a number, but the number cannot contain 8 and 9. How can I do it?
    THX.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want to enter a number, but the number cannot contain 8 and 9. How can I do it?
    I find myself repeating this over and over. Read the input as a string and validate it. Then if you need to use it as a number, convert the string.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    Can u tell me how to convert number to string and then validate it? I just a beginner in C.

    THX.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Here's a simple example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    static int validate ( char *input )
    {
      for ( ; *input != '\0'; input++ )
        if ( *input == '8' || *input == '9' )
          return EXIT_FAILURE;
      return EXIT_SUCCESS;
    }
    
    int main ( void )
    {
      char buffer[BUFSIZ];
      if ( fgets ( buffer, BUFSIZ, stdin ) != NULL ) {
        if ( validate ( buffer ) == EXIT_SUCCESS )
          (void)puts ( "It's good, work with it" );
        else
          (void)puts ( "Not good, contains an 8 or 9" );
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    What are you using tp get input? If it's something such as getc or fgets, then it's already a string. If it's scanf, then you just give it %s as a string type argument.

    As far as conversion goes, look up the itoa() function.

    starX
    www.axisoftime.com
    ---------------
    starX
    www.axisoftime.com

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    Can you briefly describe how the following code
    work. I can not understand how it work.

    Thank you very much..

    #include <stdio.h>
    #include <stdlib.h>

    static int validate ( char *input )
    {
    for ( ; *input != '\0'; input++ )
    if ( *input == '8' || *input == '9' )
    return EXIT_FAILURE;
    return EXIT_SUCCESS;
    }

    int main ( void )
    {
    char buffer[BUFSIZ];
    if ( fgets ( buffer, BUFSIZ, stdin ) != NULL )
    {
    if ( validate ( buffer ) == EXIT_SUCCESS )
    (void)puts ( "It's good, work with it" );
    else
    (void)puts ( "Not good, contains an 8 or 9" );
    }
    return EXIT_SUCCESS;
    }

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Perhaps you can be more specific. What part is difficult to understand?

    First she calls fgets() and immediately checks the return value. Then she passed the string into the validation function which returns the macro EXIT_SUCCESS if 8 & 9 are not present, EXIT_FAILURE if they are. ( EXIT_SUCCESS might represent '1' (true), and EXIT_FAILURE '0' (false). )

    The point is, in programming, you must be inquisitive. Stop saying "can you show me how?" and start saying "how can I figure this out?"...

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    No 8's or 9's sounds like you want to work in octal(base 8). If this is the case take in any numbers from the user and use:

    // Code
    printf("%o", number);

    to display it.

  9. #9
    TK
    Guest
    Here is the cheap way of doing it for beginners.

    Code:
    #include<stdio.h>
    
    int main()
    {
      int inum;
    
      printf("Enter a number: ");
      while ( ( scanf("%d",&inum) != 1) || inum == 8 || inum == 9 )
        {
          //clear input in standard input buffer
          fflush(stdin);
          printf("The number was %d, Try again: ",inum);
        }
    
      return 0;
    }

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >fflush(stdin);
    Please don't teach people this one, it's behaviour is undefined and therefore should not be used. There are better ways to clear the input buffer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    TK
    Guest
    I know about this issue however fflush() complies with ANSI C.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by TK
    I know about this issue however fflush() complies with ANSI C.
    If you knew about the issues, then why write code this way? Yes, fflush() is ANSI, BUT it is designed for OUTPUT streams, not INPUT.
    K&R2 p242
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; on an input stream effect is undefined.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    TK
    Guest
    Code:
    #include<stdio.h>
    
    int main()
    {
      int inum;
    
      printf("Enter a number: ");
      while ( ( scanf("%d%*",&inum) != 1) || inum == 8 || inum == 9 )
        {
          printf("The number was %d, Try again: ",inum);
        }
    
      return 0;
    }

  14. #14
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    Angry

    Yes, I want to use stack to convert Base-8 number to decimal.
    Below is my code but contain some errors. I tried thousand times already but still can not solve the problem. I hope someone can help me to find out the problem.

    THX.

    #include<stdio.h>
    #include<math.h>
    #include<ctype.h>
    #include<stdlib.h>
    #include"StackInterface.h"

    #define BASE 10

    void main()
    {
    int Qoutient, Number, sum=0, count=0;
    char Remainder;

    Stack_t S;

    InitializeStack(&S);

    printf("BASE-8 : ");
    scanf("%d", &Number);

    Qoutient = Number;

    do
    {
    Remainder = Qoutient % BASE * pow(8,count);
    Push(Remainder, &S);
    Qoutient/=BASE;
    count++;

    } while(Qoutient!=0);

    while(!EmptyStack(S))
    {
    Pop(&S, &Remainder);
    sum+=Remainder;

    }
    printf("BASE-10: %d\n", sum);


    }

  15. #15
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    Code:
    int validate_num(int num) {
        register int rm = 0;
        while(num) {
            rm  = num%10;
            if(rm == 9 || rm == 8){
                 return rm;//invalid .. return the num that invalidate it
            }
    
            num = num/10;
        }
        return 0; //valid
    }

Popular pages Recent additions subscribe to a feed