467,081 Members | 1,118 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

Comparing C-Style Strings

I am required to read in records from a file and store them in descending
order by an customer number, which is a c-style string of length 5. I am
storing these records in a linked list. My problem is in comparing the
customer number I just read in with those in the list. I'm not sure how to
go about comparing c-style strings for greater than, less than.. here is how
I am currently trying to do it:

while( ( custinfo.number > (*itr).number ) && itr != L.end() )

I want to compare the c-style strings, and if the one read in is greater
than the one the iterator is pointing to, then increment the iterator. When
I run the program as it is shown below, the output produced is equal to the
input file, so there is NO sorting going on. Can anyone help me fix this,
or offer a better way of sorting in descending order by customer number?
The code I have so far is shown below:

#include <iomanip>
#include <iostream>
#include <fstream>
#include <list>

using namespace std;

struct info
{
char number[6]; // Customer number
char name[21]; // Customer name
float balance; // Customer balance
};

class records
{
private:
ifstream infile; // input file stream for processing
info custinfo; // holds customer info being read in

public:
records( list<info>&, list<info>::iterator );
~records();
};

records::records( list<info> &L, list<info>::iterator itr )
{
// open the file for input
infile.open( "customer.dat" );

// check to see if the file is opened
if( infile.fail() )
{
// print an error message and exit the program
cout << "Error opening customer.dat" << endl;
exit(1);
}

// the file is now open for input; gather the existing
// customer records and store them in the list
itr = L.begin();

do
{
// read in a record to the struct
infile.read( (char *) &custinfo, sizeof(custinfo) );

// add the first record
if( L.empty() )
L.push_back( custinfo );
else
{
// add the record in sequential order by customer number
itr = L.begin();
while( itr != L.end() ) //( custinfo.number > (*itr).number ) &&
itr != L.end() )
{
if( custinfo.number > (*itr).number )
{
cout << custinfo.number << " > " << (*itr).number << endl;
itr++;
}
}

// insert the record
L.insert( itr, custinfo );
}

itr = L.begin();
} while( !infile.eof() );
}

records::~records()
{
infile.close();
}

int main()
{
list<info> L;
list<info>::iterator itr;
records customer( L, itr );
// remove this
for( itr = L.begin(); itr != L.end(); itr++ )
{
cout << setw(30) << itr->number << setw(20) << itr->name <<
itr->balance << endl;
}
return 0;
}


Jul 22 '05 #1
  • viewed: 2078
Share:
5 Replies

"Curtis Gilchrist" <Re*********@charter.net> wrote in message
news:10*************@corp.supernews.com...
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My problem is in comparing the customer number I just read in with those in the list. I'm not sure how to go about comparing c-style strings for greater than, less than.. here is how I am currently trying to do it:


If all the strings have the length 5, there is no reason to use null
terminated strings; you can just use char[5].

To compare two arrays, you can use strncmp from <string.h> or
std::char_traits<char>::compare from <string>.

Jonathan
Jul 22 '05 #2
I'm sorry, I posted the wrong code on that first post.. here is the code I
currently have:

#include <iomanip>
#include <iostream>
#include <fstream>
#include <list>
#include <string>

using namespace std;

struct info
{
char number[6]; // Customer number
char name[21]; // Customer name
float balance; // Customer balance
};

class records
{
private:
ifstream infile; // input file stream for processing
info custinfo; // holds customer info being read in

public:
records( list<info>&, list<info>::iterator );
~records();
};

records::records( list<info> &L, list<info>::iterator itr )
{
// open the file for input
infile.open( "customer.dat" );

// check to see if the file is opened
if( infile.fail() )
{
// print an error message and exit the program
cout << "Error opening customer.dat" << endl;
exit(1);
}

// the file is now open for input; gather the existing
// customer records and store them in the list
itr = L.begin();

do
{
// read in a record to the struct
infile.read( (char *) &custinfo, sizeof(custinfo) );

// add the first record
if( L.empty() )
L.push_back( custinfo );
else
{
// add the record in sequential order by customer number
itr = L.begin();
while( ( custinfo.number > (*itr).number ) && itr != L.end() )
{
itr++;
}

// insert the record
L.insert( itr, custinfo );
}

itr = L.begin();
} while( !infile.eof() );
}

records::~records()
{
infile.close();
}


int main()
{
list<info> L;
list<info>::iterator itr;
records customer( L, itr );
// remove this
for( itr = L.begin(); itr != L.end(); itr++ )
{
cout << setw(30) << itr->number << setw(20) << itr->name <<
itr->balance << endl;
}
return 0;
}
Jul 22 '05 #3
> To compare two arrays, you can use strncmp from <string.h> or
std::char_traits<char>::compare from <string>.

Jonathan


I thought that was used only for testing equality?

-- Curtis
Jul 22 '05 #4

"Curtis Gilchrist" <Re*********@charter.net> wrote in message
news:10*************@corp.supernews.com...
To compare two arrays, you can use strncmp from <string.h> or
std::char_traits<char>::compare from <string>.

Jonathan


I thought that was used only for testing equality?

-- Curtis


strncmp (and strcmp and std::char_traits<char>::compare) return a
negative value if the first operand is less than the second, zero if
they are equal, and a positive value otherwise.

Jonathan
Jul 22 '05 #5
Curtis Gilchrist wrote:
To compare two arrays, you can use strncmp from <string.h> or
std::char_traits<char>::compare from <string>.

Jonathan


I thought that was used only for testing equality?

From the draft C standard:
7.21.4.2 The strcmp function

Synopsis

[#1]

#include <string.h>
int strcmp(const char *s1, const char *s2);

Description

[#2] The strcmp function compares the string pointed to by
s1 to the string pointed to by s2.

Returns

[#3] The strcmp function returns an integer greater than,
equal to, or less than zero, accordingly as the string
pointed to by s1 is greater than, equal to, or less than the
string pointed to by s2.


Brian Rodenborn
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by nicolas | last post: by
1 post views Thread by Iain | last post: by
12 posts views Thread by Elijah Bailey | last post: by
9 posts views Thread by mahurshi@gmail.com | last post: by
16 posts views Thread by Kevin Goodsell | last post: by
3 posts views Thread by Ricky W. Hunt | last post: by
19 posts views Thread by Dennis | last post: by
4 posts views Thread by Frank | last post: by
5 posts views Thread by ma740988 | last post: by
20 posts views Thread by Bill Pursell | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.