help formulating strcmp calls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tomerkid
    New Member
    • Jun 2010
    • 4

    help formulating strcmp calls

    Hello every body i am new to this community and new to proggraming at all i would like to get some help with a strcmp command which i have promlems with.
    well take a look at what i have and what i am try to reach:
    Code:
    void Q_3()
    {
    	char p[4]="yes";/i dont know if "yes" "no" is what i should do
    	char j[4]="no";
    	while( !strcmp(* "p",* "j"));//this is ofcoruse wrong and i dont know what should i do to make it right
    	{
    		printf("your name is ***?\n");
    		scanf("%s",&p,j);//i want the answears to be yes or no only
    		if(!strcmp("p"))//if its yes 
    		{
    			printf("hi *** i am glad to see you\n");
    		}
    		else if (j == !strcmp("j"))//if its no
    			{
    				printf("if you are not *** i dont wish to speak to you\n");
    			}
    		else//and here a restart for the while since its not one of the options:yes or no
    			{
    				printf("please answer with yes or no only!\n");
    			}
    	}
    }
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    strcmp takes 2 parameters and returns zero when they match.
    Not sure what you are trying here
    Code:
    if(!strcmp("p"))//
    Doesn't seem to make sense.
    But here
    Code:
    if (j == !strcmp("j"))//
    I think you mean
    Code:
    if(!strcmp("j",j))//
    It may help if you explain what you are trying to do

    Comment

    • tomerkid
      New Member
      • Jun 2010
      • 4

      #3
      Originally posted by code green
      strcmp takes 2 parameters and returns zero when they match.
      Not sure what you are trying here
      Code:
      if(!strcmp("p"))//
      Doesn't seem to make sense.
      But here
      Code:
      if (j == !strcmp("j"))//
      I think you mean
      Code:
      if(!strcmp("j",j))//
      It may help if you explain what you are trying to do
      ok let me tell you exactly what i am trying to do.
      Code:
      void Q_2()
      {
      	char x[5]="0";
      	while((x[0]!= 'y' && x[1] != 'e' && x[2]!= 's') && (x[0]!= 'n' && x[1]!= 'o'))
      	{
      		printf("your name is ***?\n");
      		scanf("%s",&x);
      		if(x[0]== 'y' && x[1] == 'e' && x[2]== 's')
      		{
      			printf("hi *** i am glad to see you\n");
      		}
      		else if (x[0]== 'n' && x[1]=='o')
      			{
      				printf("if you are not ** i dont wish to speak to you\n");
      			}
      		else
      			{
      				printf("please answer with yes or no only!\n");
      			}
      	}
      	 Q_3();
      }
      ok i want to do the same thing as that with strcmp is it clear enought now? if not tell me ill be even more spesific
      (and as you can see this i have created the same program in 3 diffrent ways i am looking for the fast and better way)

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        I'm sorry but I do not have time to analyse code to 'work out' your intention.

        Posting code is good but that is not an explanation.
        Can we have a little more than
        ok i want to do the same thing as that with strcmp is it clear enought now

        Comment

        • tomerkid
          New Member
          • Jun 2010
          • 4

          #5
          ok so in the code i defind a string with 5 slots called x, than i gave a while condition, as long as the first2/3 slots in the string are != y,e,s or n,o the loop will keep going ,now i scanf the x'if x == yes it will print some thing if x==no it will print something else.now if it isnt yes or no the condition in the while will stay as it is so it will loop once more untill x will be changed into yes or no. so in this case i did it with 1 string,now i want to do the same thing as that with strcmp by comapring 2 strings

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Code:
            while( !strcmp(* "p",* "j"));//this is ofcoruse wrong and i dont know what should i do to make it right 
                {
            p and j are strings. Strings are char arrays. The name of an array is the address of element 0. Therefore p and j are pointers to char. strcmp arguments are pointers to char. So..

            Code:
            while( !strcmp(p,j));//this is ofcoruse wrong and i dont know what should i do to make it right 
                {
            there's no need to dereference p and j. Objviously your compiler knows that and that's why you got the error.

            There's also no need to put quotes around p and j because they are variables.

            Comment

            • tomerkid
              New Member
              • Jun 2010
              • 4

              #7
              nvm every thing is good

              Comment

              Working...