Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why this if condition is not working when i need to check string from user?

C++
#include <stdio.h>
main()
{
    char a;
    printf("Enter the Name\n");
    scanf("%d",&a);

    if(a=='xyz')
    {
        printf("You enter xyz");
    }

    if(a=='abc')
    {
        printf("You enter abc");
    }   
}


I also try this one but even not working

#include <stdio.h>
main()
{
    char a;
    printf("Enter the Name\n");
    scanf("%c",&a);

    if(a=='xyz')
    {
        printf("You enter xyz");
    }

    if(a=='abc')
    {
        printf("You enter abc");
    }
}
Posted
Updated 27-Mar-13 3:27am
v2

Jonathon has the right idea, but even once you have read the string from teh user correectly, you can't just use "==" to compare two strings - all it will do is compare a variable string address with a fixed string address and will always fail to match.

You need to look at using strcmp[^] instead to compare the string content:
C++
if (strcmp(a, "xyz") == 0)
   {
   ...
   }
 
Share this answer
 
Comments
Thomas Daniels 27-Mar-13 9:38am    
+5!
Jonathan [Darka] 27-Mar-13 17:18pm    
Damn, I was too focused on the scanf to notice that :)
Quote:
char a;
printf("Enter the Name\n");
scanf("%c",&a);
change to
C
char a[256];
printf("Enter the name\n");
fgets(a, 256, stdin);




Quote:
if(a=='xyz')
{
printf("You enter xyz");
}
if(a=='abc')
{
printf("You enter abc");
}
change to:
C
if (strcmp(a, "xyz") == 0)
{
  printf("You entered xyz\n");
}
else if (strcmp(a, "abc")==0)
{
  printf("You entered abc\n");
}


You may also simply write:
C
printf("You entered %s\n", a);


Note you must include string.h in order to use strcmp.




[UPDATE]
You are right, I forgot the "annoying newline NOT discarded by fgets". Try:
C
#include <stdio.h>
#include <string.h>
int main() {
  char a[256]={0};
  int len;
  printf("Enter the Name\n");

  if ( fgets(a,256,stdin) )

  // replace the possibly 'not-discarded' newline with string terminator
  len = strlen(a);
  if (len>0 && a[len-1]=='\n')
    a[len-1]='\0';

  if (strcmp(a, "xyz") == 0)
  {
    printf("You entered xyz\n");
  }
  else if (strcmp(a, "abc")==0)
  {
  printf("You entered abc\n");
  }
  printf("'%s'\n", a);
  return 0;
}


[/UPDATE]
 
Share this answer
 
v2
Comments
vishal2592 27-Mar-13 9:44am    
I try this one
main()
{
char a[256];
printf("Enter the Name\n");
fgets(a,256,stdin);
if (strcmp(a, "xyz") == 0)
{
printf("You entered xyz\n");
}
else if (strcmp(a, "abc")==0)
{
printf("You entered abc\n");
}
}
I used string.h
But event not working?
Try:
scanf("%s", &a)


Plus, you'll need to change the definition of a to a character array of a suitable size.
 
Share this answer
 
v2
Your Code is wrong.
In your Code you are compairing a character (a) with a string (abc or xyz) instead of scanf for taking the input use gets(a) to take the input then only your input will be considered as string.

Let me Know in case you need some other information.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900