extract records

L

Les Coover

Here is input: 1 red 7, 2 blue 5, 3 blue 6, 4 blue 6 (commas
indicate different records, the fields for each record are ID, COLOR and
NUMBER)

The following source code reads the input and lists it as output, then gives
a count of the total number of records.

Can anyone tell me how to change the code to that it will list and give a
total count of only those recods where color = blue and number = 6?

/* str_test */

#include <stdio.h>

int main(void)
{
int id, number, count = 0;
char color[100];

if (scanf("%d %99s %d", &id, &color, &number) != 0){
count += +1;
printf("%d %s %d\n", id, color, number);

while (scanf("%d %99s %d", &id, &color, &number) != 0){
count += +1;
printf("%d %s %d\n", id, color, number);
}
}
printf("%s%d\n\n", " Count = ", count);
return 0;
}

Thanks,

Les
 
M

Mike Wahler

Les Coover said:
Here is input: 1 red 7, 2 blue 5, 3 blue 6, 4 blue 6 (commas
indicate different records, the fields for each record are ID, COLOR and
NUMBER)

The following source code reads the input and lists it as output, then gives
a count of the total number of records.

Can anyone tell me how to change the code to that it will list and give a
total count of only those recods where color = blue and number = 6?

/* str_test */

#include <stdio.h>

#include said:
int main(void)
{
int id, number, count = 0;
char color[100];

if (scanf("%d %99s %d", &id, &color, &number) != 0){

if (scanf("%d %99s %d", &id, color, &number) != 0){

or

if (scanf("%d %99s %d", &id, &color[0], &number) != 0){

'color' already evaluates to a pointer.

/* From your question:
/* "count of only those recods where color = blue and number = 6?" */

/* just translate that English to C: */

if((strcmp(color, "blue") == 0) && number == 6))
{
count += +1;

Hmm, in 25 years of working with C, I've never seen anyone
express an increment this way. :) It's valid, but
the idiom most folks use and recognize is:

count++;

or

++count;

Close the 'if':
} /* [move this down past whatever other statements
you want to also be filtered by the 'blue and six'
criterion] */
printf("%d %s %d\n", id, color, number);

while (scanf("%d %99s %d", &id, &color, &number) != 0){

while (scanf("%d %99s %d", &id, color, &number) != 0){


Why do you do the first scanf separately, and the rest in
the while loop? Why not do it all in the loop?


if((strcmp(color, "blue") == 0) && number == 6))
{
count += +1;

<there it is again! :)>

Close the 'if':
}
printf("%d %s %d\n", id, color, number);
}
}
printf("%s%d\n\n", " Count = ", count);
return 0;
}

-Mike
 
I

Irrwahn Grausewitz

Les Coover said:
Here is input: 1 red 7, 2 blue 5, 3 blue 6, 4 blue 6 (commas
indicate different records, the fields for each record are ID, COLOR and
NUMBER)

The following source code reads the input and lists it as output, then gives
a count of the total number of records.

Can anyone tell me how to change the code to that it will list and give a
total count of only those recods where color = blue and number = 6?

That's easy, see below.
/* str_test */

