Hello, I am trying to figure out why the line \"invalid color:\" only prints to
ID: 3703102 • Letter: H
Question
Hello, I am trying to figure out why the line "invalid color:" only prints to the console when the first color is typed in wrong and not the following two colors. If I type the first color correctly and not the second, invalid color still does not print. My code is in the first two pics and the output is on the last one. And if you by chance know why that warning is being printed when I run it that would be greatly appreciated too. Thank you!
sloop.csship.edu - PuTTY include int search( char COLOR_CODES [oj int size, char target stringt): int main (void) wh char coLOR_CODES 1o] 1-"lack","bzown""zed"ozange" "gzeen" "blue" char band 18 char band 28 char band 3 int position, first band, second band, third band,; printf("Enter the colors of the zesistoz's thzee bands, begin scanf("s",band 1): printf("Band 2-" scanf("s",band 2) printf("Band 3-" scanf("s",band 3) first band-search (COLOR_CODE3, 10, band1) pzintf(ad is the addzess of the fizst coloz.In", first band) if(first band-) printf ("Invalid color: ", band 1); second band-search (COLOR_CODES, 10, band 2): pzintf(ad is the addzess of the second colozIn",second band) if (position-1) printf ("Invalid color: %s ", band 2); thira band-search (COLOR_CODES, 10, band 3); printf("%d is the addless of the thild color. ", thirdband); if (position-1) - printf ("Invalid color: %s ", band 3); sh band-2, band-3); printf ("%s return(O) s ", band-1,Explanation / Answer
The error "invalid color" doesn't appear for 2nd and 3rd color inputs because the condition being checked is wrong.
For first band, the code is
first_band = search(COLOR_CODES,10, band_1)
if(first_band== -1) //correct
printf("Invalid color : %s ",band_1);
This is correct, but for 2nd & 3rd colors..
----------------------------------------------------------------------
second_band = search(COLOR_CODES,10, band_2)
if(position== -1) //incorrect statement..
Change this to
if(second_band == -1)
--------------------------------------------------------------------------------
Even for third color
third_band = search(COLOR_CODES,10, band_1)
if(position== -1) //incorrect statement
change this to
if(third_band == -1)
---------------------------
And the warning for inputs is because,
scanf("%s",&band_3) is used
this is the same case for all the three input statements.
whenever we are taking a string as input to a char array, statement should be
scanf("%s", band_3);
no need to add "&" character as scanf already expects the argument of type pointer.
----------------------------
Make changes as told and it works !!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.