Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function to determine the length of an integer string

Reply
Thread Tools

Function to determine the length of an integer string

 
 
coinjo
Guest
Posts: n/a
 
      12-08-2005
#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.

 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      12-08-2005
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

 
Reply With Quote
 
 
 
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-08-2005

Jonathan Mcdougall wrote:

> coinjo wrote:
> > #include<math.h>

>
> Prefer <cmath>


The jury's out on that one. My preference is for <xxx.h> rather than
<cxxx>. See P J Plauger's posts in this thread

http://groups.google.co.uk/group/com...855f6a57221e72

Gavin Deane

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-08-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to determine Multibyte string length. Owner C Programming 4 04-11-2011 12:12 AM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
String exceeding length - Getting absolute string length james.w.appleby@gmail.com Java 5 01-11-2007 12:07 AM
left(string, length) or right(string, length)? Sam ASP .Net 3 02-17-2005 12:01 PM
How To Determine If A String Represents An Integer ? MrBill Java 1 08-26-2003 08:28 PM



Advertisments