string validation

This is a discussion on string validation within the C++ Programming forums, part of the General Programming Boards category; [QUOTE]Here's a section of my calculator program -> the 1st calculator crash proof wasn't neccessary but now it is, so ...

  1. #1
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57

    string validation

    [QUOTE]Here's a section of my calculator program -> the 1st calculator crash proof wasn't neccessary but now it is, so i gotta change it a bit. The problem is with the for loop, and where i do the if statement, i don't think i'm doing it properly, could someone point out the problem..thanks! (oh yea, i forget i'm trying to make it validate that the string has numbers only, and a deciamal point if present
    Code:
    char strnum01[10] ;                     
                double num01                                           
                cout << "Addition (+)";                                          
                cout << "Enter a number: ";                                          
                cin >> strnum01;                                         
                for(int i=0; strnum01[i]; i++)                                         
               {                                                
                   if(strnum01[i] = '1' || strnum01[i] = '2' || strnum01[i]                        = '3' || strnum01[i] = '4' || strnum01[i] = '5' ||                                   strnum01[i] = '6' || strnum01[i] = '7' || strnum01[i]                           = '8' || strnum01[i] = '9' || strnum01[i] = '.')                                      num01 =  double(strnum01);                                                            else   
                      cout << "Number input is invalid!");                                           }

  2. #2
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    the middle part of a for loop is a condition you have strnum01[i] with no bool operators so the compiler is seeing this as if this element is non-zero then keep looping this makes you go out of your array size at some point I would think.

  3. #3
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57
    would u have any idea of fixing this?

  4. #4
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    Code:
    char strnum01[10] ;                     
    double num01;                                           
    cout << "Addition (+)";                                          
    cout << "Enter a number: ";                                          
    cin >> strnum01;           
    bool ok=false;                              
    for(int i=0; i < strlen(strnum01); i++)                                         
    {     
       switch(strnum01[i])
       {
           case '0':
           case '1':
           case '2':
           case '3':
           case '4':
           case '5':
           case '6':
           case '7':
           case '8':
           case '9':
           case '.':
                     ok = true;
                     break;
           default:
                     ok = false;
       }
       if(!ok)break;                       
    }     
    if(!ok)cout<<"Invalid input"<<endl;
    else{num01 = double(strnum01);}
    and something else you need to use == to compare not =
    the switch does the same thing it is easier to read in this case
    Last edited by ~Kyo~; 05-20-2005 at 03:37 AM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    for(int i=0; i < strlen(strnum01); i++)
    strlen() goes thru checking for the NULL charactor in exacly the same way and it's inefficant to call it in a loop.
    In the origional post those weren't comparisons they were assignments, comparisons use the the '==' operator.

  6. #6
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57
    thanks alot, but i got one problem, the part when where double(num01)..is not working..it's complaining
    "pointer value used where a floating point value was expected "

    am i doin this part wrong?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You need to use the atof() function.

  8. #8
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    Quote Originally Posted by Quantum1024
    comparisons use the the '==' operator.
    Thats what I said? Also it is only a char array of 10 elements using strlen() won't make a large differance. Coulda used a compare for '\0' on the current element for better timing.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by ~Kyo~
    Thats what I said?
    sorry missed that.
    Also it is only a char array of 10 elements using strlen() won't make a large differance. Coulda used a compare for '\0' on the current element for better timing.
    Even so it is un-necercary.
    Thse are identical
    Code:
    for(int i=0; strnum01[i]; i++)
    and
    Code:
    for(int i=0; strnum01[i]!='\0'; i++)
    and
    Code:
    for(int i=0; strnum01[i]!=0; i++)

  10. #10
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    Quote Originally Posted by Quantum1024
    un-necercary.
    I can only guess what you mean by that.

    Using numstr01[i] to control a loop is a bad habbit. At least use a comparison to make it more obvious to what is really going on.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by ~Kyo~
    I can only guess what you mean by that.
    I mean it is unnecessary to call strlen in each iteration of the loop.

  12. #12
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by ~Kyo~
    I can only guess what you mean by that.

    Using numstr01[i] to control a loop is a bad habbit. At least use a comparison to make it more obvious to what is really going on.
    you're WRONG
    consider a string str of size N
    Code:
    //this does N^2 iterations
    for(int i=0,i<strlen(str);i++)
    //this does 2*N iterations
    int len = strlen(str);
    for(int i=0,i<len;i++)
    //this does N iterations
    for(int i=0,i<str[i];i++)
    vdk_au initial code is the best so far. Please Kyo don't post bad advices

    Quote Originally Posted by vdk_au
    Code:
                                              
    if(strnum01[i] = '1' || strnum01[i] = '2' || strnum01[i]  = '3' ||
     strnum01[i] = '4' || strnum01[i] = '5' || strnum01[i] = '6' ||
    strnum01[i] = '7' || strnum01[i]  = '8' || strnum01[i] = '9' || 
    strnum01[i] = '.')
    And this happens to much
    '=' is an assignement operator
    num[k] = '0' stores in num[k] the ascci value of '0'
    '==' IS THE COMPARISION operator
    num[k] == '0' returns true is num[k] is equal to '0', or false
    not that in C/C++ false is 0, otherwise is non-zero and true
    So the comparision may return false or not
    Using num[k] = '0' always return the final product of that atribution, value '0' which is non-zero therefore always true... that's not what you want
    Last edited by xErath; 05-20-2005 at 09:01 PM.

  13. #13
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57
    vdk_au initial code is the best so far. Please Kyo don't post bad advices
    lol...yea..but my code didn't work...so it's useless...

    the improved version kyro gave was good....but i gotta build on it...cos say if you someone types a decimal point twice, then this is a problem, or if someone types a negative sign somewhere after the 1st character..this poses a problem too...by the way, is it possible to compare characters of the same string...?

    one last thing, i assigned a character array of 10...if someone types over 10, this will crash....there's a way of preventing it crash rite?

    thanks again.u guys r real helpful..especially with the explanation!

  14. #14
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57
    actually, i came across a iswdigit() on google, tried it here but only isdigit()....i'm not if i understand is fully, but it mentioned something bout wide character for iswdigit...and i think they were saying u can check a number if it is a decimal? i was wondering if i could someone make use of this to reduce the amount of coding needed, and + the current code...doesn't fully validate as said before, someone may type 2 decimal points and it will still be valid

  15. #15
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by vdk_au
    lol...yea..but my code didn't work...so it's useless...
    read my previous post!! the answer is there

    you may read the number as 'double num; cin >> num'
    or check the string like
    • only the first char can be a '-' sign
    • there can only be one '.'
    • all other have to be digits
    • later you can try to parse an expoent, but that's more difficult

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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