Function to determine the length of an integer string

Discussion in 'C++' started by coinjo, Dec 7, 2005.

  1. coinjo

    coinjo Guest

    Is there any function to determine the length of an integer string?
     
    coinjo, Dec 7, 2005
    #1
    1. Advertising

  2. strlen gives the length of string irrespective of its content (ie.
    integer or character)
     
    Ravi Srivastava, Dec 7, 2005
    #2
    1. Advertising

  3. coinjo

    Guest

    coinjo wrote:
    > Is there any function to determine the length of an integer string?


    What is an integer string?

    std::string has a function that returns its length.

    If you have a variable of any of the integer types, you can turn the
    number it represents into a string using a stringstream (you can even
    choose decimal, hexadecimal or octal representation by using the
    appropriate stream manipulators). Once you've done that, you can ask
    the string how long it is.

    If none of that helps, post some code to show what you are trying to
    do.

    Gavin Deane
     
    , Dec 7, 2005
    #3
  4. coinjo

    Greg Guest

    coinjo wrote:
    > Is there any function to determine the length of an integer string?


    Take the logrithm of the value (rounded down to an integer) then add
    one.

    Note that the base of the logrithm has to match the base used to
    represent the value. For example, if the number is 100 decimal, the
    equation would be:

    Log10(100)+1 = 3

    so 100 has 3 digits as a decimal number.

    Greg
     
    Greg, Dec 7, 2005
    #4
  5. coinjo

    coinjo Guest

    #include<iostream>
    #include<fstream>
    using namespace std;

    void selectionSort(int list[], int length)
    {
    int index;
    int smallestIndex;
    int minIndex;
    int temp;

    for(index=0; index<length-1; index++)
    {
    smallestIndex=index;

    for(minIndex=index+1; minIndex<length; minIndex++)
    if(list[minIndex]<list[smallestIndex])
    smallestIndex=minIndex;
    temp=list[smallestIndex];
    list[smallestIndex]=list[index];
    list[index]=temp;
    }
    };

    int main()
    {
    int s[100];
    int c=0;
    int count=0;
    ifstream a;
    a.open("in.txt");

    selectionSort(s,10);

    while(a>>s[count] && !a.eof())
    {
    count++;
    }

    c=strlen(s);
    return 0;
    }

    in.txt contains:
    9
    8
    7
    6
    5
    4
    3
    2
    1

    whether i include cstring file or not i get an error message in vc++;
    (40) : error C2664: 'strlen' : cannot convert parameter 1 from 'int
    [100]' to 'const char *'
    Types pointed to are unrelated; conversion requires
    reinterpret_cast, C-style cast or function-style cast

    Please Help Me!
     
    coinjo, Dec 7, 2005
    #5
  6. coinjo

    Guest

    coinjo wrote:
    > #include<iostream>
    > #include<fstream>
    > using namespace std;
    >
    > void selectionSort(int list[], int length)
    > {


    <snip contents of this function that operates on values in the list
    array>

    > };


    Spurious semicolon. It can go.

    > int main()
    > {
    > int s[100];
    > int c=0;
    > int count=0;
    > ifstream a;
    > a.open("in.txt");
    >
    > selectionSort(s,10);


    There is nothing in s at the moment. What do you think selectionSort is
    going to do? If it touches any of the uninitialised int varaibles in s
    (which it does) then you have undefined behaviour. Which is bad.

    > while(a>>s[count] && !a.eof())
    > {
    > count++;
    > }
    >
    > c=strlen(s);


    If we ignore the problem call to selectionSort, it looks like you have
    an int array s with the first count elements holding the values read
    from in.txt. And if I've understood, you want to know the total number
    of digits in all those ints.

    Using the technique posted by Greg, change you while loop to

    int total_digits = 0;
    while(a>>s[count] && !a.eof())
    {
    total_digits += log10(s[count]) + 1;
    count++;
    }

    You'll need to #include<math.h> to get the log10 function. And note
    that this will count decimal digits.

    Gavin Deane
     
    , Dec 7, 2005
    #6
  7. coinjo

    Default User Guest

    coinjo wrote:


    > whether i include cstring file or not i get an error message in vc++;
    > (40) : error C2664: 'strlen' : cannot convert parameter 1 from 'int
    > [100]' to 'const char *'
    > Types pointed to are unrelated; conversion requires
    > reinterpret_cast, C-style cast or function-style cast


    The strlen() function works by searching the string for the special
    text character '\0'. As 0 is a valid integer, and in no way special,
    int arrays are not terminated in that way.

    If size in important, either keep track of it or scrap arrays and use
    vectors. At your level of skill, vectors are the better choice by far.




    Brian
     
    Default User, Dec 7, 2005
    #7
  8. coinjo wrote:
    > #include<iostream>
    > #include<fstream>
    > using namespace std;


    http://www.parashift.com/c -faq-lite/coding-standards.html#faq-27.5

    > void selectionSort(int list[], int length)
    > {
    > int index;
    > int smallestIndex;
    > int minIndex;
    > int temp;


    http://www.parashift.com/c -faq-lite/coding-standards.html#faq-27.7

    > for(index=0; index<length-1; index++)


    http://www.parashift.com/c -faq-lite/operator-overloading.html#faq-13.15

    > {
    > smallestIndex=index;
    >
    > for(minIndex=index+1; minIndex<length; minIndex++)
    > if(list[minIndex]<list[smallestIndex])
    > smallestIndex=minIndex;
    >
    > temp=list[smallestIndex];
    > list[smallestIndex]=list[index];
    > list[index]=temp;


    std::swap(list[smallestIndex], list[index]);

    > }
    > };
    >
    > int main()
    > {
    > int s[100];
    > int c=0;
    > int count=0;
    > ifstream a;
    > a.open("in.txt");


    ifstream in("in.txt");

    Use constructors and descriptive names.

    > selectionSort(s,10);


    Don't you want to sort *after* reading?

    > while(a>>s[count] && !a.eof())


    This will fail if the file contains something else than integers.

    > {
    > count++;
    > }


    You could also do

    while (in >> s[count++])
    ;

    std::sort(s, s+count);

    > c=strlen(s);


    See below.

    > return 0;
    > }
    >
    > in.txt contains:
    > 9
    > 8
    > 7
    > 6
    > 5
    > 4
    > 3
    > 2
    > 1
    >
    > whether i include cstring file or not i get an error message in vc++;
    > (40) : error C2664: 'strlen' : cannot convert parameter 1 from 'int
    > [100]' to 'const char *'
    > Types pointed to are unrelated; conversion requires
    > reinterpret_cast, C-style cast or function-style cast


    An "integer string" does not exist, it makes no sense. A string is a
    sequence of characters, so that would be an "integer sequence of
    characters". strlen() works with a sequence of char's, nothing else.
    wcslen() works with a sequence of wchar_t's.

    However, a sequence of ints may be a string (for example, an
    implementation could have a character set, such as unicode, which would
    be encoded on 32-bit unsigned ints), but in this case we would not call
    this an "integer string" (because that's not what it is), but a "wide
    character string" or something like that.

    Semantically, you are not working on a "string", but on a container of
    ints. Although both could be implemented the same way
    (container-of-chars, container-of-ints), they are different.


    IIUC, you want to determine the number of integers that were read from
    the file (the "length" of your "string", or more specifically, the
    number of elements in your container). You have several ways to do
    that:

    1) use your "count" variable
    2) use a container

    std::vector<int> v;

    int temp=0;
    while (in >> temp)
    v.push_back(temp);

    int count = v.length();

    3) use an "end" value, such as 0

    int s[100] = {0};

    // read into s

    int count=0;
    for (int i=0; s != 0; ++i)
    ++count;

    But that will work only if 0 is not a valid value.

    4) Embed the number of elements in the file (as the first line for
    example)


    Jonathan
     
    Jonathan Mcdougall, Dec 7, 2005
    #8
  9. coinjo

    coinjo Guest

    #include<iostream.h>
    #include<fstream>
    #include<cstring>
    #include<math.h>
    using namespace std;

    void selectionSort(int list[], int length)
    {
    int index;
    int smallestIndex;
    int minIndex;
    int temp;

    for(index=0; index<length-1; index++)
    {
    smallestIndex=index;

    for(minIndex=index+1; minIndex<length; minIndex++)
    if(list[minIndex]<list[smallestIndex])
    smallestIndex=minIndex;
    temp=list[smallestIndex];
    list[smallestIndex]=list[index];
    list[index]=temp;
    }
    };

    void main()
    {
    int s[100];
    int c=0;
    int count=0;
    ifstream a;
    a.open("in.txt");

    int total_digits = 0;

    while(a>>s[count] && !a.eof())
    {
    total_digits += log10(s[count]) + 1;
    count++;
    }

    count=0;


    selectionSort(s,total_digits);

    while(count<total_digits)
    {
    cout<<s[count]<<endl;
    count++;
    }
    }


    Thanks to all of you for your generous help, I now have come up with
    this and as far as i thinks, it works! Please post any suggestions or
    corrections (if there are any) to this.
     
    coinjo, Dec 8, 2005
    #9
  10. coinjo

    coinjo Guest

    #include<iostream.h>
    #include<fstream>
    #include<cstring>
    #include<math.h>
    using namespace std;

    void selectionSort(int list[], int length)
    {
    int index;
    int smallestIndex;
    int minIndex;
    int temp;

    for(index=0; index<length-1; index++)
    {
    smallestIndex=index;

    for(minIndex=index+1; minIndex<length; minIndex++)
    if(list[minIndex]<list[smallestIndex])
    smallestIndex=minIndex;
    temp=list[smallestIndex];
    list[smallestIndex]=list[index];
    list[index]=temp;
    }
    };

    void main()
    {
    int s[100];
    int c=0;
    int count=0;
    ifstream a;
    a.open("in.txt");

    int total_digits = 0;

    while(a>>s[count] && !a.eof())
    {
    total_digits += log10(s[count]) + 1;
    count++;
    }

    count=0;


    selectionSort(s,total_digits);

    while(count<total_digits)
    {
    cout<<s[count]<<endl;
    count++;
    }
    }


    Thanks to all of you for your generous help, I now have come up with
    this and as far as i thinks, it works! Please post any suggestions or
    corrections (if there are any) to this.
     
    coinjo, Dec 8, 2005
    #10
  11. coinjo

    coinjo Guest

    #include<iostream.h>
    #include<fstream>
    #include<cstring>
    #include<math.h>
    using namespace std;

    void selectionSort(int list[], int length)
    {
    int index;
    int smallestIndex;
    int minIndex;
    int temp;

    for(index=0; index<length-1; index++)
    {
    smallestIndex=index;

    for(minIndex=index+1; minIndex<length; minIndex++)
    if(list[minIndex]<list[smallestIndex])
    smallestIndex=minIndex;
    temp=list[smallestIndex];
    list[smallestIndex]=list[index];
    list[index]=temp;
    }
    };

    void main()
    {
    int s[100];
    int c=0;
    int count=0;
    ifstream a;
    a.open("in.txt");

    int total_digits = 0;

    while(a>>s[count] && !a.eof())
    {
    total_digits += log10(s[count]) + 1;
    count++;
    }

    count=0;


    selectionSort(s,total_digits);

    while(count<total_digits)
    {
    cout<<s[count]<<endl;
    count++;
    }
    }


    Thanks to all of you for your generous help, I now have come up with
    this and as far as i thinks, it works! Please post any suggestions or
    corrections (if there are any) to this.
     
    coinjo, Dec 8, 2005
    #11
  12. coinjo wrote:

    A single post would have done the job, you know.

    > #include<iostream.h>


    Non standard.

    # include <iostream>

    Seriously, this won't compile with a modern compiler. Visual C++ 8
    rejects this.

    > #include<fstream>
    > #include<cstring>
    > #include<math.h>


    Prefer <cmath>

    > using namespace std;
    >
    > void selectionSort(int list[], int length)
    > {
    > int index;
    > int smallestIndex;
    > int minIndex;
    > int temp;


    This is *not* a good way to program in C++. You have been given some
    advice, you should follow it or don't post here if you think your way
    is "better".

    > for(index=0; index<length-1; index++)
    > {
    > smallestIndex=index;
    >
    > for(minIndex=index+1; minIndex<length; minIndex++)
    > if(list[minIndex]<list[smallestIndex])
    > smallestIndex=minIndex;
    > temp=list[smallestIndex];
    > list[smallestIndex]=list[index];
    > list[index]=temp;
    > }
    > };
    >
    > void main()


    Illegal, main() returns an int.

    > {
    > int s[100];
    > int c=0;
    > int count=0;
    > ifstream a;
    > a.open("in.txt");
    >
    > int total_digits = 0;
    >
    > while(a>>s[count] && !a.eof())
    > {
    > total_digits += log10(s[count]) + 1;
    > count++;
    > }
    >
    > count=0;
    >
    >
    > selectionSort(s,total_digits);


    This won't work. For example, if each number has two digits and you
    have 60 numbers, you'll read past the array. This should be

    selectionSort(s, count);

    but don't reset count to 0 before.

    > while(count<total_digits)
    > {
    > cout<<s[count]<<endl;


    Again, this is undefined behavior if total_digits is >= 100.

    > count++;
    > }
    > }
    >
    >
    > Thanks to all of you for your generous help, I now have come up with
    > this and as far as i thinks, it works! Please post any suggestions or
    > corrections (if there are any) to this.



    Jonathan
     
    Jonathan Mcdougall, Dec 8, 2005
    #12
  13. coinjo

    Guest

    , Dec 8, 2005
    #13
  14. coinjo

    Default User Guest

    coinjo wrote:


    > Thanks to all of you for your generous help, I now have come up with
    > this and as far as i thinks, it works! Please post any suggestions or
    > corrections (if there are any) to this.


    Why did you ignore most of the advice given to you?


    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Dec 8, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitchua
    Replies:
    5
    Views:
    2,777
    Eric J. Roode
    Jul 17, 2003
  2. Sam
    Replies:
    3
    Views:
    14,146
    Karl Seguin
    Feb 17, 2005
  3. Replies:
    5
    Views:
    677
    John W. Kennedy
    Jan 11, 2007
  4. Owner

    How to determine Multibyte string length.

    Owner, Apr 9, 2011, in forum: C Programming
    Replies:
    4
    Views:
    834
    Ben Bacarisse
    Apr 11, 2011
  5. Randy Kramer
    Replies:
    12
    Views:
    385
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page