By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,376 Members | 1,537 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,376 IT Pros & Developers. It's quick & easy.

extract records

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Les Coover" <lc******@cox.net.spam> wrote in message
news:cf*******************@news1.central.cox.net.. .
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 <string.h>

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
Nov 13 '05 #2

P: n/a
"Les Coover" <lc******@cox.net.spam> wrote:
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 <string.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);
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
--
Great minds run in great circles.
Nov 13 '05 #3

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote:

You missed a (
if((strcmp(color, "blue") == 0) && number == 6)) ^ here if((strcmp(color, "blue") == 0) && number == 6))

^ and here.
8^)
--
Proofread carefully to see if you any words out.
Nov 13 '05 #4

P: n/a

"Irrwahn Grausewitz" <ir*****************@freenet.de> wrote in message
news:r9********************************@4ax.com...
"Mike Wahler" <mk******@mkwahler.net> wrote:

You missed a (
if((strcmp(color, "blue") == 0) && number == 6))

^ here
if((strcmp(color, "blue") == 0) && number == 6))

^ and here.
8^)
--
Proofread carefully to see if you any words out.


No words were omitted. But punctuation was. :-)

Thanks for correction.

-Mike
Nov 13 '05 #5

P: n/a
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" <ir*****************@freenet.de> wrote in message
news:a4********************************@4ax.com... "Les Coover" <lc******@cox.net.spam> wrote:
Here is input: 1 red 7, 2 blue 5, 3 blue 6, 4 blue 6 (commasindicate 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 givesa 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 <string.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);


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
--
Great minds run in great circles.

Nov 13 '05 #6

P: n/a
Les Coover wrote:
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?


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

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #7

P: n/a
"Les Coover" <lc******@cox.net.spam> wrote in message
news:5%*******************@news1.central.cox.net.. .
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?


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
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.