#include <stdio.h>
#include said:
int main(void)
{
int id, number, count = 0;
char color[100];

if (scanf("%d %99s %d", &id, &color, &number) != 0){
count += +1;
printf("%d %s %d\n", id, color, number);

Huh? Drop the previous three lines, they are redundant.
while (scanf("%d %99s %d", &id, &color, &number) != 0){
No, you want
while ( scanf( "%d %99s %d", &id, color, &number ) == 3 )
{
because color is already a pointer to the first element of a
character array, and you want to match exactly three elements
per record.

if ( ( strcmp( color, "blue" ) == 0 ) && ( number == 6 ) )
{
Note that the string comparision is case-sensitive.
count += +1;

Why not: count += 1;
or: ++count;
printf("%d %s %d\n", id, color, number);
}
}
printf("%s%d\n\n", " Count = ", count);
return 0;
}

After cleanup and re-indenting your code looks like this:

#include <stdio.h>
#include <string.h>

int main( void )
{
int id, number, count = 0;
char color[ 100 ];

while ( scanf("%d %99s %d", &id, color, &number) == 3 )
{
if ( ( strcmp( color, "blue" ) == 0 ) && ( number == 6 ) )
{
++count;
printf( "%d %s %d\n", id, color, number );
}
}
printf( "blue-6 count = %d\n", count );

return 0;
}

However, note that the while loop condition is still designed badly,
in that it doesn't check for all possible error conditions. The
correction is left as an exercise for the reader. ;-)

Regards

Irrwahn
 
L

Les Coover

Thank you Mike and Irrwahn

Irrwahn
However, note that the while loop condition is still designed badly,
in that it doesn't check for all possible error conditions.

Does == 3 imply that all three fields in the record are filled? And would
!= 0 continue to check records that have data in only 1 or 2 of their
fields?

By error condition do you mean notify user when a record is encountered that
does not have data in all 3 fields?

Les



Irrwahn Grausewitz said:
Les Coover said:
Here is input: 1 red 7, 2 blue 5, 3 blue 6, 4 blue 6 (commas
indicate different records, the fields for each record are ID, COLOR and
NUMBER)

The following source code reads the input and lists it as output, then gives
a count of the total number of records.

Can anyone tell me how to change the code to that it will list and give a
total count of only those recods where color = blue and number = 6?

That's easy, see below.
/* str_test */

#include <stdio.h>
#include said:
int main(void)
{
int id, number, count = 0;
char color[100];

if (scanf("%d %99s %d", &id, &color, &number) != 0){
count += +1;
printf("%d %s %d\n", id, color, number);

Huh? Drop the previous three lines, they are redundant.
while (scanf("%d %99s %d", &id, &color, &number) != 0){
No, you want
while ( scanf( "%d %99s %d", &id, color, &number ) == 3 )
{
because color is already a pointer to the first element of a
character array, and you want to match exactly three elements
per record.

if ( ( strcmp( color, "blue" ) == 0 ) && ( number == 6 ) )
{
Note that the string comparision is case-sensitive.
count += +1;

Why not: count += 1;
or: ++count;
printf("%d %s %d\n", id, color, number);
}
}
printf("%s%d\n\n", " Count = ", count);
return 0;
}

After cleanup and re-indenting your code looks like this:

#include <stdio.h>
#include <string.h>

int main( void )
{
int id, number, count = 0;
char color[ 100 ];

while ( scanf("%d %99s %d", &id, color, &number) == 3 )
{
if ( ( strcmp( color, "blue" ) == 0 ) && ( number == 6 ) )
{
++count;
printf( "%d %s %d\n", id, color, number );
}
}
printf( "blue-6 count = %d\n", count );

return 0;
}

However, note that the while loop condition is still designed badly,
in that it doesn't check for all possible error conditions. The
correction is left as an exercise for the reader. ;-)

Regards

Irrwahn
 
C

CBFalconer

Les said:
Irrwahn


Does == 3 imply that all three fields in the record are filled?
And would != 0 continue to check records that have data in only 1
or 2 of their fields?

By error condition do you mean notify user when a record is
encountered that does not have data in all 3 fields?

Please don't toppost. Please do snip extraneous matter. What is
left above makes no sense, lacking proper context.
 
M

Mike Wahler

Les Coover said:
Thank you Mike and Irrwahn

Irrwahn


Does == 3 imply that all three fields in the record are filled? And would
!= 0 continue to check records that have data in only 1 or 2 of their
fields?

scanf() returns the number of fields to which
it successfully assigns a value, or EOF for end
of file or error.

By error condition do you mean notify user when a record is encountered that
does not have data in all 3 fields?

"Error condition" means that a function or program
gives unanticipated or erroneous behavior .

-Mike
 

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

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top