Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
Why this if condition is not working when i need to check string from user?
 
#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 27-Mar-13 4:17am
vast25488
Edited 27-Mar-13 4:27am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
if (strcmp(a, "xyz") == 0)
   {
   ...
   }
  Permalink  
Comments
ProgramFOX at 27-Mar-13 9:38am
   
+5!
Jonathan [Darka] at 27-Mar-13 17:18pm
   
Damn, I was too focused on the scanf to notice that :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Quote:
char a;
printf("Enter the Name\n");
scanf("%c",&a);
change to
  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:
  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:
  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:
#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]
  Permalink  
v2
Comments
vast25 at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try:
scanf("%s", &a)
 
Plus, you'll need to change the definition of a to a character array of a suitable size.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100