Thread: how to check for chars?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Post how to check for chars?

    Okay, I have to somehow check an integer value for a character or punctuation character. The catch is that the variable has already been read in; therefor I cannot use getch() or.. can I???

    Okay, as you can see below, I read in a value and store it as a float, after I store it as a float, I check and see if it is actually a float (double) value. If it is .0, then I know it is an integer so I convert the float to an integer and return 0 to main() to let it know that the user has entered a valid integer.

    currently I have the following which checks for float values and it works as long as a period is not entered or a character is not entered. Any ideas ? Suggestions? Again, the value is set and I cannot use getch unless I can take it from a pre-assigned variable.

    Code:
    int input_validate()  {
    int guess;
    
    double temp=12,temp2,i_part;
    
     temp2=temp;
      
     if (modf(temp,&i_part)>0.)     /*  Checks for a double value  */
       return -1;
     else  {
       guess=(int)temp2;   /*Stores the temp value into guess as Integer */
       if ((guess<100)&&(guess>=0)) 
         return 1;
       else
         return -1;
     }
    }
    Any ideas how i can check for chars and punctuation??? Thanks.

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Try reading value into a string, then parsing it and converting it using atof() or atoi().

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    RE: KEN

    Okay, would this work correctly?

    inside of a function of course:

    Code:
    int var1;
    char strg[10];
    
    
    strg=var1;
    now my question is, how would I parse each individual character from that string? I understand a for loop and or a while loop, but how would I break it apart in the first place?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>how would I parse each individual character from that string?
    This prints each character one at a time. You can change it to do whatever you want.

    Code:
    for (i = 0: i < Length; i++)
    {
      putchar(mystring[i]);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Well it seems that you could benefit from the ctype library. There you will find such functions as isalpha(), isspace() and so forth.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    string problem

    how do i get a double variable stored into a string? I cannot just let the string get thh double.

    here's what i have: assume var's initialized and declared:

    Code:
    double temp=90;
    char temp_str[5]
    temp_str=temp;
    
    /* OTHER CODE HERE */
    
    for(i=0; i<N; i++)  {
            if(isdigit(temp_str[i])!=0)
             return 0;
            else 
             return -1;
          }
    Last edited by justin69enoch; 01-28-2003 at 11:25 PM.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>how do i get a double variable stored into a string?
    Lookup sprintf()
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 05:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM