472,969 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,969 software developers and data experts.

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
5 2371

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: nicolas | last post by:
I was very surprised by the output of the following program: #include <iostream> #include <numeric> int main() { double vals_1= { 0.5, 0.2, 0.1, 0.1, 0.1 }; double vals_2= { 0.1, 0.1, 0.1,...
1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
12
by: Elijah Bailey | last post by:
I have two char arrays of size k. I want to know which one is bigger (exactly like for instance I compare two ints/longs/etc.). What is the fastest way to do this? k <= 10 usually for my...
9
by: mahurshi | last post by:
i have a quick question i am putting a debug flag in my program (i really dont need this feature, but i figured it might be useful when i get into trouble) so i want to check if argv is the...
16
by: Kevin Goodsell | last post by:
What do you think is the best way to handle a compiler warning about comparing an unsigned value to a signed value? Cast to silence it? Disable that warning altogether? Or just live with it? On...
3
by: Ricky W. Hunt | last post by:
How does VB.NET determine comparing vs. assigning? For instance, if "checkbox1.checked = True" it only checks the value but leaves it as it whereas if you have "checkbox1.checked = True" by...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
4
by: Frank | last post by:
Hello, Developing an app where the user fills out a sometimes quite lengthy form of chkboxes, txtboxes, radbtns, etc. User responses are saved to a mySql db, which the user can later edit. When...
5
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.