debug please | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

debug please

#include<stdio.h> int main() { char color1 , color2 ; printf("enter two colors that you want to mix\n enter 'r' for red \n enter 'b' for blue \n enter 'y' for yellow \n"); scanf("%c",&color1); scanf("%c",&color2); if(color1=="r" || color1=="b" && color2=="b" || color2=="r" ) printf("the result is purple"); else if(color1=='r' || color1=='y' &&color2=='y' || color2=='r') printf("the result is orange"); else if(color1=='b' || color1=='y' && color2=='y' || color2=='b') printf("the result is green"); else printf("error"); return(0); }

12th Mar 2019, 4:54 PM
Shikher Jaitly
Shikher Jaitly - avatar
3 Answers
+ 2
Okay, three things I see: 1.) Between your two scanfs you need the line getchar(); If you don't write it, your second char will contain the \newline you had to give by pressing enter after the first scanf (you first need to clear the input buffer). 2.) Write single ' for chars! 3.) Your conditions don't say what you think they say. && is evaluated before ||, so your first condition says this: either color 1 has to be r or both color1 and color2 have to be b, or color b has to be r. So you mixed up your ands and ors. Try to fix these three things and see what happens. (One more thing: Save your code in Playground and link it here, that's easier for a reviewer.)
12th Mar 2019, 5:41 PM
HonFu
HonFu - avatar
+ 2
Debugging is the most important training for you as a nascent coder. You are hurting your own progress if you don't do the work yourself! You should ask a specific question here *after* you've given it your best!
12th Mar 2019, 4:57 PM
HonFu
HonFu - avatar
+ 1
i know but i'm stuck badly there is a logical error which i am just unable to point out... so help me if you can please !!!!
12th Mar 2019, 5:00 PM
Shikher Jaitly
Shikher Jaitly - avatar