Comparing C-Style Strings

C

Curtis Gilchrist

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;
}
 
C

Curtis Gilchrist

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;
}
 
J

Jonathan Turkanis

Curtis Gilchrist said:
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
 
J

Jonathan Turkanis

Curtis Gilchrist said:
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
 
D

Default User

Curtis said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top