473,748 Members | 3,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::ite rator );
~records();
};

records::record s( list<info> &L, list<info>::ite rator itr )
{
// open the file for input
infile.open( "customer.d at" );

// 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::~recor ds()
{
infile.close();
}

int main()
{
list<info> L;
list<info>::ite rator 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 2426

"Curtis Gilchrist" <Re*********@ch arter.net> wrote in message
news:10******** *****@corp.supe rnews.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_trait s<char>::compar e 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>::ite rator );
~records();
};

records::record s( list<info> &L, list<info>::ite rator itr )
{
// open the file for input
infile.open( "customer.d at" );

// 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::~recor ds()
{
infile.close();
}


int main()
{
list<info> L;
list<info>::ite rator 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_trait s<char>::compar e from <string>.

Jonathan


I thought that was used only for testing equality?

-- Curtis
Jul 22 '05 #4

"Curtis Gilchrist" <Re*********@ch arter.net> wrote in message
news:10******** *****@corp.supe rnews.com...
To compare two arrays, you can use strncmp from <string.h> or
std::char_trait s<char>::compar e from <string>.

Jonathan


I thought that was used only for testing equality?

-- Curtis


strncmp (and strcmp and std::char_trait s<char>::compar e) 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_trait s<char>::compar e 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
7867
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, 0.2, 0.5 }; double sum1 = std::accumulate( vals_1, vals_1+5, 0. );
1
495
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 checking to see if the result has any 1s in it (if so, the arrays are different). This seems to be faster than comparing each bit of the two original arrays one at a time. But I still have to iterate over each element of the array, and I'd like to
12
885
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 application. I tried bcmp / a loop for comparison, but it seems they are very slow compared to comparing longs...Any ideas? I tried splitting the array into longs and comparing, but then I face the high endian/low endian problem on some machines.
9
5928
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 letter "d" this is what i have so far if (argv) { write_read_input_file(filename); }
16
2816
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 one hand, the warning *could* be useful. Most of the time I get it in cases where I know the comparison is safe, but it's not hard to imagine that this won't always be the case. This makes disabling it undesirable. Casting is a workable solution,...
3
1265
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 itself it changes the value. Is that correct? I believe in C you have a "=" and a "= =", one for assigning and one for comparing. Am I understanding VB.NET correctly?
19
2655
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 = Color.Empty then..... or if mycolor is Color.Empty then .......
4
3720
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 the user chooses to edit, I pull the responses from the db, toss them in a dataset, check the checks, fill the txtboxes, etc.,etc. The user then adds, deletes, or changes entries as needed and clicks the Save Changes button. Here is where the fun...
5
2217
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 I'm comparing two structs. The struct test1 has information with regards to data_size and pointer to address. The struct test2 has information with regards to data_size and value. I will compare test1 and test2. For each matching data size,...
20
2157
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 mmapped to a single page. I want to determine if the second is on the page. I'd like to do: #include "platform_appropriate_definition_of_PAGESIZE.h" int compare1(const char *a, const char *b)
0
8991
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
9544
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
8243
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
6796
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
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
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
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.