473,769 Members | 6,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function to determine the length of an integer string

Is there any function to determine the length of an integer string?

Dec 7 '05 #1
13 3655
strlen gives the length of string irrespective of its content (ie.
integer or character)

Dec 7 '05 #2

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 '05 #3

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

Dec 7 '05 #4
#include<iostre am>
#include<fstrea m>
using namespace std;

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

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

for(minIndex=in dex+1; minIndex<length ; minIndex++)
if(list[minIndex]<list[smallestIndex])
smallestIndex=m inIndex;
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_cas t, C-style cast or function-style cast

Please Help Me!

Dec 7 '05 #5

coinjo wrote:
#include<iostre am>
#include<fstrea m>
using namespace std;

void selectionSort(i nt 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 '05 #6
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_cas t, 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
Dec 7 '05 #7
coinjo wrote:
#include<iostre am>
#include<fstrea m>
using namespace std;
http://www.parashift.com/c++-faq-lit....html#faq-27.5
void selectionSort(i nt list[], int length)
{
int index;
int smallestIndex;
int minIndex;
int temp;
http://www.parashift.com/c++-faq-lit....html#faq-27.7
for(index=0; index<length-1; index++)
http://www.parashift.com/c++-faq-lit...html#faq-13.15
{
smallestIndex=i ndex;

for(minIndex=in dex+1; minIndex<length ; minIndex++)
if(list[minIndex]<list[smallestIndex])
smallestIndex=m inIndex;

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_cas t, 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(tem p);

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[i] != 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

Dec 7 '05 #8
#include<iostre am.h>
#include<fstrea m>
#include<cstrin g>
#include<math.h >
using namespace std;

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

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

for(minIndex=in dex+1; minIndex<length ; minIndex++)
if(list[minIndex]<list[smallestIndex])
smallestIndex=m inIndex;
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<tot al_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.

Dec 8 '05 #9
#include<iostre am.h>
#include<fstrea m>
#include<cstrin g>
#include<math.h >
using namespace std;

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

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

for(minIndex=in dex+1; minIndex<length ; minIndex++)
if(list[minIndex]<list[smallestIndex])
smallestIndex=m inIndex;
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<tot al_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.

Dec 8 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4964
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
10
18610
by: Mamuninfo | last post by:
Hello, Have any function in the DB2 database that can generate unique id for each string like oracle, mysql,sybase,sqlserver database. In mysql:- select md5(concat_ws("Row name")) from tablename; Here this function generate unique id for each row of the table. Regards..
2
6328
by: pangel83 | last post by:
I've been trying for days to write a piece of VB.NET code that will read from winamp's memory space the paths of the files from the current winamp playlist. The GETPLAYLISTFILE command of the winamp API will only return a pointer to the position of the asked path. An article available on http://msmvps.com/ch21st/archive/2004/02/26.aspx provides a VB6 implementation of this, using the ReadProcessMemory Windows API command, but something...
19
2269
by: Gary Kahrau | last post by:
I want to create a function and can only get half way to my goal. dim sStr as string sStr = Entry(2,"a,b,c,d") ' Sets sStr = "b" Entry(2,sStr) = "B" ' Sets sStr = "a,B,c,d" I tried create a public property. However, the Set does not allow ByRef. How do I work around this?
11
3162
by: youngster94 | last post by:
Hey all, I've written a VB.Net app that creates picture badges complete with barcodes. The problem is that the barcode quality is not good enough to be read by scanners. I'm using the DRAWSTRING function to place the barcode on the image, but no matter what graphics settings (.InterpolationMode/.CompositingQuality etc.) I manipulate, the barcode image remains poor quality. For exaple I'm using a solidbrush that is black, but some of...
10
2303
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px values. Works in both ie and firefox. Parameters styleType = top | left | width | height toNumber = the new value of the style then you pass in as many ids as you would like.
6
1754
by: daveyand | last post by:
Hey Guys, I've stumped. I created a function that does various things to select boxes. Namely Get All selected indexes, populate array with these values
3
1856
by: =?Utf-8?B?SmFuIEhlcHBlbg==?= | last post by:
Hi, I've a question. I'm developing a windows application and for the application i need to run functions and procedures that are stored in a database. Here is an example that i tried to get working. How can i execute the function that is stored in the string expr ?
14
2937
by: nishit.gupta | last post by:
Is their any single fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" , It can also contain zero
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.