Friday, July 31, 2009

C-Programming question, help plz!?

I would like to write a program that would ask me do i like pears peaches and apples. And if i say yes for all 3 questions it should say that we accept you and if one of those is no then youre not accepted, here is the code!!!!





#include %26lt;stdio.h%26gt; // needed printf()


#include %26lt;conio.h%26gt; // needed for getch()


#include %26lt;string.h%26gt; // needed for strcmpi()











void main(void)


{


char answer1[30],answer2[30],answer3[30];


printf("Do you like pears?\n");


scanf("%s",%26amp;answer1);


printf("Do you like peaches?\n");


scanf("%s",%26amp;answer2);


printf("Do you like apples?\n");


scanf("%s",%26amp;answer3);


if (strcmp(answer1,"yes")==0)


if (strcmp(answer2,"yes")==0)


if (strcmp(answer3,"yes")==0)


printf("You are just what i need!\n");


else printf("Sorry you do not apply!!\n);








getch();





}





plz help me!

C-Programming question, help plz!?
#include %26lt;stdio.h%26gt; // needed printf()





#define MAX_Q 5


#define MAX_A 30








int main(void){ // void main() is wrong.


// Don't Ever do that.


int c; // For Later


// Array of Strings


char answer[MAX_Q][MAX_A];


printf("Do you like pears?\n");


scanf("%s",%26amp;answer[0]);


printf("Do you like peaches?\n");


scanf("%s",%26amp;answer[1]);


printf("Do you like apples?\n");


scanf("%s",%26amp;answer[2]);


if( (answer[0][0]=='y'||


answer[0][0]=='Y') %26amp;%26amp;


(answer[1][0]=='y'||


answer[1][0]=='Y') %26amp;%26amp;


(answer[2][0]=='y'||


answer[2][0]=='Y') )


printf("You're just what I need!\n");


// If it starts with 'y' or 'Y', it's probably "Yes"


else


printf("Sorry you do not apply!!\n");


// You were missing a another quote earlier.








while(1) // Press x and then enter to quit


if((c=getchar())=='x')


return 0;





return 0;


}
Reply:try the %26amp;%26amp; inside the if clause





if ((strcmp(answer1,"yes")==0) %26amp;%26amp;


(strcmp(answer2,"yes")==0) %26amp;%26amp;


(strcmp(answer3,"yes")==0) %26amp;%26amp;)


printf("You are just what i need!\n");


else


printf("Sorry you do not apply!!\n);
Reply:Your else statement is not doing what you intend. It will only execute if you pass the first two ifs, then fail the third if.





Either use %26amp;%26amp;, as another answer suggested, or use brackets in all of your if statements so you can see the flaw in your code flow (always a good idea anyway).


No comments:

Post a Comment