Obtaining length from char * variable

This is a discussion on Obtaining length from char * variable within the C++ Programming forums, part of the General Programming Boards category; I've converted char *itemPrice to a float, but I can't seem to get the length of the char *itemPrice before ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    47

    Obtaining length from char * variable

    I've converted char *itemPrice to a float, but I can't seem to get the length of the char *itemPrice before I convert it in order to do some error checking to make sure all characters are numbers.

    Here's the code

    Code:
    int PriceItemLength;
    						
    cout << endl << " You entered option 3" << endl << endl;
    system("pause");
    system("cls");
    cout << endl << " Please enter a price: $"; 
    cin >> itemPrice1;
    
    PriceItemLength = itemPrice1.length();  // having problem here
    
    while (index < PriceItemLength)
    {
           if (itemPrice1[index] == '9' || itemPrice1[index] == '8' 
           || itemPrice1[index] == '7' || itemPrice1[index] == '6' 
           || itemPrice1[index] == '5' || itemPrice1[index] == '4' 
           || itemPrice1[index] == '3' || itemPrice1[index] == '2' 
           || itemPrice1[index] == '1' || itemPrice1[index] == '0'
           || itemPrice1[index] == '.') 
    				
           index++;									
           else
          {
                   system("cls");
                   cout << endl << " Please enter a price only!";
                   cout << endl << " Please enter a price: $"; 
                   cin >> itemPrice1;
                   index = 0; 
           }
    } // ends while loop
    
    double x; 
    x = atof( itemPrice1 );	
    cout << setprecision(2) << x;
    I can't get the length of itemPrice1 in order to do the error checking.

    bob2509

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Code:
    int i=0;
    for (i=0;char[i];i++);
    in the end i will be the length not including the null terminator

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    makes sense. Thanks!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Assuming that you've allocated enough space for itemPrice and nul terminated the string, the strlen function in <cstring> will get the length for you.

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

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    if (itemPrice1[index] == '9' || itemPrice1[index] == '8' 
           || itemPrice1[index] == '7' || itemPrice1[index] == '6' 
           || itemPrice1[index] == '5' || itemPrice1[index] == '4' 
           || itemPrice1[index] == '3' || itemPrice1[index] == '2' 
           || itemPrice1[index] == '1' || itemPrice1[index] == '0'
           || itemPrice1[index] == '.')
    God thats ugly.. how about

    if ((itemPrice1[index] <= 9 && itemPrice1 >= 0) || itemPrice1 == '.')

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    I like it... It sucks being a newbie...

  7. #7
    Unregistered
    Guest

    Talking

    This is pretty code isn't it?

    Code:
    	switch( itemPrice1[ index ] )
    	{
    	case '1':
    	case '2':
    	case '3':
    	case '4':
    	case '5':
    	case '6':
    	case '7':
    	case '8':
    	case '9':
    	case '0':
    	case '.':
    		//blah blah blah......
    	}

  8. #8
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    This is pretty code isn't it?
    For this particular situation... no

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Or even more simply...

    if( isdigit( itemPrice1[index] ) || itemPrice1[index] == '.' )
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    better yet, just clean up your code with a single function:

    Code:
    bool isNumber( char str[] )
    {
    char *s;
    
    for( s = str; *s; s++ )
    
    if( !isdigit(*s) && *s != '.' )
    retrurn false;
    
    return true;
    }
    That would let you do just:


    Code:
    						
    cout << endl << " You entered option 3" << endl << endl;
    system("pause");
    
    do{
    
    system("cls");
    cout << endl << " Please enter a price: $"; 
    cin >> itemPrice1;
    
    if( !isNumber ( itemPrice1  ) )
     {
                   system("cls");
                   cout << endl << " Please enter a price only!";
                   cout << endl << " Please enter a price: $"; 
                   cin >> itemPrice1;
                   index = 0; 
    }
    
    else break; 
    
    } while( 1 );  //...infinate loop...
    
    double x; 
    x = atof( itemPrice1 );	
    cout << setprecision(2) << x;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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