Thread: Need help with string conversion

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    Need help with string conversion

    I'm not getting the correct answers for my program. Can someone help figure out what's wrong? or show me some example of how you would do it?

    It's not giving me the correct return for values that have a + or - preceeding the value, or it tells me the value is a float when it's not....

    ie. -1 returns... -1 is not an integer
    -1.2 is not a float
    1..2 is a float

    Any ideas?

    Thanks!

    If you want me to post the code let me know.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Any ideas?
    I'd guess, but I'd probably be wrong without seeing your program :-)
    *Cela*

  3. #3
    ahaplz
    Guest

    RE:Need help with string conversion

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

    int main()

    {
    char s[20];
    int i;
    float f;
    int inc = 0;
    int length = 0;
    int isFloat = 0;

    while ( strcmp (s , "EOF")!= 0){
    printf("Enter a string or EOF to end: \n");
    scanf("%s", &s);
    printf("String: %s\n" , s);

    i = 0; f = 0; isFloat = 0;

    length = strlen(s); /* determine the length of the string */

    for (inc = 0; inc < length; inc++){
    if( (s[inc] < '0' || s[inc] > '9' && s[inc] == '+' || s[inc] == '-')){
    i = 1;
    if( s[inc] != '.' )
    f = 1;
    }
    if( s[inc] == '.'){
    isFloat = 1;
    }
    }

    if ( i == 0 )
    printf("This IS an Integer: %s\n", s);
    else
    printf("The string %s IS NOT an integer\n", s);
    if (f == 0 && isFloat == 1){
    printf("This is a Float: %s\n" , s);}
    else
    printf("The string %s IS NOT a floating point number\n", s);
    if ( i == 1 && f == 1 )
    printf("The string %s IS NOT a floating point number or integer\n", s);
    }
    return 0;
    }

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>if( (s[inc] < '0' || s[inc] > '9' && s[inc] == '+' || s[inc] == '-')){
    Try this instead :-)
    Code:
    if( (s[inc] < '0' || s[inc] > '9') && s[inc] != '+' && s[inc] != '-'){
    *Cela*

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    Need help with string conversion

    Thanks Cela! That helped with the first problem regarding the positive and negative signs in front of the value...but any ideas on why the floating point number is returning that a string like
    1..2 IS a floating point number?...it should be returning that it isn't a floating point number.

    Thanks again!

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>it should be returning that it isn't a floating point number.
    All you do is test for if that character is '.', if you have several of them then it still returns true because you don't do any pattern matching. Instead of setting a flag for that, increment a counter and if it's not 1 then it's not floating point. It's not perfect, but for simple cases that should work fine :-)
    *Cela*

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    re: Need help with string conversion

    I also tried to use &&atof too, but it still gave me the same response....can you show me how I could use the counter for this?

    Thanks again!

  8. #8
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>can you show me how I could use the counter for this?
    I can do better, I can show you a more thorough way to do it :-)
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef enum {FALSE, TRUE} bool_t;
    
    static char *digits = "0123456789";
    static char *alphas = "abcdefghijklmnopqrstuvwxyz"
                          "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    static char *others = "~!@#$%^&*()|\\[]{} ,/<>?:\";'";
    
    static bool_t contains(char *, char *);
    static bool_t more_than_one(char *, char *);
    
    int main(void)
    {
      while (1)
      {
        char s[20], *p;
        bool_t is_int = FALSE, is_float = FALSE;
    
        printf("Enter a number or EOF to quit: ");
        fgets(s, sizeof s, stdin);
    
        if ((p = strchr(s, '\n')) != 0)
        {
          *p = '\0';
        }
    
        if (strcmp(s, "EOF") == 0)
        {
          break;
        }
    
        if (contains(s, alphas) || contains(s, others))
          /* Check for validity */
        {
          /* Do nothing, both are already false */
        }
        else if (contains(s, digits) && contains(s, "."))
          /* Check for simple floating-point */
        {
          if (!more_than_one(s, "."))
          {
            is_float = TRUE;
          }
        }
        else if (contains(s, digits))
          /* Check for int */
        {
          is_int = TRUE;
        }
    
        printf("%s is:\n", s);
        printf("\t%san int\n", (is_int) ? "" : "NOT ");
        printf("\t%sa float\n", (is_float) ? "" : "NOT ");
      }
    
      return 0;
    }
    
    bool_t contains(char *src, char *c)
    {
      if (strpbrk(src, c) != 0)
      {
        return TRUE;
      }
    
      return FALSE;
    }
    
    bool_t more_than_one(char *src, char *c)
    {
      char *found = strpbrk(src, c);
    
      if (found != 0 && strpbrk(found+1, c) != 0)
      {
        return TRUE;
      }
    
      return FALSE;
    }
    *Cela*

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    10
    Cela,

    Wow! Someday I'm hoping that the lightbulb will go off and I'll see what all of you programmers see :>

    It will take me a little while to analyze this but it will help me see all the logic behind it too!

    THANKS !!!(again!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM